From 28b2988b86fcde24c8cad7d1fe290854ee013c98 Mon Sep 17 00:00:00 2001 From: Maxime Cannoodt Date: Sun, 3 Jul 2022 11:02:21 +0200 Subject: [PATCH] Add reverse proxy to docker compose (traefik) --- docker-compose.yml | 33 +++++++++++++++++++++++++------- plugin/src/NoteSharingService.ts | 2 +- server/server.ts | 7 ++++--- webapp/src/routes/note/[id].ts | 2 +- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a326a56..3ca06c9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,21 @@ version: "3.7" 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 migrate: build: @@ -16,32 +31,36 @@ services: build: context: ./server dockerfile: Dockerfile - ports: - - "8080:8080" volumes: - sqlite:/database/ environment: - DATABASE_URL=file:/database/db.sqlite - - FRONTEND_URL=http://localhost:3000 + - FRONTEND_URL=http://localhost:5000 - CLEANUP_INTERVAL_SECONDS=600 - POST_LIMIT_WINDOW_SECONDS=86400 - POST_LIMIT=50 depends_on: migrate: 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: build: context: ./webapp dockerfile: Dockerfile args: - - VITE_SERVER_INTERNAL=http://server:8080 + - VITE_SERVER_INTERNAL=http://backend:8080 - VITE_BRANDING=Noteshare.space [preview] - ports: - - "3000:3000" depends_on: - 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: sqlite: \ No newline at end of file diff --git a/plugin/src/NoteSharingService.ts b/plugin/src/NoteSharingService.ts index e8b5a08..69e89b4 100644 --- a/plugin/src/NoteSharingService.ts +++ b/plugin/src/NoteSharingService.ts @@ -32,7 +32,7 @@ export class NoteSharingService { hmac: string ): Promise { const res = await requestUrl({ - url: `${this._url}/note`, + url: `${this._url}/api/note`, method: "POST", contentType: "application/json", body: JSON.stringify({ ciphertext, hmac }), diff --git a/server/server.ts b/server/server.ts index 4ea69a3..e84a998 100644 --- a/server/server.ts +++ b/server/server.ts @@ -34,7 +34,7 @@ app.listen(process.env.PORT, () => { // Post new encrypted note app.post( - "/note/", + "/api/note/", postLimiter, async (req: Request<{}, {}, EncryptedNote>, res) => { const note = req.body; @@ -49,7 +49,7 @@ app.post( ); // Get encrypted note -app.get("/note/:id", async (req, res) => { +app.get("/api/note/:id", async (req, res) => { const note = await prisma.encryptedNote.findUnique({ where: { id: req.params.id }, }); @@ -62,7 +62,8 @@ app.get("/note/:id", async (req, res) => { // Default response for any other request 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 diff --git a/webapp/src/routes/note/[id].ts b/webapp/src/routes/note/[id].ts index ba1686c..f6a0643 100644 --- a/webapp/src/routes/note/[id].ts +++ b/webapp/src/routes/note/[id].ts @@ -2,7 +2,7 @@ import type { EncryptedNote } from '$lib/model/EncryptedNote'; import type { RequestHandler } from '@sveltejs/kit'; 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); if (response.ok) {