diff --git a/.gitignore b/.gitignore index 7603cfd..2cbaf56 100644 --- a/.gitignore +++ b/.gitignore @@ -119,7 +119,5 @@ dist package-lock.json pnpm-lock.yaml -*.sql* -players.msgpack .env -ddnss/ \ No newline at end of file +data/* \ No newline at end of file diff --git a/dotenv-template b/dotenv-template deleted file mode 100644 index 140d847..0000000 --- a/dotenv-template +++ /dev/null @@ -1,11 +0,0 @@ -# The database connect string -MONGO_URI = "mongodb://user:passwd@host/db" - -# The port on which the server starts... -PORT = 12345 - -# Should the server try to generate the database? -GENERATE_DB = "false" - -# The API paginates. How many entries per page? -ENTRIES_PER_PAGE = 50 \ No newline at end of file diff --git a/index.js b/index.js index 5efdcc6..ddfbd09 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,14 @@ import express from 'express' -import dotenv from 'dotenv' +import { config as loadEnv } from 'dotenv' import api from './api/api.js' import { generateDB } from './libs/database/generate.js' import { dbInit } from './libs/database/init.js' -dotenv.config() - +loadEnv() dbInit() generateDB() const Server = express() Server.use('/api', api) -Server.listen(process.env.PORT, () => console.log(`Server started and listening on port ${process.env.PORT}.`)) +Server.listen(process.env.PORT ?? 12345, () => console.log(`Server started and listening on port ${process.env.PORT ?? 12345}.`)) diff --git a/libs/database/init.js b/libs/database/init.js index 4498804..281f7be 100644 --- a/libs/database/init.js +++ b/libs/database/init.js @@ -15,8 +15,8 @@ export function dbInit() { log("Starting up databases...") /* load in db using better-sqlite3 */ - sqlite = new Database('ddnet.sqlite', { verbose: console.log }); - skinDB = new Database('skindata.sqlite', { }); + sqlite = new Database(process.env.DDNET_SQLITE_PATH ?? 'data/ddnet.sqlite', { verbose: console.log }); + skinDB = new Database(process.env.DDNSS_SQLITE_PATH ?? 'data/skindata.sqlite', { }); /* WAL mode */ sqlite.pragma('journal_mode = WAL'); diff --git a/libs/database/tasks.js b/libs/database/tasks.js index 265bd50..93c69f6 100644 --- a/libs/database/tasks.js +++ b/libs/database/tasks.js @@ -9,7 +9,7 @@ import { sqlite } from './init.js' * @module db/decodeMsgpack */ export function decodeMsgpack() { - const data = fs.readFileSync('players.msgpack') + const data = fs.readFileSync(process.env.MSGPACK_PATH ?? 'data/players.msgpack') const decoded = msgpack.decodeMulti(data, { wrap: true }) const order = ['categories', 'maps', 'totalPoints', 'pointsRanks', 'pointsThisWeek', 'pointsThisMonth', 'teamRankPoints', 'rankPoints', 'serverRanks'] let final = {} diff --git a/template.env b/template.env new file mode 100644 index 0000000..a6a9cf1 --- /dev/null +++ b/template.env @@ -0,0 +1,22 @@ +# +# You should copy this file to '.env' +# and set all settings there. +# + + +# MongoDB connection URI +MONGO_URI = "mongodb://user:passwd@host/db" + +# Paths to SQLite databases... +DDNET_SQLITE_PATH = "data/ddnet.sqlite" +DDNSS_SQLITE_PATH = "data/skindata.sqlite" +MSGPACK_PATH = "data/players.msgpack" + +# Should the server try to generate the database? +GENERATE_DB = "true" + +# The port on which the server listens... +PORT = 12345 + +# The API paginates. How many entries per page? +ENTRIES_PER_PAGE = 50 \ No newline at end of file