From c6ac965fb7d6b5680f32f41db6e9157fe9abad59 Mon Sep 17 00:00:00 2001 From: gumi Date: Sun, 1 Apr 2018 11:00:30 -0400 Subject: use the built-in npm-config system instead of a custom one --- config.json.template | 15 --------------- package.json | 15 +++++++++++++++ server.js | 20 ++++++++++++-------- 3 files changed, 27 insertions(+), 23 deletions(-) delete mode 100644 config.json.template diff --git a/config.json.template b/config.json.template deleted file mode 100644 index 31968fb..0000000 --- a/config.json.template +++ /dev/null @@ -1,15 +0,0 @@ -{ - "port": 8080, - - "sql": { - "host": "localhost", - "user": "db user", - "password": "db password", - "database": "db", - "table": "table" - }, - - "recaptcha": { - "secret": "recaptcha secret key" - } -} diff --git a/package.json b/package.json index 2c2a535..a59d006 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,21 @@ "description": "TMW RESTful API", "author": "The Mana World", "license": "CC0-1.0", + "config": { + "port": 8080, + + "sql": { + "host": "localhost", + "user": "db user", + "password": "db password", + "database": "db", + "table": "table" + }, + + "recaptcha": { + "secret": "recaptcha secret key" + } + }, "repository": { "type": "git", "url": "https://github.com/themanaworld/api.git" diff --git a/server.js b/server.js index 3585be1..9384b2c 100644 --- a/server.js +++ b/server.js @@ -2,7 +2,6 @@ const express = require("express"); const mysql = require("mysql"); const bodyParser = require("body-parser"); const https = require("https"); -const config = require("./config.json"); const api = express(); const checkCaptcha = (req, res, next) => { @@ -17,7 +16,7 @@ const checkCaptcha = (req, res, next) => { return; } - https.get(`https://www.google.com/recaptcha/api/siteverify?secret=${config.recaptcha.secret}&response=${token}`, (re) => { + https.get(`https://www.google.com/recaptcha/api/siteverify?secret=${process.env.npm_package_config_recaptcha_secret}&response=${token}`, (re) => { re.setEncoding("utf8"); re.on("data", response => { const data = JSON.parse(response); @@ -71,10 +70,10 @@ api.post("/api/account", (req, res) => { }; const db = mysql.createConnection({ - host : config.sql.host, - user : config.sql.user, - password : config.sql.password, - database : config.sql.database + host : process.env.npm_package_config_sql_host, + user : process.env.npm_package_config_sql_user, + password : process.env.npm_package_config_sql_password, + database : process.env.npm_package_config_sql_database }); db.connect(err => { @@ -87,7 +86,7 @@ api.post("/api/account", (req, res) => { return; } - db.query({sql: `INSERT INTO ${config.sql.table} (USERNAME, PASSWORD, EMAIL, GENDER) VALUES ("${account.username}", "${account.password}", "${account.email}", "N")`}, (err, rows, fields) => { + db.query({sql: `INSERT INTO ${process.env.npm_package_config_sql_table} (USERNAME, PASSWORD, EMAIL, GENDER) VALUES ("${account.username}", "${account.password}", "${account.email}", "N")`}, (err, rows, fields) => { if (err) { if (err.code == "ER_DUP_ENTRY") { res.status(409).json({ @@ -124,5 +123,10 @@ api.use((req, res, next) => { console.info("a request for an unknown endpoint was received"); }); +if (process.env.npm_package_config_port === undefined) { + console.error("Please run this package with `npm start`"); + process.exit(1); +} + api.set("trust proxy", "loopback"); // only allow localhost to communicate with the API -api.listen(config.port, () => console.info(`Listening on port ${config.port}`)); +api.listen(process.env.npm_package_config_port, () => console.info(`Listening on port ${process.env.npm_package_config_port}`)); -- cgit v1.2.3-70-g09d2