From bb2fa55cd5999a3236d87e3594b1843c70efaa31 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 9 Dec 2024 18:04:56 +0100 Subject: [PATCH] feat: docker config for new frontend --- .gitignore | 4 ++++ DOCKER.md | 8 +++++--- Foxnouns.Frontend/.dockerignore | 4 ++++ Foxnouns.Frontend/Dockerfile | 17 ++++++++++++++++ Foxnouns.Frontend/src/hooks.server.ts | 1 + docker-compose.yml | 28 ++++++++++----------------- docker/Caddyfile | 11 +++++++++-- docker/frontend.env | 0 docker/frontend.example.env | 4 ++++ 9 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 Foxnouns.Frontend/.dockerignore create mode 100644 Foxnouns.Frontend/Dockerfile delete mode 100644 docker/frontend.env create mode 100644 docker/frontend.example.env diff --git a/.gitignore b/.gitignore index d9b0dfd..c61154d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,7 @@ config.ini *.DotSettings.user proxy-config.json .DS_Store + +docker/config.ini +docker/proxy-config.json +docker/frontend.env diff --git a/DOCKER.md b/DOCKER.md index a4f8c3a..5cbb6e6 100644 --- a/DOCKER.md +++ b/DOCKER.md @@ -2,7 +2,9 @@ 1. Copy `docker/config.example.ini` to `docker/config.ini`, and change the settings to your liking. 2. Copy `docker/proxy-config.example.json` to `docker/proxy-config.json`, and do the same. -3. Build with `docker compose build` -4. Run with `docker compose up` +3. Copy `docker/frontend.example.env` to `docker/frontend.env`, and do th esame. +4. Build with `docker compose build` +5. Run with `docker compose up` -The Caddy server will listen on `localhost:5004`. +The Caddy server will listen on `localhost:5004` for the frontend and API, +and on `localhost:5005` for the profile URL shortener. diff --git a/Foxnouns.Frontend/.dockerignore b/Foxnouns.Frontend/.dockerignore new file mode 100644 index 0000000..14ad623 --- /dev/null +++ b/Foxnouns.Frontend/.dockerignore @@ -0,0 +1,4 @@ +node_modules/ +build/ +.env +.env* diff --git a/Foxnouns.Frontend/Dockerfile b/Foxnouns.Frontend/Dockerfile new file mode 100644 index 0000000..166be23 --- /dev/null +++ b/Foxnouns.Frontend/Dockerfile @@ -0,0 +1,17 @@ +FROM docker.io/node:22-slim + +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable + +COPY ./Foxnouns.Frontend /app +COPY ./docker/frontend.env /app/.env.local +WORKDIR /app + +ENV PRIVATE_API_HOST=http://rate:5003/api +ENV PRIVATE_INTERNAL_API_HOST=http://backend:5000/api + +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile +RUN pnpm run build + +CMD ["pnpm", "node", "build/index.js"] diff --git a/Foxnouns.Frontend/src/hooks.server.ts b/Foxnouns.Frontend/src/hooks.server.ts index e8ec723..eb1f93d 100644 --- a/Foxnouns.Frontend/src/hooks.server.ts +++ b/Foxnouns.Frontend/src/hooks.server.ts @@ -3,6 +3,7 @@ import { PUBLIC_API_BASE } from "$env/static/public"; import type { HandleFetch } from "@sveltejs/kit"; export const handleFetch: HandleFetch = async ({ request, fetch }) => { + console.log(PUBLIC_API_BASE, PRIVATE_INTERNAL_API_HOST, PRIVATE_API_HOST); if (request.url.startsWith(`${PUBLIC_API_BASE}/internal`)) { request = new Request(request.url.replace(PUBLIC_API_BASE, PRIVATE_INTERNAL_API_HOST), request); } else if (request.url.startsWith(PUBLIC_API_BASE)) { diff --git a/docker-compose.yml b/docker-compose.yml index 6fafd18..5606760 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,8 +5,8 @@ services: context: . dockerfile: ./Dockerfile.backend environment: - - "Database:Url=Host=pgbouncer;Database=postgres;Username=postgres;Password=postgres" - - "Database:EnablePooling=false" + - "Database:Url=Host=postgres;Database=postgres;Username=postgres;Password=postgres" + - "Database:EnablePooling=true" - "Host=0.0.0.0" - "Port=5000" restart: unless-stopped @@ -15,13 +15,14 @@ services: frontend: image: frontend - build: ./Foxnouns.Frontend - environment: - - "API_BASE=http://rate:5003/api" - - "INTERNAL_API_BASE=http://backend:5000/api" + build: + context: ./ + dockerfile: ./Foxnouns.Frontend/Dockerfile restart: unless-stopped - volumes: - - ./docker/frontend.env:/app/.env + env_file: ./docker/frontend.env + environment: + - "PRIVATE_API_HOST=http://rate:5003/api" + - "PRIVATE_INTERNAL_API_HOST=http://backend:5000/api" rate: image: rate @@ -32,16 +33,6 @@ services: volumes: - ./docker/proxy-config.json:/app/proxy-config.json - pgbouncer: - image: docker.io/edoburu/pgbouncer:latest - environment: - - "DATABASE_URL=postgres://postgres:postgres@postgres/postgres" - - "AUTH_TYPE=scram-sha-256" - - "MAX_CLIENT_CONN=100" - - "DEFAULT_POOL_SIZE=100" - - "MIN_POOL_SIZE=10" - restart: unless-stopped - postgres: image: docker.io/postgres:16 command: [ "postgres", @@ -61,6 +52,7 @@ services: restart: unless-stopped ports: - "5004:80" + - "5005:81" volumes: - ./docker/Caddyfile:/etc/caddy/Caddyfile - caddy_data:/data diff --git a/docker/Caddyfile b/docker/Caddyfile index 6e12647..9132068 100644 --- a/docker/Caddyfile +++ b/docker/Caddyfile @@ -1,4 +1,11 @@ -http:// { +# Frontend and API +http://localhost:80 { reverse_proxy /api/* http://rate:5003 reverse_proxy http://frontend:3000 -} \ No newline at end of file +} + +# prns.cc (profile URL shortener) +http://localhost:81 { + rewrite * /sid{uri} + reverse_proxy http://backend:5000 +} diff --git a/docker/frontend.env b/docker/frontend.env deleted file mode 100644 index e69de29..0000000 diff --git a/docker/frontend.example.env b/docker/frontend.example.env new file mode 100644 index 0000000..b68a330 --- /dev/null +++ b/docker/frontend.example.env @@ -0,0 +1,4 @@ +PUBLIC_LANGUAGE=en +PUBLIC_BASE_URL=https://pronouns.cc +PUBLIC_SHORT_URL=https://prns.cc +PUBLIC_API_BASE=https://pronouns.cc/api