Skip to content
Draft
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
2 changes: 2 additions & 0 deletions apps/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ COPY . .
# match the browser-visible URLs published by Docker Compose.
ARG VITE_API_URL=http://localhost:8080
ARG VITE_WS_URL=ws://localhost:8080/ws
ARG VITE_DEMO_MODE=false
ENV VITE_API_URL=${VITE_API_URL}
ENV VITE_WS_URL=${VITE_WS_URL}
ENV VITE_DEMO_MODE=${VITE_DEMO_MODE}
RUN npm run build

# ---- runtime stage -------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface AppEnv {
apiUrl: string;
/** WebSocket URL for the realtime stream, e.g. ws://localhost:8080/ws. */
wsUrl: string;
demoMode: boolean;
}

type RawEnv = Record<string, string | undefined>;
Expand Down Expand Up @@ -37,7 +38,7 @@ export function validateEnv(source: RawEnv): AppEnv {
throw new Error(`Invalid frontend environment:\n- ${errors.join("\n- ")}`);
}

return { apiUrl, wsUrl };
return { apiUrl, wsUrl, demoMode: source.VITE_DEMO_MODE === "true" };
}

function isUrlWithScheme(value: string, schemes: string[]): boolean {
Expand Down
37 changes: 32 additions & 5 deletions apps/web/src/pages/LoginPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { AuthProvider } from "@/app/AuthContext";
import { LoginPage } from "@/pages/LoginPage";

function renderLoginPage() {
async function renderLoginPage() {
const [{ AuthProvider }, { LoginPage }] = await Promise.all([
import("@/app/AuthContext"),
import("@/pages/LoginPage"),
]);
return render(
<AuthProvider>
<MemoryRouter initialEntries={["/login"]}>
Expand All @@ -20,12 +22,37 @@ function renderLoginPage() {

describe("LoginPage", () => {
beforeEach(() => {
vi.resetModules();
vi.stubEnv("VITE_DEMO_MODE", undefined);
vi.stubGlobal("fetch", vi.fn());
});
afterEach(() => {
vi.unstubAllEnvs();
vi.unstubAllGlobals();
});

it("shows the synthetic data banner in demo mode", async () => {
vi.stubEnv("VITE_DEMO_MODE", "true");

await renderLoginPage();

expect(screen.getByText("Synthetic data only")).toBeVisible();
});

it("omits the synthetic data banner when demo mode is unset", async () => {
await renderLoginPage();

expect(screen.queryByText("Synthetic data only")).not.toBeInTheDocument();
});

it("omits the synthetic data banner when demo mode is false", async () => {
vi.stubEnv("VITE_DEMO_MODE", "false");

await renderLoginPage();

expect(screen.queryByText("Synthetic data only")).not.toBeInTheDocument();
});

it("navigates to /dashboard on successful submit", async () => {
(fetch as ReturnType<typeof vi.fn>).mockResolvedValue({
ok: true,
Expand All @@ -35,7 +62,7 @@ describe("LoginPage", () => {
}),
});

renderLoginPage();
await renderLoginPage();
await userEvent.type(screen.getByLabelText(/email/i), "operator@example.com");
await userEvent.type(screen.getByLabelText(/password/i), "correct-password");
await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
Expand All @@ -49,7 +76,7 @@ describe("LoginPage", () => {
json: async () => ({ error: { code: "INVALID_CREDENTIALS", message: "email or password is incorrect" } }),
});

renderLoginPage();
await renderLoginPage();
await userEvent.type(screen.getByLabelText(/email/i), "operator@example.com");
await userEvent.type(screen.getByLabelText(/password/i), "wrong-password");
await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/pages/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, type FormEvent } from "react";
import { useNavigate } from "react-router-dom";
import { useAuth } from "@/app/AuthContext";
import { Button } from "@/components/ui/button";
import { env } from "@/lib/env";

/** LoginPage authenticates against POST /login and stores the token in memory. */
export function LoginPage() {
Expand Down Expand Up @@ -32,6 +33,11 @@ export function LoginPage() {
<h1 className="text-2xl font-semibold tracking-tight">Sign in</h1>
<p className="text-sm text-muted-foreground">FinWatch operator access.</p>
</div>
{env.demoMode && (
<p className="rounded-md border bg-muted px-3 py-2 text-center text-sm font-medium">
Synthetic data only
</p>
)}
<form className="space-y-4" onSubmit={handleSubmit} aria-label="Sign in">
<div className="space-y-1">
<label htmlFor="email" className="text-sm font-medium">
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

interface ImportMetaEnv {
readonly VITE_API_URL?: string;
readonly VITE_DEMO_MODE?: string;
readonly VITE_WS_URL?: string;
}

Expand Down
1 change: 1 addition & 0 deletions deploy/demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
demo.env
22 changes: 22 additions & 0 deletions deploy/demo/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# FinWatch public demo reverse proxy. Caddy obtains and renews the TLS
# certificate for $DEMO_DOMAIN automatically.
{$DEMO_DOMAIN} {
encode zstd gzip

header {
Strict-Transport-Security "max-age=31536000"
X-Content-Type-Options "nosniff"
Referrer-Policy "no-referrer"
X-Frame-Options "DENY"
-Server
}

# API and WebSocket: /api/login -> api:8080/login, /api/ws -> api:8080/ws.
handle_path /api/* {
reverse_proxy api:8080
}

handle {
reverse_proxy web:8080
}
}
28 changes: 28 additions & 0 deletions deploy/demo/demo.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# FinWatch public demo configuration.
#
# Copy to deploy/demo/demo.env (git-ignored) and fill in every value.
# All data in the demo is synthetic.

# Public hostname pointing (DNS A/AAAA record) at the demo host.
DEMO_DOMAIN=finwatch-demo.example.com

# Database credentials: internal to the host, never published.
# Generate with: openssl rand -hex 24
POSTGRES_USER=finwatch
POSTGRES_PASSWORD=
POSTGRES_DB=finwatch

# JWT signing secret: a fresh random value per deployment, at least 32 chars.
# The API refuses the published development example value outside development.
# Generate with: openssl rand -hex 32
JWT_SIGNING_SECRET=
JWT_ACCESS_TOKEN_TTL=15m

# Demo login passwords (operator@example.com / admin@example.com). These are
# shown to visitors so they can try the app; use throwaway values that are not
# reused anywhere else.
DEMO_OPERATOR_PASSWORD=
DEMO_ADMIN_PASSWORD=

# Number of synthetic transactions ingested on first start.
DEMO_SEED_COUNT=200
135 changes: 135 additions & 0 deletions deploy/demo/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# FinWatch public demo stack (single small host).
#
# Runs the same images as the local stack behind Caddy, which terminates TLS
# and serves the SPA and API from one origin:
#
# https://$DEMO_DOMAIN/ -> web (React SPA)
# https://$DEMO_DOMAIN/api/* -> api (prefix stripped; WebSocket at /api/ws)
#
# All data is synthetic. Configure via deploy/demo/demo.env (copied from
# demo.env.example) and start with:
#
# docker compose -f deploy/demo/docker-compose.yml --env-file deploy/demo/demo.env up -d --build
#
# See docs/operations/demo-deploy.md.

name: finwatch-demo

x-api-env: &api-env
APP_ENV: production
LOG_LEVEL: info
HTTP_HOST: 0.0.0.0
HTTP_PORT: "8080"
DATABASE_URL: postgres://${POSTGRES_USER:?}:${POSTGRES_PASSWORD:?}@db:5432/${POSTGRES_DB:?}?sslmode=disable
CORS_ALLOWED_ORIGINS: https://${DEMO_DOMAIN:?}
JWT_SIGNING_SECRET: ${JWT_SIGNING_SECRET:?}
JWT_ACCESS_TOKEN_TTL: ${JWT_ACCESS_TOKEN_TTL:-15m}
OUTBOX_POLL_INTERVAL: ${OUTBOX_POLL_INTERVAL:-500ms}
WS_MAX_CONNECTIONS_PER_USER: ${WS_MAX_CONNECTIONS_PER_USER:-5}
DEMO_OPERATOR_PASSWORD: ${DEMO_OPERATOR_PASSWORD:?}
DEMO_ADMIN_PASSWORD: ${DEMO_ADMIN_PASSWORD:?}

# The api image is built once from source and shared by the api service and
# the one-shot jobs below.
x-api-image: &api-image
build:
context: ../../apps/api
image: finwatch-demo-api

services:
db:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: ${POSTGRES_USER:?}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?}
POSTGRES_DB: ${POSTGRES_DB:?}
# No published port: only the api and one-shot jobs reach Postgres.
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s

# One-shot: apply versioned SQL migrations, then exit.
migrate:
image: migrate/migrate:v4.18.3
volumes:
- ../../apps/api/migrations:/migrations:ro
command:
- -path=/migrations
- -database=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
- up
depends_on:
db:
condition: service_healthy

# One-shot: create the demo operator/admin accounts (idempotent).
seed-users:
<<: *api-image
command: ["seed-users"]
environment: *api-env
depends_on:
migrate:
condition: service_completed_successfully

# One-shot: ingest an initial batch of synthetic transactions. Re-run it to
# simulate more traffic: `docker compose ... run --rm seed`.
seed:
<<: *api-image
command: ["seed", "-n", "${DEMO_SEED_COUNT:-200}"]
environment: *api-env
depends_on:
seed-users:
condition: service_completed_successfully

api:
<<: *api-image
restart: unless-stopped
environment: *api-env
depends_on:
seed-users:
condition: service_completed_successfully
healthcheck:
test: ["CMD", "/app/api", "healthcheck"]
interval: 10s
timeout: 5s
retries: 3
start_period: 5s

web:
build:
context: ../../apps/web
args:
VITE_API_URL: https://${DEMO_DOMAIN:?}/api
VITE_WS_URL: wss://${DEMO_DOMAIN:?}/api/ws
VITE_DEMO_MODE: "true"
restart: unless-stopped
depends_on:
api:
condition: service_started

caddy:
image: caddy:2.10-alpine
restart: unless-stopped
environment:
DEMO_DOMAIN: ${DEMO_DOMAIN:?}
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy_data:/data
- caddy_config:/config
depends_on:
- api
- web

volumes:
pgdata:
caddy_data:
caddy_config:
Loading