ddstats-server/index.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-10-06 20:36:20 +02:00
/**
* This is the entrypoint for 'DDStats'.
* DDStats is a custom API that servers statistics from the DDRace Network-
* @author BurnyLlama <xmpp:burnyllama@qwik.space>
*/
2021-09-25 12:11:27 +02:00
import express from 'express'
2021-09-25 17:47:05 +02:00
import dotenv from 'dotenv'
2021-10-30 21:39:21 +02:00
import njk from 'nunjucks'
import initLog from './libs/utils/log.js'
2021-10-30 21:39:21 +02:00
import routes from './routes/routes.js'
2021-11-04 19:47:40 +01:00
import { generateDB } from './libs/database/generate.js'
import { dbInit } from './libs/database/init.js'
2021-11-05 21:04:53 +01:00
import { getStats, setStat } from './libs/serverStats.js'
2021-11-04 19:47:40 +01:00
import { downloadEssentialData } from './libs/download/dowload.js'
2021-10-30 21:39:21 +02:00
2021-11-04 23:04:42 +01:00
const start = Date.now()
2021-09-29 20:29:06 +02:00
const log = initLog("[ MAIN ]")
2021-09-25 17:47:05 +02:00
// Read the .env file
dotenv.config()
2021-09-25 12:11:27 +02:00
2021-11-05 21:04:53 +01:00
setStat("startup", start)
setStat("lastDBUpdate", start)
2021-11-04 20:06:51 +01:00
if (process.env.DOWNLOAD_FILES === "enabled")
2021-11-04 19:47:40 +01:00
await downloadEssentialData()
dbInit()
2021-11-04 19:47:40 +01:00
generateDB()
2021-10-30 21:39:21 +02:00
const Server = express()
njk.configure(
'views',
{
autoescape: true,
express: Server,
lstripBlocks: true,
trimBlocks: true
}
)
Server.use('/', routes)
Server.use('/api', routes)
2021-10-30 21:39:21 +02:00
Server.use('/assets', express.static('static'))
2021-11-04 23:04:42 +01:00
const elapsed = Date.now() - start
log(`Took ${elapsed/1000} seconds to start the server!`)
2021-11-13 02:34:52 +01:00
Server.listen(process.env.PORT ?? 12345, () => log(`Server started and listening on port ${process.env.PORT}.`))