Backend (Go/Gin/pgx): env config, pgx pool with retries, Garage S3 client (path-style, region garage), embedded golang-migrate migrations, JWT auth with refresh rotation, albums CRUD, members/roles/invites with no-escalation matrix, photo upload + async imaging/goexif WebP variants, cursor pagination, presigned URLs, likes/comments, SSE hub (album + personal), notifications, share links. Frontend (SvelteKit 2 + Svelte 5 + Tailwind v4): API wrapper with 401 refresh, auth store, all screens, Uploader/PhotoGrid/Lightbox/Comments, SSE client with reconnect + REST catch-up, three themes + system via data-theme. Deploy without Docker: Makefile builds the frontend into backend/static and embeds it (go:embed) into a single statically linked binary; systemd unit and nginx SSE example included. Use gen2brain/webp (-tags nodynamic) instead of cgo kolesa-team/go-webp to keep CGO_ENABLED=0 static builds; design.md/proposal.md updated accordingly.
107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
package members
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"shared-album/internal/auth"
|
|
"shared-album/internal/httpx"
|
|
)
|
|
|
|
type Handler struct {
|
|
svc *Service
|
|
}
|
|
|
|
func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} }
|
|
|
|
func (h *Handler) List(c *gin.Context) {
|
|
list, err := h.svc.List(c.Request.Context(), c.Param("id"), auth.UserID(c))
|
|
if err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"members": list})
|
|
}
|
|
|
|
type inviteRequest struct {
|
|
Role string `json:"role" binding:"required"`
|
|
ExpiresIn string `json:"expires_in"`
|
|
MaxUses int `json:"max_uses"`
|
|
}
|
|
|
|
func (h *Handler) CreateInvite(c *gin.Context) {
|
|
var req inviteRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.RespondError(c, httpx.BadRequest("invalid_request", "role is required"))
|
|
return
|
|
}
|
|
var ttl time.Duration
|
|
if req.ExpiresIn != "" {
|
|
d, err := time.ParseDuration(req.ExpiresIn)
|
|
if err != nil {
|
|
httpx.RespondError(c, httpx.BadRequest("invalid_expires_in", "invalid expires_in duration"))
|
|
return
|
|
}
|
|
ttl = d
|
|
}
|
|
inv, err := h.svc.CreateInvite(c.Request.Context(), c.Param("id"), auth.UserID(c), CreateInviteInput{
|
|
Role: req.Role,
|
|
TTL: ttl,
|
|
MaxUses: req.MaxUses,
|
|
})
|
|
if err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"invite": inv})
|
|
}
|
|
|
|
func (h *Handler) AcceptInvite(c *gin.Context) {
|
|
inv, err := h.svc.Accept(c.Request.Context(), c.Param("token"), auth.UserID(c))
|
|
if err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"album_id": inv.AlbumID, "role": inv.Role})
|
|
}
|
|
|
|
func (h *Handler) InviteInfo(c *gin.Context) {
|
|
inv, err := h.svc.InviteInfo(c.Request.Context(), c.Param("token"))
|
|
if err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"invite": gin.H{
|
|
"album_id": inv.AlbumID,
|
|
"role": inv.Role,
|
|
"expires_at": inv.ExpiresAt,
|
|
}})
|
|
}
|
|
|
|
type roleRequest struct {
|
|
Role string `json:"role" binding:"required"`
|
|
}
|
|
|
|
func (h *Handler) ChangeRole(c *gin.Context) {
|
|
var req roleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.RespondError(c, httpx.BadRequest("invalid_request", "role is required"))
|
|
return
|
|
}
|
|
if err := h.svc.ChangeRole(c.Request.Context(), c.Param("id"), auth.UserID(c), c.Param("user_id"), req.Role); err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *Handler) Remove(c *gin.Context) {
|
|
if err := h.svc.Remove(c.Request.Context(), c.Param("id"), auth.UserID(c), c.Param("user_id")); err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|