Docker improvements
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
config.json
|
config.json
|
||||||
|
GITVERSION
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
FROM node:20
|
FROM node:20
|
||||||
WORKDIR /app
|
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" ]
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
{
|
{
|
||||||
"startup": {
|
"startup": {
|
||||||
"apiPort": 8081,
|
"apiPort": 8081,
|
||||||
"routesDir": "./routes",
|
"routesDir": "./routes"
|
||||||
"documentationUrl": "https://api.enstrayed.com"
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"couchdb": {
|
"couchdb": {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
version: '3.0'
|
---
|
||||||
|
|
||||||
services:
|
services:
|
||||||
enstrayedapi:
|
enstrayedapi:
|
||||||
build:
|
build:
|
||||||
@@ -8,4 +7,4 @@ services:
|
|||||||
container_name: enstrayedapi
|
container_name: enstrayedapi
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
volumes:
|
volumes:
|
||||||
- .:/app
|
- ./config.json:/app/config.json
|
||||||
21
index.js
21
index.js
@@ -2,13 +2,28 @@ const fs = require('fs'); // Filesystem Access
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express(); // Init 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
|
module.exports = { app, globalConfig, fs } // Export express app and fs objects and globalconfig
|
||||||
|
|
||||||
app.use(express.json()) // Allows receiving JSON bodies
|
app.use(express.json()) // Allows receiving JSON bodies
|
||||||
// see important note: https://expressjs.com/en/api.html#express.json
|
// 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
|
// Import Routes
|
||||||
fs.readdir(globalConfig.startup.routesDir, (err, files) => {
|
fs.readdir(globalConfig.startup.routesDir, (err, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -25,8 +40,8 @@ fs.readdir(globalConfig.startup.routesDir, (err, files) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.get("/", (rreq,rres) => {
|
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)
|
app.listen(globalConfig.startup.apiPort)
|
||||||
Reference in New Issue
Block a user