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