Victor Lacasse-Beaudoin
a3c2c8d1f0
`ContenuHandler` now only stores a `ContenuDir string` instead of a whole config. BREAKING: change `handlers.RunServer` parameters to `contenuDir string, serverPort int`
55 lines
1.5 KiB
Go
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)))
|
|
}
|