Refactored env settings.

This commit is contained in:
BurnyLlama 2021-11-01 22:15:13 +01:00
parent bb0e889934
commit c92d71c66f
6 changed files with 29 additions and 21 deletions

4
.gitignore vendored
View File

@ -119,7 +119,5 @@ dist
package-lock.json package-lock.json
pnpm-lock.yaml pnpm-lock.yaml
*.sql*
players.msgpack
.env .env
ddnss/ data/*

View File

@ -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

View File

@ -1,15 +1,14 @@
import express from 'express' import express from 'express'
import dotenv from 'dotenv' import { config as loadEnv } from 'dotenv'
import api from './api/api.js' import api from './api/api.js'
import { generateDB } from './libs/database/generate.js' import { generateDB } from './libs/database/generate.js'
import { dbInit } from './libs/database/init.js' import { dbInit } from './libs/database/init.js'
dotenv.config() loadEnv()
dbInit() dbInit()
generateDB() generateDB()
const Server = express() const Server = express()
Server.use('/api', api) 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}.`))

View File

@ -15,8 +15,8 @@ export function dbInit() {
log("Starting up databases...") log("Starting up databases...")
/* load in db using better-sqlite3 */ /* load in db using better-sqlite3 */
sqlite = new Database('ddnet.sqlite', { verbose: console.log }); sqlite = new Database(process.env.DDNET_SQLITE_PATH ?? 'data/ddnet.sqlite', { verbose: console.log });
skinDB = new Database('skindata.sqlite', { }); skinDB = new Database(process.env.DDNSS_SQLITE_PATH ?? 'data/skindata.sqlite', { });
/* WAL mode */ /* WAL mode */
sqlite.pragma('journal_mode = WAL'); sqlite.pragma('journal_mode = WAL');

View File

@ -9,7 +9,7 @@ import { sqlite } from './init.js'
* @module db/decodeMsgpack * @module db/decodeMsgpack
*/ */
export function 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 decoded = msgpack.decodeMulti(data, { wrap: true })
const order = ['categories', 'maps', 'totalPoints', 'pointsRanks', 'pointsThisWeek', 'pointsThisMonth', 'teamRankPoints', 'rankPoints', 'serverRanks'] const order = ['categories', 'maps', 'totalPoints', 'pointsRanks', 'pointsThisWeek', 'pointsThisMonth', 'teamRankPoints', 'rankPoints', 'serverRanks']
let final = {} let final = {}

22
template.env Normal file
View File

@ -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