-
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
Liko Zhang
authored and
Liko Zhang
committed
Jul 10, 2021
1 parent
339b8c4
commit 9c511e3
Showing
8 changed files
with
167 additions
and
1 deletion.
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,4 @@ | ||
{ | ||
"presets": ["@babel/env"], | ||
"plugins": ["@babel/plugin-proposal-object-rest-spread"] | ||
} |
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 @@ | ||
module.exports = { | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "airbnb-base", | ||
"parserOptions": { | ||
"ecmaVersion": 2021, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"double" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"no-unused-vars": [ | ||
"error", { "caughtErrors": "all" } | ||
], | ||
"no-param-reassign": 0, | ||
"max-len": 0, | ||
"strict": 0, | ||
"no-console": 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
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,19 @@ | ||
"use strict"; | ||
|
||
import pack from "../package.json"; | ||
|
||
const env = process.env.NODE_ENV || "development"; | ||
|
||
export default { | ||
env, | ||
isDev: env === "development", | ||
|
||
port: "PORT" in process.env ? (Number(process.env.PORT) + ("NODE_APP_INSTANCE" in process.env ? Number(process.env.NODE_APP_INSTANCE) : 0)) : 8000, | ||
host: process.env.HOST || (env === "development" ? "0.0.0.0" : "127.0.0.1"), | ||
|
||
server: { | ||
name: process.env.SERVER_NAME || pack.name, | ||
version: process.env.SERVER_VERSION || pack.version, | ||
}, | ||
|
||
}; |
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,26 @@ | ||
"use strict"; | ||
|
||
import config from "../config"; | ||
|
||
function ok(req, res, next) { | ||
res.status(200).json({ success: true }); | ||
return next(); | ||
} | ||
|
||
function getHealth(req, res, next) { | ||
const info = { | ||
pid: process.pid, | ||
name: config.server.name.toUpperCase(), | ||
version: config.server.version, | ||
port: config.port, | ||
uptimeSec: process.uptime(), | ||
}; | ||
|
||
res.status(200).json(info); | ||
return next(); | ||
} | ||
|
||
export default { | ||
ok, | ||
getHealth, | ||
}; |
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 @@ | ||
"use strict"; | ||
|
||
import express from "express"; | ||
|
||
import systemRouter from "./system"; | ||
|
||
const router = express.Router(); | ||
|
||
router.use("", systemRouter); | ||
|
||
export default 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,11 @@ | ||
"use strict"; | ||
|
||
import express from "express"; | ||
import systemCtrl from "../controllers/system"; | ||
|
||
const router = express.Router(); | ||
|
||
router.route("/ok").get(systemCtrl.ok); | ||
router.route("/health").get(systemCtrl.getHealth); | ||
|
||
export default 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 |
---|---|---|
@@ -1,8 +1,47 @@ | ||
"use strict"; | ||
|
||
import bodyParser from "body-parser"; | ||
import express from "express"; | ||
import addRequestId from "express-request-id"; | ||
|
||
import config from "./config"; | ||
import router from "./routes/index"; | ||
|
||
process.title = config.server.name; | ||
|
||
const server = express(); | ||
|
||
server.use(addRequestId()); | ||
server.use(bodyParser.json()); | ||
server.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
server.use("*", (req, res, next) => { | ||
const data = req.method === "GET" ? req.query : req.body; | ||
|
||
console.log("HTTP", req.method, req.baseUrl, data); | ||
|
||
return next(); | ||
}); | ||
|
||
server.use("", router); | ||
|
||
async function init() { | ||
console.log("Launching node app"); | ||
|
||
const serverInstance = server.listen(config.port, config.host, () => { | ||
console.log(`${config.server.name.toUpperCase()} v${config.server.version} listening at ${config.host}:${config.port}`); | ||
}); | ||
|
||
process.on("SIGINT", () => { | ||
console.log("Stopping server"); | ||
|
||
serverInstance.close(() => { | ||
console.log("Server stopped"); | ||
process.exit(0); | ||
}); | ||
}); | ||
} | ||
|
||
export default { | ||
init, | ||
}; |