|
@@ -1,122 +0,0 @@
|
|
|
-'use strict';
|
|
|
-
|
|
|
-const Controller = require('egg').Controller;
|
|
|
-const jsonwebtoken = require('jsonwebtoken');
|
|
|
-const { createProxyMiddleware, responseInterceptor } = require('http-proxy-middleware');
|
|
|
-class HomeController extends Controller {
|
|
|
- async index() {
|
|
|
- const { modules } = this.app.config;
|
|
|
-
|
|
|
- const { agent, service, module, method, item } = this.ctx.params;
|
|
|
-
|
|
|
- const address = modules[service];
|
|
|
-
|
|
|
- const requestMethod = this.ctx.request.method;
|
|
|
-
|
|
|
- const path = this.ctx.path;
|
|
|
- const exampleProxy = createProxyMiddleware(
|
|
|
- `/${agent}/${service}/`,
|
|
|
- {
|
|
|
- target: `${address}${path}`,
|
|
|
- changeOrigin: true,
|
|
|
- selfHandleResponse: true,
|
|
|
- onProxyRes: responseInterceptor(async (buffer, proxyRes, req, res) => {
|
|
|
- console.log(buffer, 'buffer');
|
|
|
- console.log(proxyRes, 'proxyRes');
|
|
|
- console.log(req, 'req');
|
|
|
- console.log(res, 'res');
|
|
|
- }),
|
|
|
- });
|
|
|
- this.app.use(exampleProxy);
|
|
|
- this.app.listen(3000);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- this.ctx.body = 111;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- async setLog({ agent, service, module, method, item, details, result, auth }) {
|
|
|
-
|
|
|
- const { modules, apipath } = this.app.config;
|
|
|
-
|
|
|
- const path = this.ctx.path;
|
|
|
-
|
|
|
- const logmodules = apipath[service];
|
|
|
-
|
|
|
- if (logmodules) {
|
|
|
-
|
|
|
- const items = await this.rewrite_path(logmodules, path);
|
|
|
- if (items && items.log) {
|
|
|
-
|
|
|
- let userName = null;
|
|
|
- let name = null;
|
|
|
- if (auth.decode) {
|
|
|
- userName = auth.decode.userName;
|
|
|
- name = auth.decode.name;
|
|
|
- }
|
|
|
- try {
|
|
|
- await this.ctx.curl(`${modules.log}/api/log/log/create`, {
|
|
|
- method: 'POST',
|
|
|
- dataType: 'json',
|
|
|
- data: { agent, service, module, method, item, details, result, userName, name },
|
|
|
- });
|
|
|
- } catch (error) {
|
|
|
- this.ctx.logger.error(`日志添加失败: ${error}`);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- async auth({ service, path }) {
|
|
|
- const { jwt, apipath } = this.app.config;
|
|
|
-
|
|
|
- const modules = apipath[service];
|
|
|
-
|
|
|
- if (!modules) return { errcode: 0, details: '' };
|
|
|
-
|
|
|
-
|
|
|
- const item = await this.rewrite_path(modules, path);
|
|
|
-
|
|
|
- if (item) return { errcode: 0, details: '' };
|
|
|
-
|
|
|
- if (!item.jwt) return { errcode: 0, details: '' };
|
|
|
-
|
|
|
- const token = this.ctx.header.authorization.split('Bearer ')[1];
|
|
|
- try {
|
|
|
-
|
|
|
- const decode = jsonwebtoken.verify(token, jwt.secret);
|
|
|
-
|
|
|
- if (item && item.issuer) {
|
|
|
-
|
|
|
- if (!item.issuer.includes(decode.iss)) return { errcode: -4001, details: 'issuer fail verification' };
|
|
|
- }
|
|
|
- return { errcode: 0, details: '', decode };
|
|
|
- } catch (error) {
|
|
|
- return { errcode: -4001, details: error.message };
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- async rewrite_path(modules, path) {
|
|
|
- return modules.find(e => {
|
|
|
- const { url } = e;
|
|
|
- let data = null;
|
|
|
- const urlList = url.split('/');
|
|
|
- const pathIist = path.split('/');
|
|
|
- const urlItem = urlList.findIndex(j => j.includes(':'));
|
|
|
- if (urlItem > 0) data = pathIist[urlItem];
|
|
|
- if (data !== null) urlList[urlItem] = data;
|
|
|
- const pathItem = urlList.join('/');
|
|
|
- if (pathItem === path) return e;
|
|
|
- return false;
|
|
|
- });
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-module.exports = HomeController;
|