env file handling + fix error in libnowplaying

This commit is contained in:
Enstrayed
2025-10-26 00:44:54 -07:00
parent 535e3958cb
commit 44ed0e19d0
4 changed files with 38 additions and 29 deletions

View File

@@ -7,20 +7,24 @@ import cookieParser from 'cookie-parser'
const app = express()
if (!process.env.DATABASE_URL) {
console.log("FATAL: DATABASE_URI must be set")
process.exit(1)
try {
process.loadEnvFile("./.env")
} catch {
console.log("FATAL: DATABASE_URL was not defined or found in env file")
process.exit(1)
}
}
const db = postgres(process.env.DATABASE_URL)
const globalConfig = await db`select content from config where id = ${process.env.CONFIG_OVERRIDE ?? 'production'}`.then(response => {return response[0]["content"]}).catch(error => {
const globalConfig = await db`select content from config where id = ${process.env.CONFIG_OVERRIDE ?? 'production'}`.then(response => { return response[0]["content"] }).catch(error => {
console.log(`FATAL: Error occured in downloading configuration: ${error}`)
process.exit(1)
})
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',' ')
const startTime = new Date(new Date().getTime() - 25200000).toISOString().slice(0, 19).replace('T', ' ')
export { app, fs, db, globalConfig, globalVersion }
@@ -28,7 +32,7 @@ app.use(json()) // Allows receiving JSON bodies
// see important note: https://expressjs.com/en/api.html#express.json
app.use(cookieParser()) // Allows receiving cookies
process.on('SIGTERM', function() {
process.on('SIGTERM', function () {
console.log("Received SIGTERM, exiting...")
process.exit(0)
})
@@ -41,7 +45,7 @@ fs.readdir("./routes", (err, files) => {
let importedRoutes = []
files.forEach(file => {
import(`./routes/${file}`)
importedRoutes.push(file.slice(0,-3))
importedRoutes.push(file.slice(0, -3))
})
process.stdout.write(` | Imported ${importedRoutes} \n`)
}