babillard/handlers/handlers.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

55 lines
1.5 KiB
Go

package handlers
import (
"fmt"
"log"
"net/http"
"git.agecem.com/agecem/babillard/public"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func HandleAPIShow(c echo.Context) error {
apispec := `agecem/babillard
API Specifications
-----
'/' | GET | Affiche l'application 'slider'
'/api' | GET | Affiche les spécifications API
'/api/contenu' | GET | Affiche le nom des fichiers dans 'contenu/'
'/api/contenu/{filename}' | GET | Affiche le fichier 'contenu/{filename}'
-----`
return c.String(http.StatusOK, apispec)
}
func RunServer(contenuDir string, serverPort int) {
log.Print("[I] Starting webserver")
e := echo.New()
e.Pre(middleware.AddTrailingSlash())
e.Group("").Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "/html/",
Filesystem: http.FS(public.HTMLFS()),
}))
e.Group("/public/css").Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "/css/",
Filesystem: http.FS(public.CSSFS()),
}))
e.Group("/public/js").Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "/js/",
Filesystem: http.FS(public.JSFS()),
}))
groupAPI := e.Group("/api")
contenuHandler := NewContenuHandler(contenuDir)
groupAPI.GET("/", HandleAPIShow)
groupAPI.GET("/contenu/", contenuHandler.HandleAPIContenuList)
groupAPI.GET("/contenu/:filename/", contenuHandler.HandleAPIContenuFile)
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", serverPort)))
}