72 lines
2.6 KiB
Markdown
72 lines
2.6 KiB
Markdown
# Backend (Go + Gin)
|
|
|
|
REST API that serves the schedule/time endpoints and current episode control. Built in Go, containerized with a multi-stage Dockerfile.
|
|
|
|
## Prerequisites
|
|
- Go 1.22+
|
|
- PostgreSQL reachable (uses env-driven DSN pieces)
|
|
- `golang-migrate` not required locally (migrations shipped in repo)
|
|
|
|
## Env
|
|
The server builds its DSN from:
|
|
- `PGHOST`, `POSTGRES_PORT`, `POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_DB`, `PGSSLMODE`
|
|
- `ADDR` (listen address, default `:8082`)
|
|
- `GIN_MODE` (e.g., `release`)
|
|
- Auth (optional):
|
|
- `AUTH_ENABLED` (default `false`)
|
|
- `FIREBASE_PROJECT_ID`
|
|
- `FIREBASE_CREDENTIALS_JSON` (raw JSON or base64-encoded service account JSON) or `FIREBASE_CREDENTIALS_FILE` (path to JSON)
|
|
|
|
## Commands
|
|
```bash
|
|
# Run locally (expects Postgres per env)
|
|
go run ./cmd/server
|
|
|
|
# Fetch one dアニメ episode to stdout
|
|
go run ./cmd/danime-episode <partId>
|
|
|
|
# Run migrations
|
|
go run ./cmd/migrate
|
|
|
|
# Tests
|
|
go test ./...
|
|
|
|
# Swagger create docs
|
|
swag init -g cmd/server/main.go -o ./docs
|
|
```
|
|
|
|
## Docker build (used by compose)
|
|
```bash
|
|
docker build -t watchparty-backend .
|
|
```
|
|
Compose uses the same image for `api` and the one-off `migrate` job.
|
|
|
|
## Endpoints (key)
|
|
- `GET /api/v1/time` — server time for client clock sync
|
|
- `GET /api/v1/current` — current schedule item
|
|
- `POST /api/v1/current` — set current episode (expects `{ id, start_time? }`)
|
|
- `GET /api/v1/shows` — list of episodes
|
|
- `POST /api/v1/shows` — create a new episode (expects `{ ep_num, ep_title, season_name, start_time, playback_length }`)
|
|
- `GET /api/v1/danime?part_id=...` — scrape dアニメ episode metadata (returns `{ ep_num, ep_title, season_name, playback_length }`)
|
|
- `POST /api/v1/oauth/firebase` — verify a Firebase ID token and echo back UID/email
|
|
- `POST /api/v1/oauth/firebase/claim-admin` — set the `admin` custom claim for the UID represented by the provided ID token
|
|
- `DELETE /api/v1/shows?id=...` — delete a show (guarded by Firebase auth when `AUTH_ENABLED=true`)
|
|
- `GET /healthz` — health check
|
|
|
|
### Auth examples
|
|
```bash
|
|
# Verify Firebase ID token (AUTH_ENABLED=true)
|
|
curl -X POST http://localhost:8082/api/v1/oauth/firebase \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"id_token":"<firebase-id-token>"}'
|
|
|
|
# Claim Firebase admin for the current token's UID (AUTH_ENABLED=true)
|
|
curl -X POST http://localhost:8082/api/v1/oauth/firebase/claim-admin \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"id_token":"<firebase-id-token>"}'
|
|
|
|
# Delete a show with auth
|
|
curl -X DELETE "http://localhost:8082/api/v1/shows?id=123" \
|
|
-H "Authorization: Bearer <firebase-id-token>"
|
|
```
|