Add reverse proxy to docker compose (traefik)

This commit is contained in:
Maxime Cannoodt 2022-07-03 11:02:21 +02:00
parent 65aaa33f4f
commit 28b2988b86
4 changed files with 32 additions and 12 deletions

View File

@ -1,6 +1,21 @@
version: "3.7" version: "3.7"
services: services:
# Reverse proxy
traefik:
image: "traefik:v2.8"
command:
#- "--log.level=DEBUG"
- "--api.insecure=true" # allows accessing a Traefik dashboard, disable in production
- "--providers.docker=true" # enables the Docker configuration discovery
- "--providers.docker.exposedbydefault=false" # do not expose Docker services by default
- "--entrypoints.web.address=:5000" # create an entrypoint called web, listening on :5000
ports:
- "5000:5000"
- "8765:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
# Prisma sqlite migration utility # Prisma sqlite migration utility
migrate: migrate:
build: build:
@ -16,32 +31,36 @@ services:
build: build:
context: ./server context: ./server
dockerfile: Dockerfile dockerfile: Dockerfile
ports:
- "8080:8080"
volumes: volumes:
- sqlite:/database/ - sqlite:/database/
environment: environment:
- DATABASE_URL=file:/database/db.sqlite - DATABASE_URL=file:/database/db.sqlite
- FRONTEND_URL=http://localhost:3000 - FRONTEND_URL=http://localhost:5000
- CLEANUP_INTERVAL_SECONDS=600 - CLEANUP_INTERVAL_SECONDS=600
- POST_LIMIT_WINDOW_SECONDS=86400 - POST_LIMIT_WINDOW_SECONDS=86400
- POST_LIMIT=50 - POST_LIMIT=50
depends_on: depends_on:
migrate: migrate:
condition: service_completed_successfully condition: service_completed_successfully
labels:
- "traefik.enable=true" # tell Traefik this is something we would like to expose
- "traefik.http.routers.backend.entrypoints=web" # what entrypoint should be used for the backend service.
- "traefik.http.routers.backend.rule=Host(`localhost`) && PathPrefix(`/api`) && Method(`POST`)" #
# Frontend for serving encrypted notes over HTML (SvelteKit) # Frontend for serving encrypted notes over HTML (SvelteKit)
frontend: frontend:
build: build:
context: ./webapp context: ./webapp
dockerfile: Dockerfile dockerfile: Dockerfile
args: args:
- VITE_SERVER_INTERNAL=http://server:8080 - VITE_SERVER_INTERNAL=http://backend:8080
- VITE_BRANDING=Noteshare.space [preview] - VITE_BRANDING=Noteshare.space [preview]
ports:
- "3000:3000"
depends_on: depends_on:
- backend - backend
labels:
- "traefik.enable=true" # tell Traefik this is something we would like to expose
- "traefik.http.routers.frontend.entrypoints=web" # what entrypoint should be used for the frontend service.
- "traefik.http.routers.frontend.rule=Host(`localhost`)" #
volumes: volumes:
sqlite: sqlite:

View File

@ -32,7 +32,7 @@ export class NoteSharingService {
hmac: string hmac: string
): Promise<Response> { ): Promise<Response> {
const res = await requestUrl({ const res = await requestUrl({
url: `${this._url}/note`, url: `${this._url}/api/note`,
method: "POST", method: "POST",
contentType: "application/json", contentType: "application/json",
body: JSON.stringify({ ciphertext, hmac }), body: JSON.stringify({ ciphertext, hmac }),

View File

@ -34,7 +34,7 @@ app.listen(process.env.PORT, () => {
// Post new encrypted note // Post new encrypted note
app.post( app.post(
"/note/", "/api/note/",
postLimiter, postLimiter,
async (req: Request<{}, {}, EncryptedNote>, res) => { async (req: Request<{}, {}, EncryptedNote>, res) => {
const note = req.body; const note = req.body;
@ -49,7 +49,7 @@ app.post(
); );
// Get encrypted note // Get encrypted note
app.get("/note/:id", async (req, res) => { app.get("/api/note/:id", async (req, res) => {
const note = await prisma.encryptedNote.findUnique({ const note = await prisma.encryptedNote.findUnique({
where: { id: req.params.id }, where: { id: req.params.id },
}); });
@ -62,7 +62,8 @@ app.get("/note/:id", async (req, res) => {
// Default response for any other request // Default response for any other request
app.use((req, res, next) => { app.use((req, res, next) => {
res.status(404).send(); console.log(`Route not found: ${req.path}`);
res.status(404).send("Route not found");
}); });
// // Error handling // // Error handling

View File

@ -2,7 +2,7 @@ import type { EncryptedNote } from '$lib/model/EncryptedNote';
import type { RequestHandler } from '@sveltejs/kit'; import type { RequestHandler } from '@sveltejs/kit';
export const get: RequestHandler = async ({ params }) => { export const get: RequestHandler = async ({ params }) => {
const url = `${import.meta.env.VITE_SERVER_INTERNAL}/note/${params.id}`; const url = `${import.meta.env.VITE_SERVER_INTERNAL}/api/note/${params.id}`;
const response = await fetch(url); const response = await fetch(url);
if (response.ok) { if (response.ok) {