Comparing version 0.0.14 to 0.0.15
@@ -8,14 +8,46 @@ #!/usr/bin/env node | ||
let includes = [], excludes = [], password = null; | ||
const Head = { | ||
Server: 'PhoenixNirvana-Framework' | ||
} | ||
function app(req, res){ | ||
console.log(req.method, req.url, req.headers); | ||
// 如果设置了密码 | ||
if (!!password) { | ||
// 客户头未包含X-AUTH或不等于password | ||
if (req.headers['x-auth'] !== password) { | ||
res.writeHead(403, Head); | ||
res.end(); | ||
return; | ||
} | ||
} | ||
let matches = /\/(?<module>.*?)\/(?<controller>.*?)\/(?<action>.*)/.exec(req.url.split('?')[0]); | ||
if (!matches || matches.groups.action === '') { | ||
res.writeHead(404); | ||
res.writeHead(404, Head); | ||
res.end(); | ||
return; | ||
} | ||
// allow access module, empty is allow all | ||
if (includes.length>0 && !includes.includes(matches.groups.module)) { | ||
res.writeHead(403, Head); | ||
res.end(); | ||
return; | ||
} | ||
// deny access module | ||
if (excludes.length>0 && excludes.includes(matches.groups.module)) { | ||
res.writeHead(403, Head); | ||
res.end(); | ||
return; | ||
} | ||
const uri = `${matches.groups.module}:${matches.groups.controller}/${matches.groups.action}?${req.url.split('?')[1]}`; | ||
manage.run(uri).then(data => { | ||
res.writeHead(200, {'Content-Type': 'text/plain'}); | ||
res.end(data); | ||
if (typeof data === 'string') { | ||
res.writeHead(200, Object.assign({'Content-Type': 'text/plain'}, Head)); | ||
res.end(data); | ||
} else { | ||
res.writeHead(200, Object.assign({'Content-Type': 'text/json'}, Head)); | ||
res.end(JSON.stringify(data)); | ||
} | ||
}).catch(err => { | ||
@@ -31,4 +63,10 @@ res.writeHead(404); | ||
.command('server [port] [host]') | ||
.action((port = 9002, host = '127.0.0.1') => { | ||
const server = http.createServer(app).listen(port, host, () => { | ||
.option('-i, --include <module list>', '仅允许指定模块列表,逗号分割') | ||
.option('-e, --exclude <module list>', '排除指定模块列表,逗号分割') | ||
.option('-a, --auth <password>', '授权码,客户端通过设置X-AUTH头指定') | ||
.action((port = 9002, host = '127.0.0.1', opts) => { | ||
includes = (opts.include) ? opts.include.split(/,\s*/) : []; | ||
excludes = (opts.exclude) ? opts.exclude.split(/,\s*/) : []; | ||
password = opts.auth; | ||
http.createServer(app).listen(port, host, () => { | ||
console.log(`listening ${host}:${port}`) | ||
@@ -35,0 +73,0 @@ }) |
{ | ||
"name": "pn-cli", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "phoenix nirvana manager", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
18804
542