diff --git a/.gitignore b/.gitignore index cf7c602..664c5e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -config.json \ No newline at end of file +config.json +GITVERSION \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5f9f46d..fbcad3c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/config.example.json b/config.example.json index af5faa9..2192d6e 100644 --- a/config.example.json +++ b/config.example.json @@ -1,8 +1,7 @@ { "startup": { "apiPort": 8081, - "routesDir": "./routes", - "documentationUrl": "https://api.enstrayed.com" + "routesDir": "./routes" }, "couchdb": { diff --git a/docker-compose.yml b/docker-compose.yml index 4e25782..4f58d54 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,4 @@ -version: '3.0' - +--- services: enstrayedapi: build: @@ -8,4 +7,4 @@ services: container_name: enstrayedapi restart: unless-stopped volumes: - - .:/app \ No newline at end of file + - ./config.json:/app/config.json \ No newline at end of file diff --git a/index.js b/index.js index 0e2b2e4..b5c007e 100644 --- a/index.js +++ b/index.js @@ -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: etyd.cc/apidocs`) }) -console.log(`Started on ${globalConfig.startup.apiPort}`) +console.log(`Enstrayed API | Version: ${globalVersion} | Port: ${globalConfig.startup.apiPort}`) app.listen(globalConfig.startup.apiPort) \ No newline at end of file diff --git a/init.sh b/init.sh deleted file mode 100644 index c6a65d2..0000000 --- a/init.sh +++ /dev/null @@ -1,4 +0,0 @@ -#! /bin/bash - -npm install -node index.js \ No newline at end of file