diff --git a/Dockerfile b/Dockerfile index c5144b6..e79580c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,12 +22,3 @@ RUN npm install # # start app CMD ["node", "index.js"] - -# production environment -FROM nginx:stable-alpine as prod - -COPY --from=build /app/build /usr/share/nginx/html - -EXPOSE 80 - -CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/README.md b/README.md index 21585c0..1bfc4b6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ BTS SIO SISR 2e Année # Travailler localement -## seulement le site +## Tester le site Le rendu le plus immédiat est de lancer le serveur localement avec la commande `npm start` @@ -21,7 +21,7 @@ node --env-file=../.env src/backend/server.js` node --env-file=../.env.local src/index.js ``` -## site le déploiement +## Tester le déploiement Pour tester le site dans une configuration plus proche du déploiement réel, il est possible d'utiliser docker compose. diff --git a/docker-compose.yml b/docker-compose.yml index 6aefba2..5c8bb81 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,28 +2,27 @@ version: '3.8' services: - sample: + web: env_file: - - .env.docker - container_name: sample + - .env + container_name: web build: context: ./portfolio dockerfile: ../Dockerfile target: build - # comment out volumes when target is prod and not build + # comment out volumes when target is prod volumes: - './portfolio:/app' - # comment out until here + # comment out ports in prod, traffic only through proxy ports: - - 3001:80 - - 3002:3001 + - ${NODE_PORT}:${NODE_PORT} networks: - portfolio environment: - CHOKIDAR_USEPOLLING=true database: env_file: - - .env.docker + - .env image: postgres:16.2 environment: # using an .env file, but leaving defaults for easier first time run @@ -32,12 +31,39 @@ services: POSTGRES_DB: ${POSTGRES_DB:-portfolio} volumes: - postgres:/data/postgres + # comment out for prod, we don't need access ports: - - "5433:5432" + - 5433:${POSTGRES_PORT} networks: - portfolio restart: unless-stopped - + server: + env_file: + - .env + environment: + NGINX_ENVSUBST_TEMPLATE_SUFFIX: ".conf" + build: + context: ./portfolio/nginx + dockerfile: Dockerfile + ports: + - 8005:${NGINX_PORT} + # volumes: + # - certbot-etc:/etc/letsencrypt + # - certbot-var:/var/lib/letsencrypt + depends_on: + - web + networks: + - portfolio + # certbot: + # image: certbot/certbot + # container_name: certbot + # volumes: + # - certbot-etc:/etc/letsencrypt + # - certbot-var:/var/lib/letsencrypt + # - web-root:/var/www/html + # depends_on: + # - web + # command: certonly --webroot --webroot-path=/var/www/html --email user@email-domain.com --agree-tos --no-eff-email --staging -d node.exampledomain.com networks: portfolio: driver: bridge diff --git a/portfolio/nginx/Dockerfile b/portfolio/nginx/Dockerfile new file mode 100644 index 0000000..3ce7af5 --- /dev/null +++ b/portfolio/nginx/Dockerfile @@ -0,0 +1,7 @@ +FROM nginx:bookworm + +RUN rm /etc/nginx/conf.d/default.conf + +COPY nginx.conf /etc/nginx/templates/default.conf.conf + +# CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/portfolio/nginx/nginx.conf b/portfolio/nginx/nginx.conf new file mode 100644 index 0000000..9bd2db8 --- /dev/null +++ b/portfolio/nginx/nginx.conf @@ -0,0 +1,25 @@ +# upstream loadbalancer { +# server localhost:3002; +# } + +server { + listen ${NGINX_PORT}; + listen [::]:${NGINX_PORT}; + + server_name ${SERVER_NAME}; + + location ~ /.well-known/acme-challenge { + allow all; + root /var/www/html; + } + + location / { + proxy_pass http://web:${NODE_PORT}; +# proxy_pass http://loadbalancer; + + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +} \ No newline at end of file diff --git a/portfolio/src/index.js b/portfolio/src/index.js index 978484e..5250996 100644 --- a/portfolio/src/index.js +++ b/portfolio/src/index.js @@ -1,6 +1,6 @@ const express = require('express') const app = express() -const port = 3001 +const port = process.env.NODE_PORT var bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: true })); @@ -24,7 +24,7 @@ global.pool = new Pool({ app.use(express.json()) app.use(function (req, res, next) { - res.setHeader('Access-Control-Allow-Origin', 'http://localhost:5173'); + res.setHeader('Access-Control-Allow-Origin', ['*']); res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers'); next(); diff --git a/portfolio/src/routes/main.js b/portfolio/src/routes/main.js index 7b09390..9574ff1 100644 --- a/portfolio/src/routes/main.js +++ b/portfolio/src/routes/main.js @@ -14,22 +14,9 @@ router.get("/", (req, res) => { res.render("index.ejs"); }); -router.get('/', (req, res) => { - model.getCourses() - .then(response => { - res.status(200).send(response); - }) - .catch(error => { - res.status(500).send(error); - }) -}) - router.get('/test', (req, res) => { - model.getCourses().then((courses) => { - res.send(`This is the server endpoint! ` + courses[0][1]) - }); - // res.send(`This is the server endpoint! `+courses) + res.send(`This is the server endpoint on port ` + port) });