-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (43 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as fs from 'fs'
import { execSync } from 'child_process'
import express, { json } from 'express'
const app = express()
if (!process.env.API_DBHOST || !process.env.API_DBCRED) {
console.log("FATAL: API_DBHOST and API_DBCRED must be set")
process.exit(1)
}
const globalConfig = await fetch(`${process.env.API_DBHOST}/config/${process.env.API_DBCRED.split(":")[0]}`,{
headers: { "Authorization": `Basic ${btoa(process.env.API_DBCRED)}`}
}).then(response => {
if (response.status !== 200) {
console.log(`FATAL: Failed to download configuration: ${response.status} ${response.statusText}`)
process.exit(1)
} else {
return response.json()
}
})
const globalVersion = execSync(`git show --oneline -s`).toString().split(" ")[0]
// Returns ISO 8601 Date & 24hr time for UTC-7/PDT
const startTime = new Date(new Date().getTime() - 25200000).toISOString().slice(0,19).replace('T',' ')
export { app, fs, globalConfig, globalVersion }
app.use(json()) // Allows receiving JSON bodies
// see important note: https://expressjs.com/en/api.html#express.json
process.on('SIGTERM', function() {
console.log("Received SIGTERM, exiting...")
process.exit(0)
})
fs.readdir("./routes", (err, files) => {
if (err) {
console.log(`FATAL: Unable to import routes: ${err}`)
process.exit(1)
} else {
let importedRoutes = []
files.forEach(file => {
import(`./routes/${file}`)
importedRoutes.push(file.slice(0,-3))
})
process.stdout.write(` | Imported ${importedRoutes} \n`)
}
})
process.stdout.write(`>>> EnstrayedAPI ${globalVersion} | Started ${startTime} on ${process.env.API_PORT ?? 8081}`)
app.listen(process.env.API_PORT ?? 8081)