From 5d0c1c46ef26a2a1fb2da7dfd7e1b8aef517a98f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20Gon=C3=A7alves?= Date: Mon, 29 Jan 2024 09:08:48 +0000 Subject: [PATCH] Implement reverse proxy with Caddy Server --- .gitignore | 3 +++ Caddyfile.dev | 9 +++++++ backend/src/index.js | 6 +++-- backend/src/routes/authRoutes.js | 8 +++--- docker-compose.yaml | 45 +++++++++++++++++++++++++++----- 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 Caddyfile.dev diff --git a/.gitignore b/.gitignore index 464a908..d014731 100644 --- a/.gitignore +++ b/.gitignore @@ -176,3 +176,6 @@ sketch # Ignore storage files backend/storage + +# Ignore caddy data +caddy_data/ diff --git a/Caddyfile.dev b/Caddyfile.dev new file mode 100644 index 0000000..4fda028 --- /dev/null +++ b/Caddyfile.dev @@ -0,0 +1,9 @@ +https://localhost { + handle_path /api/* { + reverse_proxy backend:3000 + } + + handle { + reverse_proxy frontend:5173 + } +} diff --git a/backend/src/index.js b/backend/src/index.js index 363f340..88d3688 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -10,7 +10,7 @@ const morgan = require("morgan"); require("./auth/fenixOAuth2"); require("./auth/demoLocal"); const isLoggedIn = require("./middleware/isLoggedIn"); -const { logger, logInfo } = require("./modules/logging"); +const { logger, logInfo, logError } = require("./modules/logging"); const app = express(); @@ -28,6 +28,7 @@ app.use( ); const redisClient = new Redis(process.env.REDIS_PORT, process.env.REDIS_HOST); +redisClient.on("error", (error) => logError("index", error.stack, "Redis")); app.use( session({ store: new RedisStore({ client: redisClient }), @@ -49,9 +50,10 @@ app.use(passport.session()); app.use(require("./middleware/parseMultipartFormData")); app.use(require("./middleware/selectPool")); app.use(require("./middleware/error").errorHandler); +morgan.token("protocol", (req) => req.protocol); app.use( morgan( - ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]', + ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] - :protocol', { stream: { write: (message) => logger.http(message.trim()), diff --git a/backend/src/routes/authRoutes.js b/backend/src/routes/authRoutes.js index 31edb90..1716638 100644 --- a/backend/src/routes/authRoutes.js +++ b/backend/src/routes/authRoutes.js @@ -11,8 +11,8 @@ router.get( "/fenix/callback", asyncHandler( passport.authenticate("fenix", { - successRedirect: "/auth/fenix/success", - failureRedirect: "/auth/fenix/failure", + successRedirect: `${process.env.API_ADDRESS}/auth/fenix/success`, + failureRedirect: `${process.env.API_ADDRESS}/auth/fenix/failure`, }), ), ); @@ -29,8 +29,8 @@ router.post( "/demo", asyncHandler( passport.authenticate("demo", { - successRedirect: "/auth/demo/success", - failureRedirect: "/auth/demo/failure", + successRedirect: `${process.env.API_ADDRESS}/auth/demo/success`, + failureRedirect: `${process.env.API_ADDRESS}/auth/demo/failure`, }), ), ); diff --git a/docker-compose.yaml b/docker-compose.yaml index 36a79a2..10502ae 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,7 +4,7 @@ services: postgres: container_name: postgres - image: postgres + image: postgres:16 restart: always volumes: - ./backend/init.sql:/docker-entrypoint-initdb.d/init.sql @@ -13,10 +13,12 @@ services: - PGTZ=Europe/Lisbon - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres + networks: + - backend postgres-demo: container_name: postgres-demo - image: postgres + image: postgres:16 restart: always volumes: - ./backend/init-demo.sql:/docker-entrypoint-initdb.d/init.sql @@ -25,13 +27,17 @@ services: - PGTZ=Europe/Lisbon - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres + networks: + - backend redis: container_name: redis - image: redis + image: redis:7.2 restart: always environment: - TZ=Europe/Lisbon + networks: + - backend backend: container_name: backend @@ -40,13 +46,14 @@ services: depends_on: - postgres - redis - ports: - - 3000:3000 volumes: - ./backend/src:/usr/src/app/backend/src - ./backend/storage:/usr/src/app/backend/storage environment: - TZ=Europe/Lisbon + networks: + - backend + - caddy-backend frontend: container_name: frontend @@ -54,10 +61,34 @@ services: restart: always depends_on: - backend - ports: - - 5173:5173 volumes: - ./frontend/src:/usr/src/app/frontend/src - ./frontend/dist:/usr/src/app/frontend/dist environment: - TZ=Europe/Lisbon + networks: + - caddy-frontend + + caddy: + container_name: caddy + image: caddy:2.7 + restart: unless-stopped + cap_add: + - NET_ADMIN + ports: + - "80:80" + - "443:443" + volumes: + - ./Caddyfile.dev:/etc/caddy/Caddyfile + - ./caddy_data:/data + networks: + - caddy-backend + - caddy-frontend + +networks: + backend: + driver: bridge + caddy-backend: + driver: bridge + caddy-frontend: + driver: bridge