Docker improvements

This commit is contained in:
Enstrayed
2024-04-11 01:26:05 -07:00
parent 43bc49cc2d
commit 249f88a6c7
6 changed files with 29 additions and 16 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules/
config.json
GITVERSION

View File

@@ -1,6 +1,9 @@
FROM node:20
WORKDIR /app
EXPOSE 8127
CMD [ "bash", "init.sh" ]
# This is just copied from urlshortener cause both apps have similar architecture
RUN git clone https://github.com/enstrayed/enstrayedapi .
RUN git config --global --add safe.directory /app
RUN git show --oneline -s >> GITVERSION
RUN npm install
ENTRYPOINT [ "node", "index.js" ]

View File

@@ -1,8 +1,7 @@
{
"startup": {
"apiPort": 8081,
"routesDir": "./routes",
"documentationUrl": "https://api.enstrayed.com"
"routesDir": "./routes"
},
"couchdb": {

View File

@@ -1,5 +1,4 @@
version: '3.0'
---
services:
enstrayedapi:
build:
@@ -8,4 +7,4 @@ services:
container_name: enstrayedapi
restart: unless-stopped
volumes:
- .:/app
- ./config.json:/app/config.json

View File

@@ -2,13 +2,28 @@ const fs = require('fs'); // Filesystem Access
const express = require('express');
const app = express(); // Init Express
const globalConfig = JSON.parse(fs.readFileSync('config.json', 'utf-8')) // Read config file
function criticalFileLoader(file) {
try {
return fs.readFileSync(file, 'utf-8')
} catch {
console.error(`FATAL: Failed to load ${file}`)
process.exit(1)
}
}
const globalConfig = JSON.parse(criticalFileLoader('config.json'))
const globalVersion = criticalFileLoader('GITVERSION').split(" ")
module.exports = { app, globalConfig, fs } // Export express app and fs objects and globalconfig
app.use(express.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)
})
// Import Routes
fs.readdir(globalConfig.startup.routesDir, (err, files) => {
if (err) {
@@ -25,8 +40,8 @@ fs.readdir(globalConfig.startup.routesDir, (err, files) => {
})
app.get("/", (rreq,rres) => {
rres.redirect(globalConfig.startup.documentationUrl)
rres.send(`Enstrayed API | Version: ${globalVersion} | Documentation: <a href="https://etyd.cc/apidocs">etyd.cc/apidocs</a>`)
})
console.log(`Started on ${globalConfig.startup.apiPort}`)
console.log(`Enstrayed API | Version: ${globalVersion} | Port: ${globalConfig.startup.apiPort}`)
app.listen(globalConfig.startup.apiPort)

View File

@@ -1,4 +0,0 @@
#! /bin/bash
npm install
node index.js