From 59ee24b046de8c961bb01685bef48044ff7f27de Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 15 Jul 2025 15:44:15 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20dev.js=20->=20start.js=20and=20working=20user=20api=20docker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 3 ++ docker/docker-compose.yml | 14 ++++++-- docker/user-api/Dockerfile | 6 ++++ src/dev.js => docker/user-api/start.js | 0 src/start.js | 46 ++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 2 deletions(-) rename src/dev.js => docker/user-api/start.js (100%) create mode 100644 src/start.js diff --git a/.dockerignore b/.dockerignore index 1c34354..a5f1a53 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,3 +8,6 @@ node_modules/ # useless files in the docker *.md docker/ + +# PLEASE NO +.env diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 80d466d..1cbef9e 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -6,5 +6,15 @@ services: # environment: # - euuuh user-api: - image: - cmd: + build: + dockerfile: docker/user-api/Dockerfile + context: .. + networks: + - transcendence + ports: + - 3000:3000 + +networks: + transcendence: + external: false + name: transcendence diff --git a/docker/user-api/Dockerfile b/docker/user-api/Dockerfile index e69de29..857aff4 100644 --- a/docker/user-api/Dockerfile +++ b/docker/user-api/Dockerfile @@ -0,0 +1,6 @@ +FROM node-base + +EXPOSE 3000 + +ENV API_TARGET="user" +CMD [ "node", "/app/src/start.js" ] diff --git a/src/dev.js b/docker/user-api/start.js similarity index 100% rename from src/dev.js rename to docker/user-api/start.js diff --git a/src/start.js b/src/start.js new file mode 100644 index 0000000..ae048c9 --- /dev/null +++ b/src/start.js @@ -0,0 +1,46 @@ +import Fastify from 'fastify'; +import authApi from './api/auth/default.js'; +import userApi from './api/user/default.js'; + +const loggerOption = { + transport: { + target: 'pino-pretty', + options: { + colorize: true, + translateTime: 'HH:MM:ss', + ignore: 'pid,hostname' + } + } +}; + +async function start() { + const target = process.env.API_TARGET || 'all'; + + if (target === 'auth' || target === 'all') { + const auth = Fastify({ logger: loggerOption }); + auth.register(authApi); + if (target !== 'all') { + await auth.listen({ port: 3000, host: '0.0.0.0' }); + console.log('Auth API listening on http://0.0.0.0:3000'); + } + else { + await auth.listen({ port: 3001, host: '127.0.0.1'}); + console.log('Auth API listening on http://localhost:3001'); + } + } + + if (target === 'user' || target === 'all') { + const user = Fastify({ logger: loggerOption }); + user.register(userApi); + if (target !== 'all') { + await user.listen({ port: 3000, host: '0.0.0.0' }); + console.log('User API listening on http://0.0.0.0:3000'); + } + else { + await user.listen({ port: 3002, host: '127.0.0.1'}); + console.log('User API listening on http://localhost:3002'); + } + } +} + +start().catch(console.error);