babillard/handlers/contenu.go
Victor Lacasse-Beaudoin a3c2c8d1f0 change!: remove config.Config dependency injection in ContenuHandler
`ContenuHandler` now only stores a `ContenuDir string` instead of a
whole config.

BREAKING: change `handlers.RunServer` parameters to `contenuDir string, serverPort int`
2023-12-04 21:23:25 -05:00

46 lines
981 B
Go

package handlers
import (
"fmt"
"log"
"net/http"
"strings"
"git.agecem.com/agecem/babillard/data"
"github.com/labstack/echo/v4"
)
type ContenuHandler struct {
ContenuDir string
}
func NewContenuHandler(contenuDir string) (h ContenuHandler) {
h.ContenuDir = contenuDir
return
}
func (h ContenuHandler) HandleAPIContenuList(c echo.Context) error {
contenu_dir := h.ContenuDir
files, err := data.ListContenu(contenu_dir)
if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal Server Error",
"error": err.Error(),
})
}
return c.String(http.StatusOK, strings.Join(files, ";"))
}
func (h ContenuHandler) HandleAPIContenuFile(c echo.Context) error {
filename := c.Param("filename")
contenu_dir := h.ContenuDir
if filename == ".gitkeep" {
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
}
return c.File(fmt.Sprintf("%s/%s", contenu_dir, filename))
}