Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/backend-deploy-stackit-ske.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ jobs:
OVERLAY: ${{ steps.target.outputs.overlay }}
DIGEST: ${{ needs.build.outputs.digest }}
BACKEND_SECRET: ${{ steps.secrets.outputs.backend }}
GOTENBERG_SECRET: ${{ steps.secrets.outputs.gotenberg }}
run: bash infra/k8s/deploy/render-and-apply.sh

- name: Wait for rollout ⏳
Expand Down
6 changes: 5 additions & 1 deletion README_DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ You'll need accounts and API keys for:
#### Required for Backend

- **Mistral AI**: API key from https://console.mistral.ai/
- **Gotenberg**: URL and credentials for Gotenberg service (PDF conversion)
- **Gotenberg**: runs locally in Docker, no account needed. Set `GOTENBERG_URL=http://localhost:3001` (see [Development](#-development))
- **Sentry**: DSN from https://sentry.io/ (for error monitoring)
- **Redis**: Connection string (for the API rate limiting queue)

Expand Down Expand Up @@ -242,6 +242,10 @@ No environment configuration needed. This is a static page displayed during main

## 💻 Development

The backend's `npm run dev` (also via `turbo dev`) first starts Gotenberg in Docker on
`http://localhost:3001` (`compose.yml`), so Docker must be running. Stop it with
`docker compose stop gotenberg`.

**Using Turborepo (recommended):**

```bash
Expand Down
2 changes: 0 additions & 2 deletions apps/backend/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ SENTRY_ORG=...
SENTRY_PROJECT=...

# Gotenberg
GOTENBERG_API_BASIC_AUTH_USERNAME=...
GOTENBERG_API_BASIC_AUTH_PASSWORD=...
GOTENBERG_URL=...

# Feature Flags
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"auth:rotate-passwords": "tsx supabase/scripts/rotate-user-passwords.ts",
"auth:assess-inactive-users": "tsx supabase/scripts/process-inactive-users.ts -- --dry-run",
"auth:notify-inactive-users": "tsx supabase/scripts/process-inactive-users.ts -- --skip-deletions",
"auth:delete-inactive-users": "tsx supabase/scripts/process-inactive-users.ts -- --deletions-only"
"auth:delete-inactive-users": "tsx supabase/scripts/process-inactive-users.ts -- --deletions-only",
"predev": "docker compose -f ../../compose.yml up -d --quiet-pull gotenberg"
},
"dependencies": {
"@ai-sdk/mcp": "2.0.91",
Expand Down
10 changes: 0 additions & 10 deletions apps/backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export interface Config {
defaultDocumentProcessingModel: string;
sentryDsn: string;
gotenbergUrl: string;
gotenbergApiBasicAuthUsername: string;
gotenbergApiBasicAuthPassword: string;
presencePenalty: number;
frequencyPenalty: number;
featureFlagMcpParlaAllowed: boolean;
Expand Down Expand Up @@ -117,12 +115,6 @@ export function verifyConfig(): void {
if (!process.env.GOTENBERG_URL && !process.env.CI) {
throw new Error("GOTENBERG_URL must be defined");
}
if (!process.env.GOTENBERG_API_BASIC_AUTH_USERNAME && !process.env.CI) {
throw new Error("GOTENBERG_API_BASIC_AUTH_USERNAME must be defined");
}
if (!process.env.GOTENBERG_API_BASIC_AUTH_PASSWORD && !process.env.CI) {
throw new Error("GOTENBERG_API_BASIC_AUTH_PASSWORD must be defined");
}
if (
process.env.FEATURE_FLAG_MCP_PARLA_ALLOWED === "true" &&
!process.env.MCP_PARLA_URL
Expand Down Expand Up @@ -210,8 +202,6 @@ export const config: Config = {
defaultDocumentProcessingModel: process.env.DEFAULT_DOCUMENT_PROCESSING_MODEL,
sentryDsn: process.env.SENTRY_DSN,
gotenbergUrl: process.env.GOTENBERG_URL,
gotenbergApiBasicAuthUsername: process.env.GOTENBERG_API_BASIC_AUTH_USERNAME,
gotenbergApiBasicAuthPassword: process.env.GOTENBERG_API_BASIC_AUTH_PASSWORD,
presencePenalty: parseFloat(process.env.PRESENCE_PENALTY || "0"),
frequencyPenalty: parseFloat(process.env.FREQUENCY_PENALTY || "0"),
featureFlagMcpParlaAllowed:
Expand Down
4 changes: 0 additions & 4 deletions apps/backend/src/services/document-extraction-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,10 @@ export class WordDocumentExtractionService {
"/forms/libreoffice/convert",
config.gotenbergUrl,
).toString();
const authHeader = `Basic ${Buffer.from(
`${config.gotenbergApiBasicAuthUsername}:${config.gotenbergApiBasicAuthPassword}`,
).toString("base64")}`;

const res = await fetch(endpoint, {
method: "POST",
headers: {
Authorization: authHeader,
Accept: "application/pdf",
},
body: form,
Expand Down
21 changes: 21 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ services:
# can keep localhost for `turbo dev`. A real env var beats the mounted .env
environment:
SUPABASE_URL: http://host.docker.internal:54321
GOTENBERG_URL: http://gotenberg:3000
depends_on:
gotenberg:
condition: service_healthy
# Liveness probe. Using fetch since the slim image ships no curl/wget.
healthcheck:
test:
Expand All @@ -40,3 +44,20 @@ services:
start_period: 10s
mem_limit: 2g
cpus: 1

# Document → PDF conversion. `npm run dev` in apps/backend starts it on its own (predev).
gotenberg:
container_name: baergpt-gotenberg
# Keep in sync with infra/k8s/base/gotenberg/deployment.yaml.
image: gotenberg/gotenberg:8.37.0@sha256:f29984bd1e226bf1b93ba90af06000afa8b315853e99d27b9aaa41b93f15c769
command: ["gotenberg", "--chromium-disable-routes"]
ports:
# 3000 is the backend's. Loopback only, as Gotenberg has no auth.
- "127.0.0.1:3001:3000"
healthcheck:
test: ["CMD", "curl", "-fsS", "http://localhost:3000/health"]
interval: 10s
timeout: 5s
retries: 18
start_period: 10s
mem_limit: 2g
9 changes: 3 additions & 6 deletions infra/k8s/base/gotenberg/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,12 @@ spec:
drop: ["ALL"]
# 8.37.0+: older LibreOffice has memory bugs in the DOCX import.
image: gotenberg/gotenberg:8.37.0@sha256:f29984bd1e226bf1b93ba90af06000afa8b315853e99d27b9aaa41b93f15c769
# The backend only uses the LibreOffice route.
args: ["gotenberg", "--api-enable-basic-auth", "--chromium-disable-routes"]
# The backend only uses the LibreOffice route. No basic auth: the NetworkPolicy lets
# only the backend reach Gotenberg.
args: ["gotenberg", "--chromium-disable-routes"]
ports:
- name: http
containerPort: 3000
envFrom:
# The deploy workflow replaces this with gotenberg-env-a or -b, filled from 1Password.
- secretRef:
name: gotenberg-env # GOTENBERG_API_BASIC_AUTH_USERNAME/PASSWORD
volumeMounts:
- name: tmp
mountPath: /tmp
Expand Down
10 changes: 3 additions & 7 deletions infra/k8s/deploy/render-and-apply.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/usr/bin/env bash
# Renders an environment's overlay with this deploy's image and env Secrets, and applies it.
# Renders an environment's overlay with this deploy's image and env Secret, and applies it.
#
# The overlay is wrapped in a temporary kustomization, so the checked-out files stay unchanged.
# Before applying, it records each Deployment's revision, so roll-back.sh can undo exactly what
# this deploy changed.
#
# Inputs: NAMESPACE, OVERLAY (path to infra/k8s/overlays/<namespace>), IMAGE_NAME, DIGEST,
# BACKEND_SECRET, and GOTENBERG_SECRET (empty where the overlay has no Gotenberg).
# Inputs: NAMESPACE, OVERLAY (path to infra/k8s/overlays/<namespace>), IMAGE_NAME, DIGEST and
# BACKEND_SECRET.
# Writes to the current directory, for wait-for-rollout.sh and roll-back.sh:
# rendered.yaml, applied.txt, deployments.txt, revisions-before.txt
set -euo pipefail

: "${NAMESPACE:?}" "${OVERLAY:?}" "${IMAGE_NAME:?}" "${DIGEST:?}" "${BACKEND_SECRET:?}"
GOTENBERG_SECRET=${GOTENBERG_SECRET:-}

main() {
kustomize_dir=$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/render.XXXXXX")
Expand Down Expand Up @@ -45,9 +44,6 @@ images:
patches:
EOF
use_env_secret backend "$BACKEND_SECRET" >> "$dir/kustomization.yaml"
if [[ -n "$GOTENBERG_SECRET" ]]; then
use_env_secret gotenberg "$GOTENBERG_SECRET" >> "$dir/kustomization.yaml"
fi
}

# A patch that points the app's Deployment at the given env Secret. The `test` fails the
Expand Down
24 changes: 8 additions & 16 deletions infra/k8s/deploy/sync-env-secrets.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
# Writes an environment's env from its 1Password note into the app Secrets and reports which
# Secret each Deployment should use (GitHub step outputs `backend` and `gotenberg`).
# Writes an environment's env from its 1Password note into the backend Secret and reports which
# Secret the Deployment should use (GitHub step output `backend`).
#
# Each app has two Secrets, <app>-env-a and <app>-env-b. A changed env goes into the one not
# The app has two Secrets, <app>-env-a and <app>-env-b. A changed env goes into the one not
# in use, so the other keeps the previous env and `kubectl rollout undo` restores code and env
# together. An unchanged env keeps the Secret in use, so the pods don't restart.
#
Expand All @@ -17,23 +17,18 @@ main() {
trap 'rm -rf "$work_dir"' EXIT

op read "$ENV_NOTE_REF" > "$work_dir/note.env"
split_note "$work_dir/note.env" "$work_dir/backend.env" "$work_dir/gotenberg.env"
backend_env_from_note "$work_dir/note.env" "$work_dir/backend.env"

sync_app backend "$work_dir/backend.env"
# Sandbox uses staging's Gotenberg.
if [[ "$NAMESPACE" != "sandbox" ]]; then
sync_app gotenberg "$work_dir/gotenberg.env"
fi
}

# The backend gets every variable except the Cloud Foundry sizing keys in the same note.
# Gotenberg only reads its basic-auth credentials. The note is read again on every deploy,
# so a variable removed from it disappears from the Secret too.
split_note() {
local note=$1 backend_env=$2 gotenberg_env=$3
# The note is read again on every deploy, so a variable removed from it disappears from the
# Secret too.
backend_env_from_note() {
local note=$1 backend_env=$2
local line key value
: > "$backend_env"
: > "$gotenberg_env"
while IFS= read -r line || [[ -n "$line" ]]; do
line=${line%$'\r'} # tolerate a CRLF-edited note
if is_blank_or_comment "$line"; then
Expand All @@ -47,9 +42,6 @@ split_note() {
CF_* | GOTENBERG_MEMORY) continue ;;
esac
echo "$key=$value" >> "$backend_env"
if [[ "$key" == GOTENBERG_API_BASIC_AUTH_* ]]; then
echo "$key=$value" >> "$gotenberg_env"
fi
done < "$note"
}

Expand Down