Skip to content

Commit

Permalink
Implement reverse proxy with Caddy Server
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogoncalves03 committed Jan 29, 2024
1 parent 2575170 commit 5d0c1c4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,6 @@ sketch

# Ignore storage files
backend/storage

# Ignore caddy data
caddy_data/
9 changes: 9 additions & 0 deletions Caddyfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
https://localhost {
handle_path /api/* {
reverse_proxy backend:3000
}

handle {
reverse_proxy frontend:5173
}
}
6 changes: 4 additions & 2 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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 }),
Expand All @@ -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()),
Expand Down
8 changes: 4 additions & 4 deletions backend/src/routes/authRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
}),
),
);
Expand All @@ -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`,
}),
),
);
Expand Down
45 changes: 38 additions & 7 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -40,24 +46,49 @@ 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
build: ./frontend
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

0 comments on commit 5d0c1c4

Please sign in to comment.