How tantin135777/a-web-forum works
This is a small full-stack web application: a web forum with topics → posts → comments plus username-only authentication. Architecturally it’s a classic SPA frontend (React/TypeScript) calling a Go HTTP API that persists to a PostgreSQL database (Neon hosted). Sessions are managed via HTTP-only cookies, and write operations require auth (cookie session or X-User header), while reads are public.
• Browser + SPA boundary and routing/pages
• API surface and auth rules (public GET vs authenticated writes)
• Session/cookie flow
• Database + migrations
• Environments (dev ports, Vite proxying via VITE_API_BASE, Neon remote DB)
From the file tree:
Backend (Go)
• main.go → HTTP server entrypoint: routes/handlers, middleware (likely auth/session), listens on :8080.
• db.go → DB connection pool (Postgres). This is the backend’s persistence adapter.
• migrations/0001_init.sql → schema definition for topics/posts/comments/users/sessions (or similar).
• go.mod, go.sum → dependency graph (helps annotate: net/http, postgres driver, migration tooling if any).
Frontend (React + TS via Vite)
• frontend/src/main.tsx → SPA bootstrap.
• frontend/src/App.tsx → top-level router/layout composition.
• frontend/src/pages/* → page-level UI: HomePage, LoginPage, TopicPage, PostPage (these align well to user journeys).
• frontend/src/api/client.ts → single API client abstraction (base URL VITE_API_BASE).
• frontend/src/components/Layout.tsx → shared UI chrome/navigation.
• frontend/src/types.ts → shared data contracts (topics/posts/comments/user).
• frontend/vite.config.ts, frontend/.env.local → runtime configuration / API base.
External dependency
• Neon Postgres (hosted). Important: DB is not local; it’s an external cloud service.
This structure strongly suggests a 3-tier picture (Client ↔ API ↔ DB) with a few important sub-components (Auth/session, migrations, API client).
What to include from README (constraints and runtime behavior)
• Frontend: http://localhost:5173 (Vite dev server)
• Backend: http://localhost:8080
• API base: configured by VITE_API_BASE
• Auth:
• Login with username; user auto-created
• Session via HTTP-only cookie, SameSite=Lax
• Write ops require auth (cookie or X-User)
• Read ops (GET) public
• Data model actions:
• Topics: create/edit/delete (any logged-in user)
• Posts/comments: only authors can edit/delete
• DB is Neon hosted (external boundary)
React SPA
• API client (src/api/client.ts) as an internal module
Go API Server
• HTTP routing/handlers (main.go)
• DB access layer (db.go)
Postgres (Neon)
• Schema managed by migrations/*.sql
Relationships:
• SPA uses api/client.ts → calls API endpoints
• API server validates session/auth → executes CRUD → DB
Goal: express request lifecycle and auth rules.
• HTTP Router / Handlers (from main.go)
• Endpoints by resource:
• /login (or similar) → sets session cookie
• /topics → list/create/update/delete
• /topics/{id}/posts or /posts → create/list
• /posts/{id}/comments or /comments → create/list
• Auth module
• Session validation from cookie
• Alternative auth via X-User
• Authorization checks (topic editable by any logged-in; post/comment only by author)
• DB Pool (db.go)
• “Request → auth check → handler → SQL → response”
• “Login → create/find user → create session → Set-Cookie”
Inside React SPA:
• App shell (App.tsx, Layout.tsx)
• Pages (Home/Login/Topic/Post)
• API client (api/client.ts)
• Types/contracts (types.ts)
Data flow:
• Page → API client → fetch JSON → render
• Login page triggers login call; browser stores cookie automatically; subsequent writes include cookie
Explicitly annotate: “Cookie is HttpOnly, not readable by JS; browser attaches automatically.”
“Don’t overthink it”: maximize component separation (but keep it accurate)
To be very detailed for this repo without inventing nonexistent services, split into many components *within the two main processes*:
• Frontend: App shell, Pages, API client, Types, Styling/Theme
• Backend: Router, Handlers by resource, Auth/session middleware, Authorization policy, DB pool, SQL queries, Migration artifacts
• External: Neon Postgres
C4 Container (1 page): Vite SPA, Go API, DB, Migrations, Env config
3. Backend Component (1 page): Router/handlers, auth/session, authorization rules, DB pool, entities
4. (Optional) Frontend Component (1 page): pages → api client → backend, cookie behavior note