"use strict";
/**
* 应用入口
* Created by liamjung on 2018/5/21.
*/
require("co")(function* () {
let config = require("./app_config");
let appConfig;
//加载配置
if (typeof config === "function") {
appConfig = yield config();
} else {
appConfig = global.appConfig = config;
}
const {Log} = require("@libfintech/api-gateway-core/log/log");
global.Log = Log;
const {Pipeline} = require("@libfintech/api-gateway-core/entity/pipeline");
const Koa = require("koa");
const bodyParser = require("@libfintech/api-gateway-core/util/koa_body_parser");
const logger = require("koa-logger");
const stripAnsi = require("strip-ansi");
const fs = require("fs");
const appRoot = require("app-root-path");
const app = new Koa();
app.on("error", (err, ctx) => Log.error("server error", err, ctx));
/**
* 响应客户端
* @param ctx
* @param resp
*/
async function responseClient(ctx, resp) {
//设置状态
ctx.response.status = resp.status;
if (resp.headers) {
// fix: The response headers can't include "Content-Length" with chunked encoding
delete resp.headers['transfer-encoding'];
//设置响应头
await ctx.response.set(resp.headers);
}
if (!ctx.type && resp.body && resp.body.startsWith("{"))
ctx.type = "application/json";
ctx.length = !resp.body ? 0 : Buffer.byteLength(resp.body, "utf8");
//刷新任何已设置的响应头,并开始主体
ctx.response.flushHeaders();
//设置响应体
ctx.response.body = resp.body;
}
//中间件
app.use(bodyParser({
enableTypes: ["json", "form", "file", "text"],
extendTypes: {
//will parse text/json type body as a JSON string
json: ["text/json"],
text: ["*/*"]
}
}));
app.use(logger((str, args) => {
Log.info(stripAnsi(str));
}));
//初始化
app.use(async (ctx, next) => {
//初始化管道
ctx.pipeline = new Pipeline(ctx);
ctx.pipeline.logger.debug("headers:\t\t" + JSON.stringify(ctx.request.headers));
ctx.pipeline.logger.debug(ctx.request.method + ":" + ctx.pipeline.request.originalPath);
ctx.pipeline.logger.debug("querystring:\t\t" + ctx.pipeline.request.querystring);
let contentType = ctx.headers["content-type"];
if (!contentType || !contentType.toLowerCase().includes("multipart/form-data"))
ctx.pipeline.logger.debug("body:\t\t" + ctx.pipeline.request.body);
ctx.pipeline.logger.debug("----------------------------------------------------------------------------------------------------------------------");
await next();
});
//引入插件
appConfig.plugins.forEach((item, index) => {
let path = item.path;
if (!item.enable) {
//未开启时,忽略
Log.info("Plugin (index: " + index + "; path: " + path + ") is not enabled");
return;
}
//入口方法名称必须为main
let main = require(path + "/index").main;
let configPath = item.config_path;
//插件配置
let configs = {};
if (configPath) {
let configFiles = fs.readdirSync(appRoot.path + item.config_path);
for (let cf of configFiles) {
if (cf.endsWith(".conf.js"))
configs = Object.assign(configs, require("." + item.config_path + "/" + cf));
}
}
app.use(async (ctx, next) => {
ctx.pipeline.logger.info("Plugin (index: " + index + "; path: " + path + ")");
try {
await main(ctx.pipeline, configs);
await next();
} catch (err) {
if (err.status) {
//响应客户端
await responseClient(ctx, {
status: err.status,
headers: err.headers,
body: err.body
});
} else
ctx.pipeline.logger.error("server error", err, ctx)
}
});
Log.info("Load plugin (index: " + index + "; path: " + path + ")");
});
app.use(async (ctx, next) => await responseClient(ctx, ctx.pipeline.response));
Log.info("App has been started, listen " + appConfig.port + ".");
app.listen(appConfig.port);
});
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本项目是基于koa2框架实现的API网关,使用koa2框架的中间件机制提供插件功能。通过使用微信公众号、服务注册&发现、反向代理等等插件的组合,来有效解决来自不同客户端和服务器请求的统一接入、分发、监控等问题
资源推荐
资源详情
资源评论
收起资源包目录
wechat_api_gateway-master.zip (20个子文件)
wechat_api_gateway-master
yarn.lock 92KB
app_config.js.example 3KB
app.js 4KB
template
example.msg 28B
LICENSE 1KB
package.json 619B
plugin_config
authe
xxx.conf.js.example 234B
sso.conf.js 188B
transfer
xxx.conf.js.example 517B
sso.conf.js 224B
wx_gateway_response
wx.conf.js 440B
ip
xxx.conf.js.example 560B
wx_gateway
wx.conf.js 788B
.gitignore 17B
images
server_url.png 33KB
pipeline.png 9KB
ip_white_list.png 70KB
domain_authe.png 65KB
net.png 31KB
mp_config.json.example 1KB
共 20 条
- 1
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6656
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功