diff --git a/.env b/.env new file mode 100644 index 00000000..18a79c59 --- /dev/null +++ b/.env @@ -0,0 +1,10 @@ +POSTGRES_USER=Admin +POSTGRES_PASSWORD=QwEr12345. +POSTGRES_DB_1=Default +POSTGRES_DB_2=USERS +POSTGRES_DB_3=QUESTIONS +POSTGRES_DB_4=STATISTICS + +BACKEND_PORT=3000 +FRONTEND_PORT=5173 +DATABASE_PORT=80 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..32b2c4bb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM node:18 AS base +WORKDIR /usr/local/app + +################ FRONTEND BUILD ################# +FROM base AS client-build +WORKDIR /usr/local/app/frontend +COPY SerpentRace_Frontend/package.json SerpentRace_Frontend/package-lock.json ./ +RUN npm ci +COPY SerpentRace_Frontend/ ./ +RUN npm run build + +################ BACKEND BUILD ################# +FROM base AS backend-build +WORKDIR /usr/local/app/backend +COPY SerpentRace_Backend/package.json SerpentRace_Backend/package-lock.json ./ +RUN npm ci +COPY SerpentRace_Backend/ ./ +# Copy frontend build output to backend static directory +COPY --from=client-build /usr/local/app/frontend/dist ./src/static + +################ PRODUCTION IMAGE ################# +FROM backend-build AS prod +WORKDIR /usr/local/app/backend +ENV NODE_ENV=production +RUN npm ci --only=production +EXPOSE 3000 +CMD ["node", "src/index.mjs"] \ No newline at end of file diff --git a/SerpentRace_Backend/src/index.mjs b/SerpentRace_Backend/src/index.mjs index 6e0cb22d..87da00bf 100644 --- a/SerpentRace_Backend/src/index.mjs +++ b/SerpentRace_Backend/src/index.mjs @@ -4,7 +4,11 @@ const app = express(); const PORT = process.env.PORT || 3000; app.use(express.json()) -app.listen(PORT, ()=>{ +app.listen(PORT, '0.0.0.0', ()=>{ console.log(`Running on Port: ${PORT}`) }); +app.get("/api/", (req, res) => { + res.send("KÖRTE"); +}); + diff --git a/SerpentRace_Frontend/README.Docker.md b/SerpentRace_Frontend/README.Docker.md new file mode 100644 index 00000000..8300f21d --- /dev/null +++ b/SerpentRace_Frontend/README.Docker.md @@ -0,0 +1,22 @@ +### Building and running your application + +When you're ready, start your application by running: +`docker compose up --build`. + +Your application will be available at http://localhost:5173. + +### Deploying your application to the cloud + +First, build your image, e.g.: `docker build -t myapp .`. +If your cloud uses a different CPU architecture than your development +machine (e.g., you are on a Mac M1 and your cloud provider is amd64), +you'll want to build the image for that platform, e.g.: +`docker build --platform=linux/amd64 -t myapp .`. + +Then, push it to your registry, e.g. `docker push myregistry.com/myapp`. + +Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/) +docs for more detail on building and pushing. + +### References +* [Docker's Node.js guide](https://docs.docker.com/language/nodejs/) \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..71f58ea1 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,76 @@ +services: + proxy: + image: traefik:v2.11 + command: --providers.docker + ports: + - 80:80 + volumes: + - /var/run/docker.sock:/var/run/docker.sock + + backend: + build: + context: . + dockerfile: Dockerfile + target: prod + environment: + POSTGRES_HOST: db + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB_1} + DB_1: ${POSTGRES_DB_2} + DB_2: ${POSTGRES_DB_3} + DB_3: ${POSTGRES_DB_4} + command: ["npx", "nodemon", "src/index.mjs"] + develop: + watch: + - path: ./SerpentRace_Backend/src + action: sync + target: /usr/local/app/backend/src + - path: ./SerpentRace_Backend/package.json + action: rebuild + labels: + traefik.http.routers.backend.rule: Host(`localhost`) && PathPrefix(`/api`) + traefik.http.services.backend.loadbalancer.server.port: 3000 + depends_on: + - db + + frontend: + build: + context: . + dockerfile: Dockerfile + target: client-build + command: ["npm", "run", "dev", "--", "--host", "0.0.0.0", "--port", "5173"] + working_dir: /usr/local/app/frontend + develop: + watch: + - path: ./SerpentRace_Frontend/src + action: sync + target: /usr/local/app/frontend/src + - path: ./SerpentRace_Frontend/package.json + action: rebuild + labels: + traefik.http.routers.frontend.rule: Host(`localhost`) + traefik.http.services.frontend.loadbalancer.server.port: 5173 + + db: + image: postgres + restart: always + shm_size: 128mb + environment: + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB_1} + DB_1: ${POSTGRES_DB_2} + DB_2: ${POSTGRES_DB_3} + DB_3: ${POSTGRES_DB_4} + volumes: + - ./init-multi-db.sh:/docker-entrypoint-initdb.d/init-multi-db.sh:ro + + adminer: + image: adminer + restart: always + ports: + - "8080:8080" + labels: + traefik.http.routers.adminer.rule: Host(`db.localhost`) + traefik.http.services.adminer.loadbalancer.server.port: 8080 \ No newline at end of file diff --git a/init-multi-db.sh b/init-multi-db.sh new file mode 100644 index 00000000..a4be45ab --- /dev/null +++ b/init-multi-db.sh @@ -0,0 +1,6 @@ +set -e +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE DATABASE "${DB_1}"; + CREATE DATABASE "${DB_2}"; + CREATE DATABASE "${DB_3}"; +EOSQL \ No newline at end of file diff --git a/run.bat b/run.bat new file mode 100644 index 00000000..f9400ff2 --- /dev/null +++ b/run.bat @@ -0,0 +1 @@ +docker compose watch \ No newline at end of file diff --git a/stop.bat b/stop.bat new file mode 100644 index 00000000..345e7ea1 --- /dev/null +++ b/stop.bat @@ -0,0 +1 @@ +docker compose stop \ No newline at end of file