Skip to content

Commit

Permalink
environment set up
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanchenxi95 committed Nov 17, 2017
0 parents commit 04bbb2f
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .eslintrc.js
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
}
};
37 changes: 37 additions & 0 deletions .gitignore
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
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

require('./src/index').init();
36 changes: 36 additions & 0 deletions package.json
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"
}
}
3 changes: 3 additions & 0 deletions src/config/env/.env_example
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"
1 change: 1 addition & 0 deletions src/config/env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
1 change: 1 addition & 0 deletions src/config/ssl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pem
1 change: 1 addition & 0 deletions src/controller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.router = require('./routers');
28 changes: 28 additions & 0 deletions src/controller/routers/index.js
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;
22 changes: 22 additions & 0 deletions src/controller/routers/middlewares.js
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,
};
11 changes: 11 additions & 0 deletions src/controller/routers/user.js
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;
10 changes: 10 additions & 0 deletions src/index.js
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();
};
16 changes: 16 additions & 0 deletions src/logger/config.js
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' },
},
};
20 changes: 20 additions & 0 deletions src/logger/index.js
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,
};
45 changes: 45 additions & 0 deletions src/server.js
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,
};

0 comments on commit 04bbb2f

Please sign in to comment.