Authored by htoooth

add api logger

... ... @@ -18,6 +18,7 @@
"bluebird": "^3.5.0",
"body-parser": "^1.17.1",
"cookie-parser": "^1.4.3",
"cookie-session": "^2.0.0-beta.1",
"express": "^4.15.2",
"express-session": "^1.15.2",
"iview": "^2.0.0-rc.8",
... ...
... ... @@ -9,6 +9,7 @@ const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const Express = require('express');
const session = require('express-session');
const cookieSession = require('cookie-session')
const favicon = require('serve-favicon');
const path = require('path');
... ... @@ -28,10 +29,12 @@ global.yoho = {
co: global.Promise.coroutine
};
app.use(session({
app.use(cookieSession({
name: 'yoho-shop',
secret: 'yoho!shop@manage',
resave: false,
saveUninitialized: true
cookie: {
maxAge: 24 * 60 * 60 * 1000
}
}));
app.use(favicon(path.join(__dirname, '/favicon.ico')));
... ...
... ... @@ -16,9 +16,8 @@ const API_INTERNAL_ERROR = {
};
class Api extends Context {
constructor(domain = config.apiDomain.auth) {
constructor() {
super();
this.domain = domain;
}
get(url, data, headers) {
this.logGet(url, data);
... ...
const _ = require('lodash');
// 域名列表
const domains = {
auth: 'http://serve.yohobuy.com',
shop: 'http://192.168.102.210:8088/platform'
};
// api调用列表
let domainApis = {
auth: {
login: '/service/account/v1/Profile/login'
login: {
path: '/service/account/v1/Profile/login'
}
},
shop: {
profile: '/SellerShopController/queryShopsByAdminPid'
profile: {
path: '/SellerShopController/queryShopsByAdminPid'
},
brand: {
path: '/SellerProductController/getSellerBrandInfo'
},
sort: {
path: '/SellerProductController/getSellerSortInfo'
}
}
};
// 域名列表
const domains = {
auth: 'http://serve.yohobuy.com',
shop: 'http://192.168.102.210:8088/platform'
};
_.each(domainApis, (apis, domainName) => {
_.each(apis, (url, api) => {
apis[api] = _.get(domains, domainName, '') + url;
_.each(apis, (uri, api) => {
apis[api].url = domains[domainName] + uri.path;
});
});
... ...
... ... @@ -4,6 +4,20 @@
* @date: 2017/04/13
*/
const Api = require('../common/api');
const allowdUrls = global.yoho.apiDomain;
const _ = require('lodash');
const Fn = require('lodash/fp');
const logger = global.yoho.logger;
function _matchUrl(path, api) {
return api.path.toLowerCase() === path.toLowerCase();
}
function allowed(path) {
return _.flow(_.toPairs, Fn.find((api) => {
return _matchUrl(path, api[1])
}))(allowdUrls.shop);
}
module.exports = (req, res, next) => {
let api = new Api();
... ... @@ -12,7 +26,26 @@ module.exports = (req, res, next) => {
req,
res
});
return api.post(req.originalUrl, req.body).then(data => {
res.json(data);
}).catch(next);
let allowApi = allowed(req.path);
if (!allowApi) {
logger.warn(`proxy ${req.method} fail`, `${req.path} can't find proxy url`);
return next();
}
logger.info(`proxy ${req.method} successful ok`, `[${req.path}] => [${allowApi[1].url}]`);
if (req.method.toLowerCase() === 'get') {
return api.get(allowApi[1].url, req.query).then(data => {
res.json(data);
}).catch(next);
}
if (req.method.toLowerCase() === 'post') {
return api.post(allowApi[1].url, req.body).then(data => {
res.json(data);
}).catch(next);
}
};
... ...
... ... @@ -14,7 +14,10 @@ class loginModel extends Context {
let self = this;
return co(function * () {
let userInfo = yield self.instance(Api).post(apiDomain.auth.login, JSON.stringify([username, password, 2]))
let userInfo = yield self.instance(Api).post(
apiDomain.auth.login.url,
JSON.stringify([username, password, 2])
);
if (userInfo.code !== 200 || !userInfo.data.pid) {
return Promise.reject({code: 500, message: '登录服务器错误'});
... ... @@ -36,7 +39,7 @@ class loginModel extends Context {
}
profile(pid) {
return this.instance(Api).get(apiDomain.shop.profile, {userId: pid});
return this.instance(Api).get(apiDomain.shop.profile.url, {userId: pid});
}
}
... ...