babillard/handlers/handlers.go

57 lines
1.5 KiB
Go
Raw Permalink Normal View History

package handlers
import (
"fmt"
"log"
"net/http"
"git.agecem.com/agecem/babillard/config"
"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(cfg config.Config) {
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(cfg)
groupAPI.GET("/", HandleAPIShow)
groupAPI.GET("/contenu/", contenuHandler.HandleAPIContenuList)
groupAPI.GET("/contenu/:filename/", contenuHandler.HandleAPIContenuFile)
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", cfg.ServerPort)))
}