-
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
Showing
10 changed files
with
211 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,10 @@ | ||
{ | ||
"plugins": [ | ||
"transform-strict-mode", | ||
"transform-es2015-modules-commonjs", | ||
"transform-es2015-spread", | ||
"transform-es2015-destructuring", | ||
"transform-es2015-parameters", | ||
"transform-async-to-generator" | ||
] | ||
} |
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,14 @@ | ||
# http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 2 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
max_line_length = 0 | ||
trim_trailing_whitespace = false |
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,25 @@ | ||
{ | ||
"extends": "airbnb-base", | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"parser": "babel-eslint", | ||
"plugins": ["import", "babel"], | ||
"rules": { | ||
"comma-dangle": ["warn", "only-multiline"], | ||
"indent": [2, 2, {"SwitchCase": 1}], | ||
"no-console": 0, | ||
"generator-star-spacing": 0, | ||
"babel/generator-star-spacing": 0, | ||
"babel/array-bracket-spacing": 1, | ||
"babel/object-shorthand": 1, | ||
"babel/arrow-parens": 1, | ||
"babel/no-await-in-loop": 1, | ||
"babel/flow-object-type": 1, | ||
"babel/func-params-comma-dangle": 1, | ||
}, | ||
"globals": { | ||
"expect": true | ||
} | ||
} |
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,15 @@ | ||
logs | ||
*.log | ||
npm-debug.log* | ||
node_modules | ||
.npm | ||
coverage | ||
build | ||
build/Release | ||
.module-cache | ||
.DS_Store | ||
.idea | ||
*.iml | ||
.vscode | ||
|
||
lib |
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,51 @@ | ||
{ | ||
"name": "api-server-restify", | ||
"version": "0.1.0", | ||
"description": "A Node.js RESTful API server with restify", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "cross-env NODE_ENV=production node lib/server", | ||
"build": "babel src --out-dir lib", | ||
"dev": "cross-env NODE_ENV=development babel-node src/server", | ||
"clean": "rimraf lib", | ||
"clean:logs": "rimraf logs" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hahoocn/api-server-restify.git" | ||
}, | ||
"keywords": [ | ||
"RESTful", | ||
"API", | ||
"restify" | ||
], | ||
"author": "Ray Guo", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/hahoocn/api-server-restify/issues" | ||
}, | ||
"homepage": "https://github.com/hahoocn/api-server-restify#readme", | ||
"dependencies": { | ||
"babel-polyfill": "^6.9.1", | ||
"bunyan": "^1.8.1", | ||
"restify": "^4.1.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.10.1", | ||
"babel-core": "^6.10.4", | ||
"babel-eslint": "^6.1.2", | ||
"babel-plugin-transform-async-to-generator": "^6.8.0", | ||
"babel-plugin-transform-es2015-destructuring": "^6.9.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.10.3", | ||
"babel-plugin-transform-es2015-parameters": "^6.11.3", | ||
"babel-plugin-transform-es2015-spread": "^6.8.0", | ||
"babel-plugin-transform-strict-mode": "^6.11.3", | ||
"cross-env": "^2.0.0", | ||
"eslint": "^3.0.1", | ||
"eslint-config-airbnb-base": "^4.0.2", | ||
"eslint-plugin-babel": "^3.3.0", | ||
"eslint-plugin-import": "^1.10.3", | ||
"rimraf": "^2.5.3" | ||
} | ||
} |
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,6 @@ | ||
function hello(req, res, next) { | ||
res.send({ msg: 'hello world!' }); | ||
next(); | ||
} | ||
|
||
export default hello; |
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,9 @@ | ||
import path from 'path'; | ||
|
||
const config = { | ||
name: 'MyApp', | ||
port: process.env.PORT || 3030, | ||
logPath: path.resolve(__dirname, '../logs') | ||
}; | ||
|
||
export default config; |
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,5 @@ | ||
import hello from '../api/hello'; | ||
|
||
export default function helloRoute(server) { | ||
server.get('/hello', hello); | ||
} |
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,5 @@ | ||
import hello from './hello'; | ||
|
||
export default function routes(server) { | ||
hello(server); | ||
} |
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,71 @@ | ||
import 'babel-polyfill'; | ||
import fs from 'fs'; | ||
import restify from 'restify'; | ||
import bunyan from 'bunyan'; | ||
import config from './config'; | ||
import routes from './routes'; | ||
|
||
function checkLogDirectory() { | ||
return new Promise((resolve, reject) => { | ||
fs.stat(config.logPath, (err, stats) => { | ||
if (err || !stats.isDirectory()) { | ||
fs.mkdir(config.logPath, (error) => { | ||
if (error) { | ||
reject(new Error(error)); | ||
} | ||
}); | ||
} | ||
|
||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
function createLogger() { | ||
const log = bunyan.createLogger({ | ||
name: config.name, | ||
streams: [{ | ||
level: 'warn', | ||
path: `logs/${config.name}_warn.log` | ||
}, { | ||
level: 'error', | ||
path: `logs/${config.name}_error.log` | ||
}, { | ||
level: 'fatal', | ||
path: `logs/${config.name}_fatal.log` | ||
}, { | ||
level: 'info', | ||
path: `logs/${config.name}_info.log` | ||
}] | ||
}); | ||
return Promise.resolve(log); | ||
} | ||
|
||
function createServer(log) { | ||
const server = restify.createServer({ | ||
log, | ||
name: config.name | ||
}); | ||
|
||
server.use(restify.queryParser()); | ||
server.use(restify.bodyParser({ | ||
mapParams: false | ||
})); | ||
/* eslint new-cap: 0 */ | ||
server.use(restify.CORS()); | ||
|
||
routes(server); | ||
|
||
server.listen(config.port, () => { | ||
log.info(`${server.name} start listening at ${server.url}`); | ||
console.info('==> 🚀 %s listening at %s', server.name, server.url); | ||
}); | ||
} | ||
|
||
async function startServer() { | ||
await checkLogDirectory(); | ||
const log = await createLogger(); | ||
createServer(log); | ||
} | ||
|
||
startServer(); |