Gopher Social is a backend‑focused sample social media API implemented in Go (Gin + pgx). It demonstrates layered architecture (handlers → services/stores → PostgreSQL), migrations, seeding, role-based access control, and OpenAPI (Swagger) documentation. A minimal Next.js client scaffold exists but full frontend work is intentionally deferred.
- User registration & activation (token / invitation flow)
- Role-based access control (
user,moderator,adminroles) - Posts with tags, comments, and ownership/role checks
- Structured error responses
- Database migrations & deterministic seeding
- Swagger documentation endpoint (
/swagger/*any)
- Go / Gin
- PostgreSQL (pgx driver & connection pool)
- Swaggo (OpenAPI generation)
- Next.js (placeholder in
web/)
.
├── internal/
│ ├── config/ # Configuration loading
│ ├── db/ # DB connection + seeding
│ ├── handler/ # HTTP handlers & middleware
│ ├── store/ # Data access layer (users, posts, comments, roles)
│ └── errors/ # Error helpers
├── migrate/migrations # SQL migration files
├── web/ # Placeholder Next.js app (frontend TODO)
├── docs/ # Generated Swagger (after running `swag init`)
└── main.go # App entrypoint
Configuration is loaded via environment variables (see internal/config). Common variables:
| Variable | Description | Example |
|---|---|---|
DATABASE_URL |
Postgres connection string | postgres://user:pass@localhost:5432/gopher_social?sslmode=disable |
ADDR |
Server listen address | :8080 |
ENV |
Environment (dev, prod, etc.) | dev |
BASIC_AUTH_USER / BASIC_AUTH_PASS |
Basic auth credentials (if used) | |
| Auth / JWT vars | Secrets / expiration settings | (configure as needed) |
DATABASE_URL=postgres://postgres:postgres@localhost:5432/gopher_social?sslmode=disable
ADDR=:8080
ENV=dev
BASIC_AUTH_USER=admin
BASIC_AUTH_PASS=changeme
- Install dependencies (Go modules auto-resolve on build).
- Ensure PostgreSQL is running and accessible.
- Export or create your
.envvalues. - Apply migrations.
- (Optional) Seed data.
- Run the server.
make migrate-upThis creates the core tables (users, posts, comments, roles, etc.).
go run migrate/seed/main.goThe seeder inserts users, posts, comments, and assigns role_id = 1 (expects a seeded user role). Adjust if your role IDs differ.
make run
# or
go run .Server listens on ADDR (default :8080).
Generate docs (only needed after changing annotations):
swag initThen visit: http://localhost:8080/swagger/index.html
The web/ directory contains a minimal Next.js scaffold. You can ignore it for backend-focused development.
cd web
npm install
npm run devRuns at http://localhost:3000.
| Command | Purpose |
|---|---|
make migrate-up |
Apply migrations |
make run |
Run the backend server |
make seed |
(If defined) run seeding logic |
| Symptom / Error | Cause / Fix |
|---|---|
multiple default values specified (migrations) |
Avoid BIGSERIAL + explicit DEFAULT; use BIGINT or separate steps. |
null value in column "role_id" during seeding |
Roles not seeded yet; ensure roles migration ran and role id matches seed expectation. |
cannot scan bytea into *string |
Password column is bytea; model expects hashed bytes. Update struct types accordingly. |
Swagger UI 500 fetching doc.json |
Regenerate with swag init; ensure docs import path matches module. |
| Unauthorized or forbidden on modifying a post | Check JWT / context user; ownership or role precedence may block. |
- Prefer small, focused PRs.
- Keep SQL migrations additive (avoid destructive changes without a plan).
- Run
swag initafter adding or modifying route annotations.