diff options
author | gumi <git@gumi.ca> | 2018-04-01 11:00:30 -0400 |
---|---|---|
committer | gumi <git@gumi.ca> | 2018-04-01 11:00:30 -0400 |
commit | c6ac965fb7d6b5680f32f41db6e9157fe9abad59 (patch) | |
tree | 9563b7c01c714badab7bb381070c5a9c8d11e0fb | |
parent | 4263446107b856aad27232713c2f88e398c78a7f (diff) | |
download | apiv1-c6ac965fb7d6b5680f32f41db6e9157fe9abad59.tar.gz apiv1-c6ac965fb7d6b5680f32f41db6e9157fe9abad59.tar.bz2 apiv1-c6ac965fb7d6b5680f32f41db6e9157fe9abad59.tar.xz apiv1-c6ac965fb7d6b5680f32f41db6e9157fe9abad59.zip |
use the built-in npm-config system instead of a custom one
-rw-r--r-- | config.json.template | 15 | ||||
-rw-r--r-- | package.json | 15 | ||||
-rw-r--r-- | server.js | 20 |
3 files changed, 27 insertions, 23 deletions
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" @@ -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}`)); |