package server import ( "net/http" "music-server/internal/backend" "github.com/labstack/echo/v5" ) type DownloadHandler struct { releases *backend.ReleaseService } func NewDownloadHandler(releases *backend.ReleaseService) *DownloadHandler { return &DownloadHandler{releases: releases} } // CheckLatest godoc // @Summary Check for latest version // @Description Returns the latest version of the client application // @Tags download // @Produce json // @Success 200 {string} string // @Failure 502 {string} string "upstream service unavailable" // @Router /download [get] func (d *DownloadHandler) CheckLatest(ctx *echo.Context) error { latest, err := d.releases.LatestVersion(ctx.Request().Context()) if err != nil { return songError(err) } return ctx.JSON(http.StatusOK, latest) } // ListAssetsOfLatest godoc // @Summary List assets of latest version // @Description Lists all assets available for the latest version // @Tags download // @Produce json // @Success 200 {array} string // @Failure 502 {string} string "upstream service unavailable" // @Router /download/list [get] func (d *DownloadHandler) ListAssetsOfLatest(ctx *echo.Context) error { assets, err := d.releases.AssetNames(ctx.Request().Context()) if err != nil { return songError(err) } return ctx.JSON(http.StatusOK, assets) } // DownloadLatestWindows godoc // @Summary Download latest Windows version // @Description Redirects to download the latest Windows version // @Tags download // @Success 302 {string} string // @Failure 404 {string} string "not found" // @Failure 502 {string} string "upstream service unavailable" // @Router /download/windows [get] func (d *DownloadHandler) DownloadLatestWindows(ctx *echo.Context) error { return d.redirectToAsset(ctx, ".exe") } // DownloadLatestLinux godoc // @Summary Download latest Linux version // @Description Redirects to download the latest Linux version // @Tags download // @Success 302 {string} string // @Failure 404 {string} string "not found" // @Failure 502 {string} string "upstream service unavailable" // @Router /download/linux [get] func (d *DownloadHandler) DownloadLatestLinux(ctx *echo.Context) error { return d.redirectToAsset(ctx, ".x86_64") } func (d *DownloadHandler) redirectToAsset(ctx *echo.Context, suffix string) error { url, err := d.releases.AssetURL(ctx.Request().Context(), suffix) if err != nil { return songError(err) } return ctx.Redirect(http.StatusFound, url) }