Backend implementation
This commit is contained in:
parent
3ebe2cbf47
commit
0c4453500f
40
backend/Dockerfile
Normal file
40
backend/Dockerfile
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
# ---- build stage ----
|
||||||
|
FROM golang:1.25.1-alpine AS builder
|
||||||
|
WORKDIR /src
|
||||||
|
|
||||||
|
# Speed up builds by caching deps
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
# Copy the rest (includes your embedded SQL in db/migration/*.sql)
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Build statically (no CGO) for Linux
|
||||||
|
ARG TARGETOS=linux
|
||||||
|
ARG TARGETARCH
|
||||||
|
ENV CGO_ENABLED=0
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||||
|
GOOS=$TARGETOS GOARCH=${TARGETARCH:-amd64} \
|
||||||
|
go build -trimpath -ldflags "-s -w" -o /out/server ./cmd/server
|
||||||
|
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||||
|
GOOS=$TARGETOS GOARCH=${TARGETARCH:-amd64} \
|
||||||
|
go build -trimpath -ldflags "-s -w" -o /out/migrate ./cmd/migrate
|
||||||
|
|
||||||
|
# ---- runtime stage ----
|
||||||
|
FROM alpine:3.20
|
||||||
|
# minimal tools for healthcheck + TLS roots + timezone
|
||||||
|
RUN apk add --no-cache ca-certificates tzdata curl && \
|
||||||
|
adduser -D -H -u 10001 app
|
||||||
|
USER app
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=builder /out/server /app/server
|
||||||
|
COPY --from=builder /out/migrate /app/migrate
|
||||||
|
|
||||||
|
EXPOSE 8082
|
||||||
|
# Container-local healthcheck
|
||||||
|
HEALTHCHECK --interval=15s --timeout=3s --retries=3 \
|
||||||
|
CMD curl -sf http://localhost:8082/healthz || exit 1
|
||||||
|
|
||||||
|
ENTRYPOINT ["/app/server"]
|
||||||
19
backend/cmd/migrate/main.go
Normal file
19
backend/cmd/migrate/main.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"watch-party-backend/internal/config"
|
||||||
|
"watch-party-backend/internal/migrate"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Load env (.env supported) and run migrations
|
||||||
|
cfg := config.Load()
|
||||||
|
|
||||||
|
if err := migrate.Run(context.Background(), cfg); err != nil {
|
||||||
|
log.Fatalf("migrations failed: %v", err)
|
||||||
|
}
|
||||||
|
log.Println("migrations completed")
|
||||||
|
}
|
||||||
50
backend/cmd/server/main.go
Normal file
50
backend/cmd/server/main.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"watch-party-backend/internal/config"
|
||||||
|
"watch-party-backend/internal/db"
|
||||||
|
httpapi "watch-party-backend/internal/http"
|
||||||
|
"watch-party-backend/internal/repo"
|
||||||
|
"watch-party-backend/internal/service"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// 1) config
|
||||||
|
cfg := config.Load()
|
||||||
|
gin.SetMode(cfg.GinMode)
|
||||||
|
|
||||||
|
// 2) DB pool
|
||||||
|
ctx := context.Background()
|
||||||
|
pool, err := db.NewPool(ctx, cfg.DB.DSN(), cfg.DB.MaxConns)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("db connect failed: %v", err)
|
||||||
|
}
|
||||||
|
defer pool.Close()
|
||||||
|
|
||||||
|
// 3) wiring
|
||||||
|
episodeRepo := repo.NewEpisodeRepo(pool)
|
||||||
|
episodeSvc := service.NewEpisodeService(episodeRepo)
|
||||||
|
router := httpapi.NewRouter(episodeSvc)
|
||||||
|
|
||||||
|
// 4) HTTP server with timeouts
|
||||||
|
s := &http.Server{
|
||||||
|
Addr: cfg.Addr,
|
||||||
|
Handler: router,
|
||||||
|
ReadTimeout: 10 * time.Second,
|
||||||
|
ReadHeaderTimeout: 5 * time.Second,
|
||||||
|
WriteTimeout: 30 * time.Second,
|
||||||
|
IdleTimeout: 60 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("listening on %s (dsn: %s)", cfg.Addr, config.RedactDSN(cfg.DB.DSN()))
|
||||||
|
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
log.Fatalf("server error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
28
backend/db/migration/0001_init.sql
Normal file
28
backend/db/migration/0001_init.sql
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
-- Create the "current" table and a useful index
|
||||||
|
CREATE TABLE IF NOT EXISTS "current" (
|
||||||
|
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||||
|
ep_num INTEGER NOT NULL,
|
||||||
|
ep_title TEXT NOT NULL,
|
||||||
|
season_name TEXT NOT NULL,
|
||||||
|
start_time TIME NOT NULL,
|
||||||
|
playback_length INTERVAL NOT NULL,
|
||||||
|
current_ep BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
date_created TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS current_date_created_idx
|
||||||
|
ON "current" (date_created DESC);
|
||||||
|
|
||||||
|
-- Archive table (no identity; just store the id from "current")
|
||||||
|
CREATE TABLE IF NOT EXISTS current_archive (
|
||||||
|
id BIGINT NOT NULL,
|
||||||
|
ep_num INTEGER NOT NULL,
|
||||||
|
ep_title TEXT NOT NULL,
|
||||||
|
season_name TEXT NOT NULL,
|
||||||
|
start_time TIME NOT NULL,
|
||||||
|
playback_length INTERVAL NOT NULL,
|
||||||
|
current_ep BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
date_created TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
date_archived TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
CONSTRAINT current_archive_pkey PRIMARY KEY (id)
|
||||||
|
);
|
||||||
6
backend/db/migration/embed.go
Normal file
6
backend/db/migration/embed.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed *.sql
|
||||||
|
var FS embed.FS
|
||||||
46
backend/go.mod
Normal file
46
backend/go.mod
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
module watch-party-backend
|
||||||
|
|
||||||
|
go 1.25.1
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gin-gonic/gin v1.11.0
|
||||||
|
github.com/jackc/pgx/v5 v5.7.6
|
||||||
|
github.com/joho/godotenv v1.5.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/bytedance/sonic v1.14.0 // indirect
|
||||||
|
github.com/bytedance/sonic/loader v0.3.0 // indirect
|
||||||
|
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||||
|
github.com/gin-contrib/sse v1.1.0 // indirect
|
||||||
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
|
github.com/go-playground/validator/v10 v10.27.0 // indirect
|
||||||
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
|
github.com/goccy/go-yaml v1.18.0 // indirect
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||||
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||||
|
github.com/quic-go/qpack v0.5.1 // indirect
|
||||||
|
github.com/quic-go/quic-go v0.54.0 // indirect
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
|
github.com/ugorji/go/codec v1.3.0 // indirect
|
||||||
|
go.uber.org/mock v0.5.0 // indirect
|
||||||
|
golang.org/x/arch v0.20.0 // indirect
|
||||||
|
golang.org/x/crypto v0.40.0 // indirect
|
||||||
|
golang.org/x/mod v0.25.0 // indirect
|
||||||
|
golang.org/x/net v0.42.0 // indirect
|
||||||
|
golang.org/x/sync v0.16.0 // indirect
|
||||||
|
golang.org/x/sys v0.35.0 // indirect
|
||||||
|
golang.org/x/text v0.27.0 // indirect
|
||||||
|
golang.org/x/tools v0.34.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.36.9 // indirect
|
||||||
|
)
|
||||||
99
backend/go.sum
Normal file
99
backend/go.sum
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
|
||||||
|
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
|
||||||
|
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
|
||||||
|
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||||
|
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
|
||||||
|
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||||
|
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
|
||||||
|
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||||
|
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
|
||||||
|
github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||||
|
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
|
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
|
||||||
|
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
||||||
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
|
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||||
|
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||||
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||||
|
github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
|
||||||
|
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||||
|
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
|
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
|
||||||
|
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
|
||||||
|
github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg=
|
||||||
|
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
|
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
|
||||||
|
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||||
|
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
||||||
|
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
|
||||||
|
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
|
||||||
|
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
|
||||||
|
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
|
||||||
|
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
|
||||||
|
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
|
||||||
|
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||||
|
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
|
||||||
|
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
|
||||||
|
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
|
||||||
|
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||||
|
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
|
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
|
||||||
|
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
|
||||||
|
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
|
||||||
|
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
|
||||||
|
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
|
||||||
|
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
96
backend/internal/config/config.go
Normal file
96
backend/internal/config/config.go
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Addr string
|
||||||
|
GinMode string
|
||||||
|
DB DBConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
type DBConfig struct {
|
||||||
|
Host string
|
||||||
|
Port string // NOTE: you use POSTGRES_PORT for this
|
||||||
|
User string
|
||||||
|
Password string
|
||||||
|
Name string
|
||||||
|
SSLMode string
|
||||||
|
MaxConns int32
|
||||||
|
AppName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load() Config {
|
||||||
|
_ = godotenv.Load()
|
||||||
|
|
||||||
|
cfg := Config{
|
||||||
|
Addr: getenv("ADDR", ":8082"),
|
||||||
|
GinMode: getenv("GIN_MODE", "release"),
|
||||||
|
DB: DBConfig{
|
||||||
|
Host: must("PGHOST"),
|
||||||
|
Port: must("POSTGRES_PORT"),
|
||||||
|
User: must("POSTGRES_USER"),
|
||||||
|
Password: os.Getenv("POSTGRES_PASSWORD"),
|
||||||
|
Name: must("POSTGRES_DB"),
|
||||||
|
SSLMode: getenv("PGSSLMODE", "disable"),
|
||||||
|
AppName: getenv("DB_APP_NAME", "current-api"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if n := getenv("DB_MAX_CONNS", ""); n != "" {
|
||||||
|
if v, err := strconv.Atoi(n); err == nil && v > 0 {
|
||||||
|
cfg.DB.MaxConns = int32(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d DBConfig) DSN() string {
|
||||||
|
// DATABASE_URL wins if present
|
||||||
|
if raw := os.Getenv("DATABASE_URL"); raw != "" {
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
u := &url.URL{
|
||||||
|
Scheme: "postgres",
|
||||||
|
User: url.UserPassword(d.User, d.Password),
|
||||||
|
Host: net.JoinHostPort(d.Host, d.Port),
|
||||||
|
Path: "/" + d.Name,
|
||||||
|
}
|
||||||
|
q := url.Values{}
|
||||||
|
q.Set("sslmode", d.SSLMode)
|
||||||
|
if d.AppName != "" {
|
||||||
|
q.Set("application_name", d.AppName)
|
||||||
|
}
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
return u.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func RedactDSN(raw string) string {
|
||||||
|
u, err := url.Parse(raw)
|
||||||
|
if err != nil || u.User == nil {
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
if _, ok := u.User.Password(); ok {
|
||||||
|
u.User = url.UserPassword(u.User.Username(), "*****")
|
||||||
|
}
|
||||||
|
return u.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getenv(k, def string) string {
|
||||||
|
if v := os.Getenv(k); v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
func must(k string) string {
|
||||||
|
v := os.Getenv(k)
|
||||||
|
if v == "" {
|
||||||
|
panic("missing required env: " + k)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
31
backend/internal/db/postgres.go
Normal file
31
backend/internal/db/postgres.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewPool(ctx context.Context, dsn string, maxConns int32) (*pgxpool.Pool, error) {
|
||||||
|
cfg, err := pgxpool.ParseConfig(dsn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if maxConns > 0 {
|
||||||
|
cfg.MaxConns = maxConns
|
||||||
|
}
|
||||||
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fail fast if unreachable
|
||||||
|
pctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := pool.Ping(pctx); err != nil {
|
||||||
|
pool.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return pool, nil
|
||||||
|
}
|
||||||
34
backend/internal/http/handlers.go
Normal file
34
backend/internal/http/handlers.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package httpapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"watch-party-backend/internal/repo"
|
||||||
|
"watch-party-backend/internal/service"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewRouter(svc service.EpisodeService) *gin.Engine {
|
||||||
|
r := gin.New()
|
||||||
|
r.Use(gin.Logger(), gin.Recovery())
|
||||||
|
|
||||||
|
r.GET("/healthz", func(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/current", func(c *gin.Context) {
|
||||||
|
cur, err := svc.GetCurrent(c.Request.Context())
|
||||||
|
if err != nil {
|
||||||
|
if err == repo.ErrNotFound {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "no current row found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "query failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, cur)
|
||||||
|
})
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
183
backend/internal/migrate/runner.go
Normal file
183
backend/internal/migrate/runner.go
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
package migrate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"log"
|
||||||
|
"net/url"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
|
|
||||||
|
mig "watch-party-backend/db/migration"
|
||||||
|
"watch-party-backend/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Run applies all pending migrations.
|
||||||
|
// Uses cfg.DB.DSN() for the app database and derives an admin DSN (db=postgres) from it.
|
||||||
|
func Run(ctx context.Context, cfg config.Config) error {
|
||||||
|
appDSN := cfg.DB.DSN()
|
||||||
|
adminDSN := dsnWithDB(appDSN, "postgres")
|
||||||
|
dbName := cfg.DB.Name
|
||||||
|
|
||||||
|
// 1) Connect to admin DB to ensure target DB exists
|
||||||
|
adminCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
adminConn, err := pgx.Connect(adminCtx, adminDSN)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect admin DB: %w", err)
|
||||||
|
}
|
||||||
|
defer adminConn.Close(ctx)
|
||||||
|
|
||||||
|
if err := createDatabaseIfNotExists(ctx, adminConn, dbName); err != nil {
|
||||||
|
return fmt.Errorf("create database: %w", err)
|
||||||
|
}
|
||||||
|
log.Printf("database %q is present", dbName)
|
||||||
|
|
||||||
|
// 2) Connect to target DB and run migrations
|
||||||
|
appCtx, cancel2 := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
defer cancel2()
|
||||||
|
|
||||||
|
appConn, err := pgx.Connect(appCtx, appDSN)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect app DB: %w", err)
|
||||||
|
}
|
||||||
|
defer appConn.Close(ctx)
|
||||||
|
|
||||||
|
if err := ensureSchemaMigrations(ctx, appConn); err != nil {
|
||||||
|
return fmt.Errorf("ensure schema_migrations: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
applied, err := fetchAppliedVersions(ctx, appConn)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read applied versions: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := fs.Glob(mig.FS, "*.sql")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("glob migrations: %w", err)
|
||||||
|
}
|
||||||
|
sort.Strings(files)
|
||||||
|
|
||||||
|
for _, name := range files {
|
||||||
|
version := versionOf(name)
|
||||||
|
if applied[version] {
|
||||||
|
log.Printf("skip %s (already applied)", version)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlBytes, err := mig.FS.ReadFile(name)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read %s: %w", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("applying %s ...", version)
|
||||||
|
if err := applyMigration(ctx, appConn, version, string(sqlBytes)); err != nil {
|
||||||
|
return fmt.Errorf("apply %s failed: %w", version, err)
|
||||||
|
}
|
||||||
|
log.Printf("applied %s", version)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// dsnWithDB parses a Postgres URL DSN and replaces the DB path segment.
|
||||||
|
// Example: postgres://user:pass@host:5432/appdb?sslmode=disable -> .../postgres?sslmode=disable
|
||||||
|
func dsnWithDB(raw string, dbName string) string {
|
||||||
|
u, err := url.Parse(raw)
|
||||||
|
if err != nil {
|
||||||
|
// if malformed, just return raw; pgx will error at connect time
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
u.Path = "/" + dbName
|
||||||
|
return u.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDatabaseIfNotExists(ctx context.Context, admin *pgx.Conn, dbName string) error {
|
||||||
|
var exists bool
|
||||||
|
if err := admin.QueryRow(ctx,
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname=$1)", dbName).
|
||||||
|
Scan(&exists); err != nil {
|
||||||
|
return fmt.Errorf("check db exists: %w", err)
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Quote identifier safely via quote_ident
|
||||||
|
_, err := admin.Exec(ctx, `
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (SELECT FROM pg_database WHERE datname = $1) THEN
|
||||||
|
EXECUTE 'CREATE DATABASE ' || quote_ident($1);
|
||||||
|
END IF;
|
||||||
|
END$$;`, dbName)
|
||||||
|
if err != nil {
|
||||||
|
// 42P04: duplicate_database
|
||||||
|
if pgErr, ok := err.(*pgconn.PgError); ok && pgErr.Code == "42P04" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("create database: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureSchemaMigrations(ctx context.Context, conn *pgx.Conn) error {
|
||||||
|
_, err := conn.Exec(ctx, `
|
||||||
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||||
|
version TEXT PRIMARY KEY,
|
||||||
|
applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
)`)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchAppliedVersions(ctx context.Context, conn *pgx.Conn) (map[string]bool, error) {
|
||||||
|
rows, err := conn.Query(ctx, `SELECT version FROM schema_migrations`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
out := make(map[string]bool)
|
||||||
|
for rows.Next() {
|
||||||
|
var v string
|
||||||
|
if err := rows.Scan(&v); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out[v] = true
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyMigration(ctx context.Context, conn *pgx.Conn, version, sqlText string) error {
|
||||||
|
tx, err := conn.Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() { _ = tx.Rollback(ctx) }()
|
||||||
|
|
||||||
|
if _, err := tx.Exec(ctx, sqlText); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := tx.Exec(ctx, `INSERT INTO schema_migrations(version) VALUES ($1)`, version); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Commit(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func versionOf(path string) string {
|
||||||
|
base := path[strings.LastIndex(path, "/")+1:]
|
||||||
|
// "0001_init.sql" -> "0001"
|
||||||
|
dot := strings.Index(base, ".")
|
||||||
|
if underscore := strings.Index(base, "_"); underscore > 0 && underscore < dot {
|
||||||
|
return base[:underscore]
|
||||||
|
}
|
||||||
|
if dot > 0 {
|
||||||
|
return base[:dot]
|
||||||
|
}
|
||||||
|
return base
|
||||||
|
}
|
||||||
60
backend/internal/repo/episode_repo.go
Normal file
60
backend/internal/repo/episode_repo.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrNotFound = errors.New("not found")
|
||||||
|
|
||||||
|
type Episode struct {
|
||||||
|
EpNum int `json:"ep_num"`
|
||||||
|
EpTitle string `json:"ep_title"`
|
||||||
|
SeasonName string `json:"season_name"`
|
||||||
|
StartTime string `json:"start_time"`
|
||||||
|
PlaybackLength string `json:"playback_length"`
|
||||||
|
DateCreated time.Time `json:"date_created"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type EpisodeRepository interface {
|
||||||
|
GetCurrent(ctx context.Context) (Episode, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type pgxEpisodeRepo struct {
|
||||||
|
pool *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEpisodeRepo(pool *pgxpool.Pool) EpisodeRepository {
|
||||||
|
return &pgxEpisodeRepo{pool: pool}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *pgxEpisodeRepo) GetCurrent(ctx context.Context) (Episode, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT
|
||||||
|
ep_num,
|
||||||
|
ep_title,
|
||||||
|
season_name,
|
||||||
|
to_char(start_time, 'HH24:MI:SS') AS start_time,
|
||||||
|
to_char(playback_length, 'HH24:MI:SS') AS playback_length,
|
||||||
|
date_created
|
||||||
|
FROM current
|
||||||
|
WHERE current_ep = true
|
||||||
|
ORDER BY id DESC
|
||||||
|
LIMIT 1;
|
||||||
|
`
|
||||||
|
var e Episode
|
||||||
|
err := r.pool.QueryRow(ctx, q).Scan(
|
||||||
|
&e.EpNum, &e.EpTitle, &e.SeasonName, &e.StartTime, &e.PlaybackLength, &e.DateCreated,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return Episode{}, ErrNotFound
|
||||||
|
}
|
||||||
|
return Episode{}, err
|
||||||
|
}
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
27
backend/internal/service/episode_service.go
Normal file
27
backend/internal/service/episode_service.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"watch-party-backend/internal/repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EpisodeService interface {
|
||||||
|
GetCurrent(ctx context.Context) (repo.Episode, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type episodeService struct {
|
||||||
|
repo repo.EpisodeRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEpisodeService(r repo.EpisodeRepository) EpisodeService {
|
||||||
|
return &episodeService{repo: r}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *episodeService) GetCurrent(ctx context.Context) (repo.Episode, error) {
|
||||||
|
// Add cross-cutting concerns here (timeouts, metrics, caching, etc.)
|
||||||
|
c, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
return s.repo.GetCurrent(c)
|
||||||
|
}
|
||||||
32
backend/internal/service/episode_service_test.go
Normal file
32
backend/internal/service/episode_service_test.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package service_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"watch-party-backend/internal/repo"
|
||||||
|
"watch-party-backend/internal/service"
|
||||||
|
)
|
||||||
|
|
||||||
|
type stubRepo struct {
|
||||||
|
current repo.Episode
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s stubRepo) GetCurrent(ctx context.Context) (repo.Episode, error) {
|
||||||
|
return s.current, s.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEpisodeService_GetCurrent(t *testing.T) {
|
||||||
|
want := repo.Episode{EpNum: 1, EpTitle: "Ep1", SeasonName: "S1", StartTime: "00:00:00", PlaybackLength: "00:24:00", DateCreated: time.Now()}
|
||||||
|
svc := service.NewEpisodeService(stubRepo{current: want})
|
||||||
|
|
||||||
|
got, err := svc.GetCurrent(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if got.EpNum != want.EpNum || got.EpTitle != want.EpTitle {
|
||||||
|
t.Fatalf("mismatch: got %+v want %+v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user