Commit 794efb5d authored by huhao's avatar huhao

配置文件外移

parent f9da1759
...@@ -9,9 +9,6 @@ const jaeger = require('jaeger-client'); ...@@ -9,9 +9,6 @@ const jaeger = require('jaeger-client');
const log = require('log4js').getLogger(); const log = require('log4js').getLogger();
const httpLog = require('log4js').getLogger('http'); const httpLog = require('log4js').getLogger('http');
// 配置文件
const props = require('./props.json');
/** /**
* 业务异常 * 业务异常
*/ */
...@@ -199,8 +196,8 @@ function debug() { ...@@ -199,8 +196,8 @@ function debug() {
* opentracing链路跟踪中间件 * opentracing链路跟踪中间件
* https://github.com/opentracing-contrib/javascript-express * https://github.com/opentracing-contrib/javascript-express
*/ */
function tracing() { function tracing(opts) {
opentracing.initGlobalTracer(jaeger.initTracer(props.tracing)); opentracing.initGlobalTracer(jaeger.initTracer(opts.tracing));
const tracer = opentracing.globalTracer(); const tracer = opentracing.globalTracer();
return async function (ctx, next) { return async function (ctx, next) {
let wireCtx = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, ctx.headers); let wireCtx = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, ctx.headers);
...@@ -295,9 +292,9 @@ function headerx() { ...@@ -295,9 +292,9 @@ function headerx() {
/** /**
* Token解析中间件 * Token解析中间件
*/ */
function token() { function token(opts) {
// token公钥,缓存 // token公钥,缓存
const publicKey = axios(`${props.apiBase}/auth/oauth2/public-key.pem`); const publicKey = axios(`${opts.apiBase}/auth/oauth2/public-key.pem`);
return async function (ctx, next) { return async function (ctx, next) {
let authorizationToken = ctx.getAuthorizationToken(); let authorizationToken = ctx.getAuthorizationToken();
if (authorizationToken) { if (authorizationToken) {
...@@ -337,7 +334,7 @@ function token() { ...@@ -337,7 +334,7 @@ function token() {
/** /**
* 鉴权中间件 * 鉴权中间件
*/ */
function auth() { function auth(opts) {
return async function (ctx, next) { return async function (ctx, next) {
/** /**
* 当前用户 * 当前用户
...@@ -346,7 +343,7 @@ function auth() { ...@@ -346,7 +343,7 @@ function auth() {
if (ctx.token) { if (ctx.token) {
return http({ return http({
context: ctx, context: ctx,
url: `${props.apiBase}/auth/v3/user/getUser`, url: `${opts.apiBase}/auth/v3/user/getUser`,
data: { data: {
appKey: ctx.getAppKey() appKey: ctx.getAppKey()
} }
...@@ -361,10 +358,10 @@ function auth() { ...@@ -361,10 +358,10 @@ function auth() {
ctx.getApplicationToken = function () { ctx.getApplicationToken = function () {
return http({ return http({
context: ctx, context: ctx,
url: `${props.apiBase}/auth/v3/application/getTokenByApplication`, url: `${opts.apiBase}/auth/v3/application/getTokenByApplication`,
data: { data: {
appKey: props.appKey, appKey: opts.appKey,
appSecret: props.appSecret appSecret: opts.appSecret
} }
}); });
}; };
...@@ -375,12 +372,12 @@ function auth() { ...@@ -375,12 +372,12 @@ function auth() {
/** /**
* 用户行为跟踪上报中间件 * 用户行为跟踪上报中间件
*/ */
function track() { function track(opts) {
return async function (ctx, next) { return async function (ctx, next) {
ctx.track = true; // 全部接口收集 ctx.track = true; // 全部接口收集
ctx.trackEvent = { ctx.trackEvent = {
__referer__: `${props.serviceName}:${ctx.path}`, __referer__: `${opts.serviceName}:${ctx.path}`,
app_key: ctx.getAppKey() || _.get(ctx.token, 'aud') || props.appKey, app_key: ctx.getAppKey() || _.get(ctx.token, 'aud') || opts.appKey,
event: { event: {
key: 'api' key: 'api'
}, },
...@@ -407,7 +404,7 @@ function track() { ...@@ -407,7 +404,7 @@ function track() {
}).join('&'); }).join('&');
axios({ axios({
method: 'GET', method: 'GET',
url: `${props.trackBase}${trackQuery}`, url: `${opts.trackBase}${trackQuery}`,
headers: { headers: {
'user-agent': ctx.headers['user-agent'] 'user-agent': ctx.headers['user-agent']
} }
...@@ -451,14 +448,14 @@ function notEmpty(value, message) { ...@@ -451,14 +448,14 @@ function notEmpty(value, message) {
/** /**
* App扩展 * App扩展
*/ */
function extendApp(app) { function extendApp(app, opts) {
app.use(errorHandler()); // 启用异常处理 app.use(errorHandler()); // 启用异常处理
app.use(debug()); // 启用请求参数调试 app.use(debug()); // 启用请求参数调试
app.use(tracing()); // 启用链路跟踪 app.use(tracing(opts)); // 启用链路跟踪
app.use(headerx()); // 启用亿童请求头提取 app.use(headerx()); // 启用亿童请求头提取
app.use(token()); // 启动token解析 app.use(token(opts)); // 启动token解析
app.use(auth()); // 启动鉴权中间件 app.use(auth(opts)); // 启动鉴权中间件
app.use(track()); // 启动用户行为跟踪 app.use(track(opts)); // 启动用户行为跟踪
return app; return app;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment