Skip to content

Commit

Permalink
add nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroMartinSteenstrup committed Apr 16, 2024
1 parent 4fcd5b4 commit 9c2d1f7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 37 deletions.
9 changes: 0 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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;"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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.
Expand Down
46 changes: 36 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [email protected] --agree-tos --no-eff-email --staging -d node.exampledomain.com
networks:
portfolio:
driver: bridge
Expand Down
7 changes: 7 additions & 0 deletions portfolio/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -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;"]
25 changes: 25 additions & 0 deletions portfolio/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions portfolio/src/index.js
Original file line number Diff line number Diff line change
@@ -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 }));
Expand All @@ -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();
Expand Down
15 changes: 1 addition & 14 deletions portfolio/src/routes/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});


Expand Down

0 comments on commit 9c2d1f7

Please sign in to comment.