forked from ecomfe/baidubce-sdk
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 支持文档示例调试; createBucket支持创建多AZ存储桶
- Loading branch information
1 parent
f631658
commit 07b27ed
Showing
15 changed files
with
7,206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ node_modules | |
bower_components | ||
.travis.yml | ||
appveyor.yml | ||
example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"printWidth": 120, | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"semi": true, | ||
"trailingComma": "none", | ||
"bracketSpacing": false, | ||
"quoteProps": "consistent", | ||
"arrowParens": "avoid", | ||
"jsxBracketSameLine": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"execMap": { | ||
"ts": "ts-node" | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "@baiducloud/sdk-docs", | ||
"version": "0.0.1", | ||
"description": "Baidu Cloud Engine JavaScript SDK Docs", | ||
"main": "src/app.ts", | ||
"scripts": { | ||
"build": "rimraf dist && tsc", | ||
"start": "nodemon src/app.ts", | ||
"debug": "nodemon --exec 'node --inspect=0.0.0.0:9230 --require ts-node/register src/app.ts'" | ||
}, | ||
"keywords": [ | ||
"baiducloud" | ||
], | ||
"authors": [ | ||
"lurunze <[email protected]>" | ||
], | ||
"license": "MIT", | ||
"dependencies": { | ||
"@types/express-jwt": "^6.0.4", | ||
"@types/lodash": "^4.14.191", | ||
"amis": "^2.6.1-alpha.0", | ||
"body-parser": "^1.20.1", | ||
"cookie-parser": "^1.4.6", | ||
"cors": "^2.8.5", | ||
"express": "^4.18.2", | ||
"jsonwebtoken": "^9.0.0", | ||
"lodash": "^4.17.21", | ||
"react": "^18.2.0", | ||
"response-time": "^2.3.2", | ||
"swagger-jsdoc": "^6.2.7", | ||
"swagger-ui-express": "^4.6.0" | ||
}, | ||
"devDependencies": { | ||
"@types/body-parser": "^1.19.2", | ||
"@types/cookie-parser": "^1.4.3", | ||
"@types/cors": "^2.8.13", | ||
"@types/express": "^4.17.15", | ||
"@types/jsonwebtoken": "^9.0.1", | ||
"@types/node": "^18.11.18", | ||
"@types/response-time": "^2.3.5", | ||
"@types/swagger-jsdoc": "^6.0.1", | ||
"@types/swagger-ui-express": "^4.1.3", | ||
"nodemon": "^2.0.20", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>登录</title> | ||
<link rel="stylesheet" type="" href="sdk.css" /> | ||
<link rel="stylesheet" type="" href="cxd.css" /> | ||
<link rel="stylesheet" href="iconfont.css" /> | ||
<style> | ||
body { | ||
margin: 0; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script type="text/javascript" src="sdk.js"></script> | ||
<script> | ||
(function () { | ||
const amis = amisRequire('amis/embed'); | ||
const amisJSON = { | ||
type: 'page', | ||
style: { | ||
background: 'rgb(235, 237, 244)' | ||
}, | ||
body: [ | ||
{ | ||
type: 'alert', | ||
title: '请注意', | ||
body: { | ||
type: 'tpl', | ||
tpl: '当前应用为了方便调试,会将当前信息存储到本地浏览器(sessionStorage)中,为了您的安全,请创建<a target="_blank" href="https://console.bce.baidu.com/iam/#/iam/accesslist">临时AK、SK</a>测试使用,测试完成后尽快删除测试密钥对' | ||
}, | ||
level: 'warning', | ||
showIcon: true, | ||
className: 'mb-3' | ||
}, | ||
{ | ||
type: 'form', | ||
mode: 'normal', | ||
api: '/api/login', | ||
redirect: '/api-docs', | ||
persistData: 'bosclient-sdk-config', | ||
style: { | ||
position: 'absolute', | ||
width: '500px', | ||
top: '50%', | ||
left: '50%', | ||
transform: 'translate(-50%, -50%)' | ||
}, | ||
body: [ | ||
{ | ||
label: 'Endpoint', | ||
name: 'endpoint', | ||
required: true, | ||
type: 'select', | ||
selectFirst: true, | ||
menuTpl: `<div style='width: 450px; display: flex; justify-content: space-between; align-items: center;'> | ||
<span style='display: inline-block'>\${value}</span> | ||
<span class='label label-info'>\${label}</span> | ||
</div>`, | ||
options: [ | ||
{value: 'https://bj.bcebos.com', label: '北京'}, | ||
{value: 'https://bd.bcebos.com', label: '保定'}, | ||
{value: 'https://su.bcebos.com', label: '苏州'}, | ||
{value: 'https://gz.bcebos.com', label: '广州'}, | ||
{value: 'https://hkg.bcebos.com', label: '中国香港'}, | ||
{value: 'https://fwh.bcebos.com', label: '金融华中 - 武汉'}, | ||
{value: 'https://fsh.bcebos.com', label: '华东 - 上海'} | ||
] | ||
}, | ||
{ | ||
label: 'Access Key ID (AK)', | ||
type: 'input-text', | ||
name: 'ak', | ||
placeholder: '请输入Access Key ID (AK)', | ||
required: true, | ||
validations: { | ||
isAlphanumeric: true, | ||
maxLength: 32 | ||
} | ||
}, | ||
{ | ||
label: 'Secret Access Key (SK)', | ||
type: 'input-text', | ||
name: 'sk', | ||
placeholder: '请输入Secret Access Key (SK)', | ||
required: true, | ||
validations: { | ||
isAlphanumeric: true, | ||
maxLength: 32 | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}; | ||
const amisScoped = amis.embed('#root', amisJSON); | ||
})(); | ||
</script> | ||
</body> | ||
<script> | ||
function login() { | ||
const endpoint = document.getElementById('endpoint')?.value; | ||
const ak = document.getElementById('ak')?.value; | ||
const sk = document.getElementById('sk')?.value; | ||
console.log({endpoint, sk, ak}); | ||
} | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import path from 'path'; | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
import cors from 'cors'; | ||
import cookieParser from 'cookie-parser'; | ||
import responseTime from 'response-time'; | ||
import pick from 'lodash/pick'; | ||
import swaggerJSDoc from 'swagger-jsdoc'; | ||
import swaggerUI from 'swagger-ui-express'; | ||
import jwt from 'jsonwebtoken'; | ||
import type {VerifyErrors, JwtPayload} from 'jsonwebtoken'; | ||
|
||
import {jwtSecretKey, jwtAlgorithm} from './config'; | ||
import {PORT} from './config'; | ||
// @ts-ignore | ||
import sdk from '../../index'; | ||
import SwaggerOptions from './swagger'; | ||
import BucketRouter from './routes/Bucket'; | ||
|
||
const app = express(); | ||
const swaggerSpec = swaggerJSDoc(SwaggerOptions); | ||
|
||
/** 静态资源 */ | ||
app.use(express.static(path.join(__dirname, '../public'))); | ||
app.use(express.static(path.join(__dirname, '../node_modules/amis/sdk'))); | ||
|
||
/** 请求中间件 */ | ||
app.use(cors({origin: '*', credentials: false})); | ||
app.use( | ||
bodyParser.urlencoded({ | ||
extended: true | ||
}) | ||
); | ||
/** 响应时间 */ | ||
app.use(responseTime()); | ||
/** 请求体 */ | ||
app.use(bodyParser.json()); | ||
app.use(cookieParser()); | ||
/** 全局拦截器 */ | ||
app.use((req, res, next) => { | ||
const token = req.cookies['token']; | ||
|
||
if (req.url === '/login' || req.url === '/api/login') { | ||
next(); | ||
} else { | ||
if (!token) { | ||
console.log('Authentication Token not found and redirect to login page.'); | ||
return res.redirect('/login'); | ||
} | ||
|
||
// @ts-ignore | ||
jwt.verify(token, jwtSecretKey, (error: VerifyErrors, decode: JwtPayload) => { | ||
if (error) { | ||
return res.status(403).send('Authentication failed, please login again.'); | ||
} else { | ||
const config = pick(decode, ['endpoint', 'credentials']); | ||
const bosClient = new sdk.BosClient(config); | ||
|
||
res.locals.client = bosClient; | ||
next(); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
/** 登录模块 */ | ||
app.use('/login', express.static(path.join(__dirname, '../public/login.html'))); | ||
app.post('/api/login', (req, res) => { | ||
const {endpoint, ak, sk} = req.body; | ||
|
||
if (!endpoint || !sk || !sk) { | ||
return res.status(401).json({status: 401, message: 'Authentication failed', data: null}); | ||
} | ||
|
||
const token = jwt.sign( | ||
{ | ||
endpoint, | ||
credentials: {ak, sk} | ||
}, | ||
jwtSecretKey, | ||
{ | ||
algorithm: jwtAlgorithm, | ||
expiresIn: '90 days' | ||
} | ||
); | ||
|
||
res.cookie('token', token); | ||
return res.status(200).json({token, status: 0, message: 'Login Successfully'}); | ||
}); | ||
|
||
/** API路由 */ | ||
app.use('/', BucketRouter); | ||
app.use( | ||
'/api-docs', | ||
swaggerUI.serve, | ||
swaggerUI.setup(swaggerSpec, { | ||
// explorer: true, | ||
// customJs: '', | ||
customSiteTitle: 'Bos OpenAPI调试', | ||
customfavIcon: 'https://bce.bdstatic.com/img/favicon.ico' | ||
// customCssUrl: 'https://cdn.jsdelivr.net/npm/[email protected]/themes/3.x/theme-newspaper.css' | ||
}) | ||
); | ||
|
||
app.listen(PORT, () => { | ||
console.debug('✨ Server tart successfully!'); | ||
console.debug('Server listening on port: ' + PORT); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type {Algorithm} from 'jsonwebtoken'; | ||
|
||
export const jwtSecretKey = 'bce-sdk-js'; | ||
|
||
export const jwtAlgorithm: Algorithm = 'HS256'; | ||
|
||
export const PORT = process.env.PORT || 3000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import express from 'express'; | ||
const router = express.Router(); | ||
|
||
router.get('/listBuckets', async (req, res) => { | ||
const client = res.locals.client; | ||
|
||
try { | ||
const response = await client.listBuckets(); | ||
|
||
return res.status(200).json({...response}); | ||
} catch (error: any) { | ||
return res.status(error.status_code || 500).json({...error}); | ||
} | ||
}); | ||
|
||
router.put('/createBucket', async (req, res) => { | ||
const client = res.locals.client; | ||
const {bucketName, enableMultiAZ} = req.body; | ||
|
||
try { | ||
const response = await client.createBucket(bucketName, {body: {enableMultiAZ: !!enableMultiAZ}}); | ||
|
||
return res.status(200).json({...response}); | ||
} catch (error: any) { | ||
return res.status(error.status_code || 500).json({...error}); | ||
} | ||
}); | ||
|
||
export default router; |
Oops, something went wrong.