diff --git a/server/.env.template b/server/.env.template new file mode 100644 index 0000000000..6c1e8b8e3b --- /dev/null +++ b/server/.env.template @@ -0,0 +1,25 @@ +################## +# BACKEND CONFIG # +################## + +# !NOTE: This is a template. Make a copy named .env and fill in the relevant fields. + +# COMMON +# --------------- +ENV=PROD + + +# USER SERVICE +# --------------- +PORT_USER=3001 +# MongoDB +DB_URI_USER='' +# JWT secret +JWT_SECRET=you-can-replace-this-with-your-own-secret + + +# QUESTION SERVICE +# --------------- +PORT_QUESTION=3002 +# MongoDB +DB_URI_QUESTION='' diff --git a/server/README.md b/server/README.md index a4032e56d4..f0deff4ecd 100644 --- a/server/README.md +++ b/server/README.md @@ -1,6 +1,14 @@ -# Backend +# Back-end -The back-end has been split into multiple containerized microservices. You can run all of them at once using `docker compose`: +The back-end has been split into multiple containerized microservices. + +## Setup + +1. Make a copy of `.env.template` called `.env` and fill in the variables (or add a pre-filled env file) + +## Running + +You can run all the services using `docker compose`: ```sh docker compose build diff --git a/server/docker-compose.yml b/server/docker-compose.yml index cc8cce11b0..be82aba51a 100644 --- a/server/docker-compose.yml +++ b/server/docker-compose.yml @@ -1,10 +1,26 @@ services: user: - build: ./user-service + build: + context: ./user-service + dockerfile: Dockerfile + args: + - PORT=${PORT_USER} ports: - - 3001:3001 + - ${PORT_USER}:${PORT_USER} + environment: + - ENV + - DB_CLOUD_URI=${DB_URI_USER} + - DB_LOCAL_URI=${DB_URI_USER} + - JWT_SECRET questions: - build: ./question-service + build: + context: ./question-service + dockerfile: Dockerfile + args: + - PORT=${PORT_QUESTION} ports: - - 3002:3002 \ No newline at end of file + - ${PORT_QUESTION}:${PORT_QUESTION} + environment: + - ENV + - DB_CLOUD_URI=${DB_URI_QUESTION} diff --git a/server/question-service/.env.template b/server/question-service/.env.template deleted file mode 100644 index d802eab5da..0000000000 --- a/server/question-service/.env.template +++ /dev/null @@ -1,11 +0,0 @@ -# !NOTE: This is a template. Make a copy without the .template suffix in the name and fill in the relevant fields. -# QUESTION SERVICE - -# Port that the service will run on -PORT=3002 - -# MongoDB -# ENV=PROD: Use cloud MongoDB Atlas database -ENV=PROD -DB_CLOUD_URI= -DB_LOCAL_URI= diff --git a/server/question-service/Dockerfile b/server/question-service/Dockerfile index 6632391bdb..5b2ab6d8e0 100644 --- a/server/question-service/Dockerfile +++ b/server/question-service/Dockerfile @@ -1,8 +1,8 @@ -# syntax=docker/dockerfile:1 - FROM node:18-alpine +ARG PORT +ENV PORT=$PORT WORKDIR /app COPY . . RUN npm install --omit=dev CMD ["npm", "start"] -EXPOSE 3002 +EXPOSE $PORT diff --git a/server/user-service/.env.template b/server/user-service/.env.template deleted file mode 100644 index 3996b2cf54..0000000000 --- a/server/user-service/.env.template +++ /dev/null @@ -1,14 +0,0 @@ -# !NOTE: This is a template. Make a copy named .env and fill in the relevant fields. -# USER SERVICE - -# Port that the service will run on -PORT=3002 - -# MongoDB -# ENV=PROD: Use cloud MongoDB Atlas database -ENV=PROD -DB_CLOUD_URI= -DB_LOCAL_URI= - -# Secret for creating JWT signature -JWT_SECRET= \ No newline at end of file diff --git a/server/user-service/Dockerfile b/server/user-service/Dockerfile index a9f11460f3..5b2ab6d8e0 100644 --- a/server/user-service/Dockerfile +++ b/server/user-service/Dockerfile @@ -1,8 +1,8 @@ -# syntax=docker/dockerfile:1 - FROM node:18-alpine +ARG PORT +ENV PORT=$PORT WORKDIR /app COPY . . RUN npm install --omit=dev CMD ["npm", "start"] -EXPOSE 3001 +EXPOSE $PORT