From dc570dc1c1c1926c8281f7aa1798c204334cabc1 Mon Sep 17 00:00:00 2001 From: Leo Date: Sun, 19 Oct 2025 11:41:12 -0400 Subject: [PATCH] fix(docker): align electric http port config --- docker-compose.yml | 1 + docs/deployment/railway.md | 140 ------------------------------------- env.example | 3 +- 3 files changed, 3 insertions(+), 141 deletions(-) delete mode 100644 docs/deployment/railway.md diff --git a/docker-compose.yml b/docker-compose.yml index fd3de436..c9dc09e7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -45,6 +45,7 @@ services: ELECTRIC_INSECURE: ${ELECTRIC_INSECURE:-false} ELECTRIC_LOG_LEVEL: ${ELECTRIC_LOG_LEVEL:-info} ELECTRIC_HTTP_PORT: ${ELECTRIC_HTTP_PORT:-3010} + ELECTRIC__HTTP__PORT: ${ELECTRIC_HTTP_PORT:-3010} ELECTRIC__HTTP__HOST: ${ELECTRIC__HTTP__HOST:-0.0.0.0} ELECTRIC__PG_PROXY__PORT: ${ELECTRIC__PG_PROXY__PORT:-5133} ELECTRIC__PG_PROXY__HOST: ${ELECTRIC__PG_PROXY__HOST:-0.0.0.0} diff --git a/docs/deployment/railway.md b/docs/deployment/railway.md deleted file mode 100644 index afcece6a..00000000 --- a/docs/deployment/railway.md +++ /dev/null @@ -1,140 +0,0 @@ -# Railway Deployment - -This guide explains how to deploy the OpenChat stack (web, server, Postgres, ElectricSQL) to [Railway](https://railway.app) using config-as-code and the Railway CLI. The deployment relies on four services: - -| Service | Purpose | Notes | -| --- | --- | --- | -| `postgres` | Primary Postgres database | Provided via Railway Postgres plugin | -| `electric` | ElectricSQL sync service | Uses the official `electricsql/electric` image | -| `server` | Bun/Elysia API | Built from `docker/server.Dockerfile` | -| `web` | Next.js frontend | Built from `docker/web.Dockerfile` | - -## 1. Prerequisites - -- Install the Railway CLI (`npm i -g @railway/cli` or `bunx railway --version`). -- Authenticate: `railway login`. -- Ensure the repository is connected to GitHub; Railway will reference the repo for every service. -- (Optional) Create a `production` environment: `railway environment create production`. - -## 2. Config-as-code Files - -Railway reads a `railway.toml`/`railway.json` file for each service. Because this is a monorepo, we keep service-specific files alongside the code and point Railway to them via the “Config file path” setting. - -| Service | Config file | Key settings | -| --- | --- | --- | -| `server` | `apps/server/railway.toml` | Forces Dockerfile builder for `docker/server.Dockerfile`. | -| `web` | `apps/web/railway.toml` | Forces Dockerfile builder for `docker/web.Dockerfile` and sets the root healthcheck. | - -After linking the repo to a Railway service, open **Settings → Build & Deploy → Config as code** and set the file path (e.g. `apps/server/railway.toml`). Railway will respect the Docker CMD from the image, so no additional start command is required. - -## 3. Provision Services - -Run the following from the repo root with the CLI bound to your project (`railway link `): - -```bash -# Postgres database (managed plugin) -railway add --database postgres --service postgres - -# ElectricSQL container using the public image -railway add \ - --image electricsql/electric:1.1.11 \ - --service electric \ - --variables "ELECTRIC_HTTP_PORT=3010" \ - --variables "ELECTRIC__HTTP__HOST=0.0.0.0" \ - --variables "ELECTRIC__PG_PROXY__PORT=5133" - -# Bun API (Dockerfile build) -railway add --service server -railway up --service server --detach - -# Next.js frontend (Dockerfile build) -railway add --service web -railway up --service web --detach -``` - -Notes: - -- `railway up` deploys the current directory to the active service; ensure you run it after linking the proper service. Subsequent deployments can be triggered either via CLI or GitHub integration. -- For the `electric` service we reuse the upstream Docker image rather than building locally. If you need a custom image, add a Dockerfile and switch the config-as-code to the Dockerfile builder. - -## 4. Environment Variables & Linking - -Use Railway’s variable templating to keep secrets centralized. Examples assume the active CLI service is `server` unless noted. - -### Shared Variables (project-level) - -```bash -railway variables set --shared NEXT_PUBLIC_APP_URL=https://ochat.pro -railway variables set --shared NEXT_PUBLIC_SERVER_URL=https://api.ochat.pro -railway variables set --shared NEXT_PUBLIC_ELECTRIC_URL=https://electric.ochat.pro -railway variables set --shared BETTER_AUTH_URL=https://api.ochat.pro -railway variables set --shared CORS_ORIGIN=https://ochat.pro -``` - -### Postgres credentials - -Railway’s Postgres plugin creates `DATABASE_URL` automatically. Reference it from other services using the templating syntax: - -```bash -# Server service -railway variables set --service server DATABASE_URL='${{postgres.DATABASE_URL}}' -railway variables set --service server SHADOW_DATABASE_URL='${{postgres.DATABASE_URL}}?schema=openchat_shadow' - -# Electric service -railway variables set --service electric DATABASE_URL='${{postgres.DATABASE_URL}}' -railway variables set --service electric SHADOW_DATABASE_URL='${{postgres.DATABASE_URL}}?schema=openchat_shadow' -``` - -> **Note:** If Railway (or another platform) provides Postgres credentials as individual variables, you can omit `DATABASE_URL` and instead set `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_USER`, `DATABASE_PASSWORD`, and `DATABASE_NAME`. The server will derive the connection string at runtime, but you still need to supply `SHADOW_DATABASE_URL` separately. Production builds no longer load workspace `.env` defaults automatically; configure these vars in Railway or set `SERVER_REQUIRE_WORKSPACE_ENV=1` only if you intentionally want to fallback to workspace files. - -Create the databases once (if the default `openchat` DB is missing, recreate it too): - -```bash -railway connect postgres -# inside psql -CREATE DATABASE openchat; -CREATE DATABASE openchat_shadow; -\q -``` - -### Auth & Electric secrets - -Generate strong secrets locally (reusing `bun run generate:secrets --force` is fine) and set them as sealed variables: - -```bash -railway variables set --service server BETTER_AUTH_SECRET= -railway variables set --service server ELECTRIC_GATEKEEPER_SECRET= -railway variables set --service server ELECTRIC_SECRET= - -railway variables set --service electric ELECTRIC_GATEKEEPER_SECRET= -railway variables set --service electric PG_PROXY_PASSWORD= -railway variables set --service electric ELECTRIC_SECRET= -railway variables set --service server PG_PROXY_PASSWORD='${{electric.PG_PROXY_PASSWORD}}' -``` - -### Frontend variables - -Point the web service at the shared values and the server proxy: - -```bash -railway service web -railway variables set NEXT_PUBLIC_APP_URL='${{shared.NEXT_PUBLIC_APP_URL}}' -railway variables set NEXT_PUBLIC_SERVER_URL='${{shared.NEXT_PUBLIC_SERVER_URL}}' -railway variables set NEXT_PUBLIC_ELECTRIC_URL='${{shared.NEXT_PUBLIC_ELECTRIC_URL}}' -``` - -## 5. Deployment Flow - -1. Push changes to `main`. If GitHub integration is enabled for each service, Railway will build using the specified config-as-code file. -2. For manual deploys, run `railway service ` followed by `railway up`. -3. Verify logs: `railway logs --since 1h --service server` and `railway logs --service electric`. -4. Apply database migrations: `railway service server && railway run bun run db:push`. - -## 6. Troubleshooting Checklist - -- **Config file not picked up**: open the service settings and ensure the “Config file” path (e.g. `apps/web/railway.toml`) is set correctly. -- **Electric cannot reach Postgres**: confirm the `electric` service has `DATABASE_URL` templated from `postgres` and that the shadow database exists. -- **Next.js build fails from missing HTTPS URLs**: verify the shared variables include full `https://` origins. -- **Auth sessions invalid**: ensure `BETTER_AUTH_SECRET` and cookie domain (`AUTH_COOKIE_DOMAIN=.ochat.pro`) are set on the server service. - -With these files and commands in place, you can version your Railway configuration alongside the repo and reproduce the four-service stack reliably. diff --git a/env.example b/env.example index 8c0bb5f8..616eaf28 100644 --- a/env.example +++ b/env.example @@ -18,10 +18,11 @@ ELECTRIC_GATEKEEPER_SECRET=replace-with-generated-secret ELECTRIC_INSECURE=false ELECTRIC_LOG_LEVEL=info ELECTRIC_HTTP_PORT=3010 +ELECTRIC__HTTP__PORT=3010 ELECTRIC__HTTP__HOST=0.0.0.0 ELECTRIC__PG_PROXY__PORT=5133 ELECTRIC__PG_PROXY__HOST=0.0.0.0 -ELECTRIC_SERVICE_URL=http://openchat-electric:3000 +ELECTRIC_SERVICE_URL=http://openchat-electric:3010 # --- API / Auth ------------------------------------------------------------- BETTER_AUTH_SECRET=replace-with-generated-secret