32 Star 106 Fork 25

来贝科技 / 微信API网关

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
app.js 4.46 KB
一键复制 编辑 原始数据 按行查看 历史
老技 提交于 2023-04-25 00:43 . fix: bug for err status response
"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);
});
NodeJS
1
https://gitee.com/libfintech/wechat_api_gateway.git
git@gitee.com:libfintech/wechat_api_gateway.git
libfintech
wechat_api_gateway
微信API网关
master

搜索帮助