Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ihahoo committed Jul 15, 2016
1 parent 4226dbf commit d3129f9
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .babelrc
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"
]
}
14 changes: 14 additions & 0 deletions .editorconfig
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
25 changes: 25 additions & 0 deletions .eslintrc
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
}
}
15 changes: 15 additions & 0 deletions .gitignore
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
51 changes: 51 additions & 0 deletions package.json
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"
}
}
6 changes: 6 additions & 0 deletions src/api/hello.js
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;
9 changes: 9 additions & 0 deletions src/config.js
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;
5 changes: 5 additions & 0 deletions src/routes/hello.js
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);
}
5 changes: 5 additions & 0 deletions src/routes/index.js
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);
}
71 changes: 71 additions & 0 deletions src/server.js
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();

0 comments on commit d3129f9

Please sign in to comment.