-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 04bbb2f
Showing
15 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module.exports = { | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": ["eslint:recommended", "google"], | ||
"parserOptions": { | ||
"sourceType": "module", | ||
"ecmaVersion": 2017 | ||
}, | ||
"rules": { | ||
"object-curly-spacing": [ | ||
2, | ||
"always" | ||
], | ||
"indent": [ | ||
"error", | ||
4 | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"no-invalid-this": 0, | ||
"require-jsdoc": ["error", { | ||
"require": { | ||
"FunctionDeclaration": false, | ||
"MethodDefinition": false, | ||
"ClassDeclaration": false, | ||
"ArrowFunctionExpression": false, | ||
"FunctionExpression": false | ||
} | ||
}], | ||
"max-len": [ | ||
"error", | ||
120 | ||
], | ||
"new-cap": 0 | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
*.env | ||
*.dat | ||
*.DS_Store | ||
*.gz | ||
*.log | ||
*.out | ||
*.pid | ||
*.seed | ||
*.swp | ||
*.un~ | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
*.sublime-* | ||
|
||
logs | ||
node_modules/ | ||
npm-debug.log | ||
pids | ||
results | ||
coverage | ||
coverage.html | ||
.cover* | ||
lib-cov | ||
|
||
bundles | ||
docs/_dist | ||
admin/public/styles/auth.css | ||
admin/public/styles/auth.min.css | ||
admin/public/styles/keystone.css | ||
admin/public/styles/keystone.min.css | ||
reports | ||
|
||
test/e2e/drivers/* | ||
|
||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
require('./src/index').init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "moniotr_smart_router_data_server", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"lint": "node_modules/eslint/bin/eslint.js ./", | ||
"fix": "node_modules/eslint/bin/eslint.js ./ --fix" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/yuanchenxi95/MonIoTr_Smart_Router_Data_Server.git" | ||
}, | ||
"author": "Chenxi Yuan", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/yuanchenxi95/MonIoTr_Smart_Router_Data_Server/issues" | ||
}, | ||
"homepage": "https://github.com/yuanchenxi95/MonIoTr_Smart_Router_Data_Server#readme", | ||
"dependencies": { | ||
"dotenv": "^4.0.0", | ||
"axios": "^0.17.0", | ||
"koa": "^2.4.1", | ||
"koa-bodyparser": "^4.2.0", | ||
"koa-compress": "^2.0.0", | ||
"koa-router": "^7.2.1", | ||
"koa-session": "^5.5.0", | ||
"log4js": "^2.3.11", | ||
"lodash": "^4.17.4" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^4.11.0", | ||
"eslint-config-google": "^0.9.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MONGO_DB_HOST="YOUR_MONGO_DB_URI" | ||
COOKIE_SECRET="YOUR_COOKIE_SECRET" | ||
SSL_PASS_PHRASE="YOUR_SSL_PASS_PHRASE" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports.router = require('./routers'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const Router = require('koa-router'); | ||
const bodyParser = require('koa-bodyparser'); | ||
const { responder } = require('./middlewares'); | ||
const user = require('./user'); | ||
|
||
/** | ||
* 这里进行区分 | ||
* api 接口统一添加 api 前缀 | ||
* 大家都可以访问的,什么都不加 | ||
* 普通用户才能访问的,加 '/user' 前缀 | ||
* 库管用户可以访问的,加 '/owner' 前缀 | ||
* 超管用户可以访问的,加 '/admin' 前缀 | ||
*/ | ||
const router = new Router(); | ||
router.prefix('/api'); | ||
router.use(bodyParser()); | ||
router.use(responder); | ||
|
||
router.get('/hello', async function(ctx, next) { | ||
ctx.body = 'Hello World'; | ||
await next(); | ||
}); | ||
|
||
router.use('/user', user.routes()); | ||
|
||
router.use(); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const { errorLogger } = require('../../logger'); | ||
|
||
/** | ||
* the responder middleware | ||
* @param {any} ctx | ||
* @param {function} next | ||
*/ | ||
async function responder(ctx, next) { | ||
try { | ||
await next(); | ||
} catch (e) { | ||
ctx.status = e.status || 500; | ||
ctx.body = e.message || 'Server Error'; | ||
if (e.name !== 'Invariant Violation') { | ||
errorLogger.error(e); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
responder, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const Router = require('koa-router'); | ||
|
||
|
||
const user = new Router(); | ||
|
||
user.get('/testUser', async function(ctx, next) { | ||
ctx.body = 'This is test user'; | ||
await next(); | ||
}); | ||
|
||
module.exports = user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports.init = function() { | ||
const dotenv = require('dotenv'); | ||
dotenv.config({ path: __dirname + '/config/env/.env' }); | ||
|
||
const logger = require('./logger'); | ||
logger.configureLog(); | ||
|
||
const server = require('./server'); | ||
server.configureServer(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
appenders: { | ||
out: { type: 'console' }, | ||
access: { 'type': 'dateFile', 'filename': 'logs/access.log', | ||
'pattern': '-yyyy-MM-dd.log', 'alwaysIncludePattern': true }, | ||
error: { 'type': 'dateFile', 'filename': 'logs/error', | ||
'pattern': '-yyyy-MM-dd.log', 'alwaysIncludePattern': true }, | ||
default: { 'type': 'dateFile', 'filename': 'logs/default', | ||
'pattern': '-yyyy-MM-dd.log', 'alwaysIncludePattern': true }, | ||
}, | ||
categories: { | ||
default: { appenders: ['out', 'default'], level: 'info' }, | ||
access: { appenders: ['access'], level: 'info' }, | ||
error: { appenders: ['error'], level: 'error' }, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const config = require('./config'); | ||
const log4js = require('log4js'); | ||
|
||
/** | ||
* configure log using log4js. | ||
*/ | ||
function configureLog() { | ||
log4js.configure(config); | ||
} | ||
|
||
/** | ||
* get error logger | ||
* @return {Logger} | ||
*/ | ||
const errorLogger = log4js.getLogger('error'); | ||
|
||
module.exports = { | ||
configureLog, | ||
errorLogger, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const Koa = require('koa'); | ||
const https = require('https'); | ||
const http = require('http'); | ||
const fs = require('fs'); | ||
const compress = require('koa-compress'); | ||
const session = require('koa-session'); | ||
const { router } = require('./controller'); | ||
|
||
/** | ||
* Configure server | ||
*/ | ||
function configureServer() { | ||
const cookieSecret = process.env.COOKIE_SECRET; | ||
const sslPassPhrase = process.env.SSL_PASS_PHRASE; | ||
const app = new Koa(); | ||
app.keys = [cookieSecret]; | ||
|
||
app.use(compress()); | ||
app.use(session({ key: cookieSecret }, app)); | ||
|
||
// redirect http to https | ||
app.use(async (ctx, next) => { | ||
const { request, response } = ctx; | ||
if (!request.secure) { | ||
return response.redirect(['https://', request.get('Host'), request.url].join('')); | ||
} | ||
next(); | ||
}); | ||
|
||
const options = { | ||
key: fs.readFileSync(__dirname + '/config/ssl/key.pem'), | ||
cert: fs.readFileSync(__dirname + '/config/ssl/cert.pem'), | ||
passphrase: sslPassPhrase, | ||
}; | ||
|
||
app.use(router.routes()) | ||
|
||
https.createServer(options, app.callback()).listen(443); | ||
http.createServer(app.callback()).listen(80); | ||
} | ||
|
||
module.exports = { | ||
configureServer, | ||
}; | ||
|