everything dockerized

This commit is contained in:
j-weissen 2022-12-06 09:45:31 +01:00
parent f0b379a379
commit 529da45219
6 changed files with 62 additions and 14 deletions

7
.env.example Normal file
View file

@ -0,0 +1,7 @@
POSTGRES_PORT=3000
EXPRESS_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=rr

View file

@ -1,9 +1,11 @@
FROM node:18 FROM node:18
COPY ./* /app/ COPY . /app
WORKDIR /app WORKDIR /app
RUN npm install RUN npm install
EXPOSE 3000
ENTRYPOINT ["npm", "run"] ENTRYPOINT ["npm", "run"]
CMD ["prod"] CMD ["prod"]

View file

@ -19,6 +19,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.14", "@types/express": "^4.17.14",
"@types/morgan": "^1.9.3",
"@types/node": "^18.11.9" "@types/node": "^18.11.9"
} }
}, },
@ -123,6 +124,15 @@
"integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==", "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==",
"dev": true "dev": true
}, },
"node_modules/@types/morgan": {
"version": "1.9.3",
"resolved": "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.3.tgz",
"integrity": "sha512-BiLcfVqGBZCyNCnCH3F4o2GmDLrpy0HeBVnNlyZG4fo88ZiE9SoiBe3C+2ezuwbjlEyT+PDZ17//TAlRxAn75Q==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "18.11.9", "version": "18.11.9",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz",
@ -1166,6 +1176,15 @@
"integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==", "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==",
"dev": true "dev": true
}, },
"@types/morgan": {
"version": "1.9.3",
"resolved": "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.3.tgz",
"integrity": "sha512-BiLcfVqGBZCyNCnCH3F4o2GmDLrpy0HeBVnNlyZG4fo88ZiE9SoiBe3C+2ezuwbjlEyT+PDZ17//TAlRxAn75Q==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/node": { "@types/node": {
"version": "18.11.9", "version": "18.11.9",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz",

View file

@ -6,7 +6,7 @@
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"dev": "ts-node-esm src/app.ts", "dev": "ts-node-esm src/app.ts",
"prod": "tsc && node build/app.ts" "prod": "tsc && node build/app.js"
}, },
"author": "jweissen", "author": "jweissen",
"license": "ISC", "license": "ISC",
@ -21,6 +21,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.14", "@types/express": "^4.17.14",
"@types/morgan": "^1.9.3",
"@types/node": "^18.11.9" "@types/node": "^18.11.9"
} }
} }

View file

@ -16,16 +16,29 @@ app.use(morganFormatted);
// init database connection // init database connection
const pgp = pgPromise({}); const pgp = pgPromise({});
const db = pgp('postgres://postgres:postgres@localhost:5432/rr') const db = pgp('postgres://postgres:postgres@db:5432/rr')
app.get('/helloworld', (req, res) => {
res.json({message: "Hello World!"})
})
app.get('/highscore', async (req, res) => { app.get('/highscore', async (req, res) => {
let data = await db.any( let data = await dbQueryCatcher(async () =>
'SELECT * FROM lb_highscore;' await db.manyOrNone('SELECT * FROM lb_highscore;')
) )
res.send(data) res.json(data)
}) })
async function dbQueryCatcher(request): Promise<any> {
let data;
try {
data = await request();
} catch (e) {
console.log((e as Error).message)
}
return data;
}
app.listen(port, () => { app.listen(port, () => {
morganFormatted.log(`Server started at http://localhost:3000`); console.log(`Server started at http://localhost:3000`);
}) })

View file

@ -5,11 +5,17 @@ services:
build: backend/db build: backend/db
container_name: postgres-db container_name: postgres-db
environment: environment:
POSTGRES_DB: rr POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: postgres POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: postgres POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
ports: ports:
- "5432:5432" - "${POSTGRES_PORT}:5432"
volumes: volumes:
- ./backend/pgdata:/var/lib/postgresql/data - ./backend/pgdata:/var/lib/postgresql/data
api:
build: backend/api
container_name: express-api
ports:
- "${EXPRESS_PORT}:3000"