50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"music-server/internal/backend"
|
|
)
|
|
|
|
type CharacterHandler struct {
|
|
}
|
|
|
|
func NewCharacterHandler() *CharacterHandler {
|
|
return &CharacterHandler{}
|
|
}
|
|
|
|
// GetCharacterList godoc
|
|
// @Summary Get list of characters
|
|
// @Description Returns a list of all available characters
|
|
// @Tags characters
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {array} string
|
|
// @Router /characters [get]
|
|
func (c *CharacterHandler) GetCharacterList(ctx *echo.Context) error {
|
|
characters := backend.GetCharacterList()
|
|
return ctx.JSON(http.StatusOK, characters)
|
|
}
|
|
|
|
// GetCharacter godoc
|
|
// @Summary Get character image
|
|
// @Description Returns the image for a specific character
|
|
// @Tags characters
|
|
// @Accept json
|
|
// @Produce image/png
|
|
// @Param name query string true "Character name"
|
|
// @Success 200 {file} file
|
|
// @Router /character [get]
|
|
func (c *CharacterHandler) GetCharacter(ctx *echo.Context) error {
|
|
character := ctx.QueryParam("name")
|
|
characterPath := backend.GetCharacter(character)
|
|
file, err := os.Open(characterPath)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err.Error())
|
|
}
|
|
defer file.Close()
|
|
return ctx.Stream(http.StatusOK, "image/png", file)
|
|
}
|