正在显示
6 个修改的文件
包含
187 行增加
和
2 行删除
.github/workflows/nodejs.yml
0 → 100644
| 1 | +# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node | ||
| 2 | +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions | ||
| 3 | + | ||
| 4 | +name: Node.js CI | ||
| 5 | + | ||
| 6 | +on: | ||
| 7 | + push: | ||
| 8 | + branches: | ||
| 9 | + - main | ||
| 10 | + - master | ||
| 11 | + pull_request: | ||
| 12 | + branches: | ||
| 13 | + - main | ||
| 14 | + - master | ||
| 15 | + schedule: | ||
| 16 | + - cron: '0 2 * * *' | ||
| 17 | + | ||
| 18 | +jobs: | ||
| 19 | + build: | ||
| 20 | + runs-on: ${{ matrix.os }} | ||
| 21 | + | ||
| 22 | + strategy: | ||
| 23 | + fail-fast: false | ||
| 24 | + matrix: | ||
| 25 | + node-version: [10] | ||
| 26 | + os: [ubuntu-latest, windows-latest, macos-latest] | ||
| 27 | + | ||
| 28 | + steps: | ||
| 29 | + - name: Checkout Git Source | ||
| 30 | + uses: actions/checkout@v2 | ||
| 31 | + | ||
| 32 | + - name: Use Node.js ${{ matrix.node-version }} | ||
| 33 | + uses: actions/setup-node@v1 | ||
| 34 | + with: | ||
| 35 | + node-version: ${{ matrix.node-version }} | ||
| 36 | + | ||
| 37 | + - name: Install Dependencies | ||
| 38 | + run: npm i -g npminstall && npminstall | ||
| 39 | + | ||
| 40 | + - name: Continuous Integration | ||
| 41 | + run: npm run ci | ||
| 42 | + | ||
| 43 | + - name: Code Coverage | ||
| 44 | + uses: codecov/codecov-action@v1 | ||
| 45 | + with: | ||
| 46 | + token: ${{ secrets.CODECOV_TOKEN }} |
app/controller/proxy.js
0 → 100644
| 1 | +'use strict'; | ||
| 2 | +const Controller = require('egg').Controller; | ||
| 3 | + | ||
| 4 | +class ProxyController extends Controller { | ||
| 5 | + async info() { | ||
| 6 | + const { ctx } = this; | ||
| 7 | + ctx.body = { | ||
| 8 | + name: `hello ${ctx.params.id}`, | ||
| 9 | + }; | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + getOptions() { | ||
| 14 | + const { ctx, config } = this; | ||
| 15 | + const { url, type } = ctx; | ||
| 16 | + | ||
| 17 | + let catalog = url.substring(1, url.indexOf('/', 1)); | ||
| 18 | + let pathUrl = url.substring(url.indexOf('/', 1)); | ||
| 19 | + let fullPath = '/' + catalog + config.restful.version + pathUrl; | ||
| 20 | + | ||
| 21 | + return { | ||
| 22 | + 'host': config.restful.host, //后台请求地址 | ||
| 23 | + 'port': config.restful.port, | ||
| 24 | + 'path': fullPath, | ||
| 25 | + 'method': type, | ||
| 26 | + 'agent': false, | ||
| 27 | + 'headers': { | ||
| 28 | + "Accept": "application/json", | ||
| 29 | + "Content-Type": "application/json", | ||
| 30 | + 'User-Agent': 'Request for Express' | ||
| 31 | + } | ||
| 32 | + }; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + addToken(options) { | ||
| 36 | + const { ctx } = this; | ||
| 37 | + | ||
| 38 | + if (ctx.session.passport && ctx.session.passport.user && ctx.session.passport.user && ctx.session.passport.user.token) { | ||
| 39 | + options.headers['Authorization'] = "Bearer " + ctx.session.passport.user.token; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + if (ctx.headers['device-id']) { | ||
| 43 | + options.headers['Device-Id'] = ctx.headers['device-id']; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + if (ctx.ip) { | ||
| 47 | + options.headers['Client-Ip'] = ctx.ip; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + return options; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + splitUrl(fullUrl) { | ||
| 55 | + const { config } = this; | ||
| 56 | + | ||
| 57 | + if (config.prefix && config.prefix.length > 1) { | ||
| 58 | + fullUrl = fullUrl.substring(config.prefix.length - 1); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + return fullUrl.substring(4); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + async get() { | ||
| 66 | + const { ctx, config, logger } = this; | ||
| 67 | + const { originalUrl } = ctx; | ||
| 68 | + const { restful = {} } = config; | ||
| 69 | + const { host } = restful; | ||
| 70 | + const { access_token } = ctx.session.user_info; | ||
| 71 | + const url = originalUrl.substring(4); | ||
| 72 | + | ||
| 73 | + const result = await ctx.curl(`${host}${url}`, { | ||
| 74 | + method: 'GET', | ||
| 75 | + dataType: 'json', | ||
| 76 | + headers: { | ||
| 77 | + 'authorization': `Bearer ${access_token}`, | ||
| 78 | + 'accept': 'application/json', | ||
| 79 | + 'content-type': 'application/json' | ||
| 80 | + }, | ||
| 81 | + timeout: [5000, 60000] | ||
| 82 | + }); | ||
| 83 | + | ||
| 84 | + logger.info( | ||
| 85 | + "proxy url:", | ||
| 86 | + `${host}${url}`, | ||
| 87 | + 'headers:', | ||
| 88 | + { | ||
| 89 | + 'authorization': `Bearer ${access_token}`, | ||
| 90 | + 'accept': 'application/json', | ||
| 91 | + 'content-type': 'application/json' | ||
| 92 | + }, | ||
| 93 | + "result:", | ||
| 94 | + result | ||
| 95 | + ); | ||
| 96 | + | ||
| 97 | + ctx.body = result.data; | ||
| 98 | + | ||
| 99 | + return ctx.body; | ||
| 100 | + } | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +module.exports = ProxyController; |
| @@ -4,6 +4,6 @@ | @@ -4,6 +4,6 @@ | ||
| 4 | * @param {Egg.Application} app - egg application | 4 | * @param {Egg.Application} app - egg application |
| 5 | */ | 5 | */ |
| 6 | module.exports = app => { | 6 | module.exports = app => { |
| 7 | - const { router, controller } = app; | ||
| 8 | - router.get('/', controller.home.index); | 7 | + require('./router/api')(app); |
| 8 | + require('./router/home')(app); | ||
| 9 | }; | 9 | }; |
app/router/api.js
0 → 100644
| 1 | +'use strict'; | ||
| 2 | + | ||
| 3 | +module.exports = app => { | ||
| 4 | + const { router, controller } = app; | ||
| 5 | + const { proxy } = controller; | ||
| 6 | + // 给proxy设置token | ||
| 7 | + router.get(/^\/api\/[\w/]+/, 'proxy.get'); | ||
| 8 | + // router.post(/^\/api\/[\w/]+/, 'proxy.post'); | ||
| 9 | + // router.put(/^\/api\/[\w/]+/, 'proxy.put'); | ||
| 10 | + // router.delete(/^\/api\/[\w/]+/, 'proxy.del'); | ||
| 11 | +} |
app/router/home.js
0 → 100644
| @@ -39,6 +39,24 @@ module.exports = appInfo => { | @@ -39,6 +39,24 @@ module.exports = appInfo => { | ||
| 39 | } | 39 | } |
| 40 | }; | 40 | }; |
| 41 | 41 | ||
| 42 | + config.restful = { | ||
| 43 | + tokenUrl: '/uaa/v1/auth/tokens', | ||
| 44 | + tokenMethod: 'POST', | ||
| 45 | + scope: "global_access:tenant_admin", | ||
| 46 | + // host: 'http://118.178.181.180:20000/', | ||
| 47 | + host: 'http://47.110.250.177:20000/', | ||
| 48 | + // host: 'http://47.110.158.110:20000/', | ||
| 49 | + // host:'http://120.27.220.60:20000/', | ||
| 50 | + // host: 'http://39.104.52.206:20000/', | ||
| 51 | + // host: 'http://47.99.189.12:20000/', | ||
| 52 | + ossUrl: 'http://47.110.250.177:20000/', | ||
| 53 | + version: '/v1', | ||
| 54 | + // host: '47.110.158.110', | ||
| 55 | + // host: '120.27.220.60', | ||
| 56 | + // host: '39.104.52.206', | ||
| 57 | + port: 20000 | ||
| 58 | + }; | ||
| 59 | + | ||
| 42 | config.static = { | 60 | config.static = { |
| 43 | prefix: '', | 61 | prefix: '', |
| 44 | dir: path.join(appInfo.baseDir, 'app/public'), | 62 | dir: path.join(appInfo.baseDir, 'app/public'), |
请
注册
或
登录
后发表评论