change!: commandline tool from serpents/cobra/viper to couleuvre

Ajouter `config.Config` pour stocker valeurs de flags

Migrer `cmd/` à couleuvre

Ajouter `cmd.Cfg()` pour retourner une copie courante de la configuration.

Cleanup dependencies avec `go get` et `go mod tidy`

BREAKING: renommer flags
BREAKING: déplacer `cmd.runServer` à `handlers.RunServer(cfg config.Config)`
BREAKING: changer backend à couleuvre, qui gère différemment le commandline
This commit is contained in:
Victor Lacasse-Beaudoin 2023-12-04 19:05:56 -05:00
parent cdcd10ba1e
commit 605206197a
9 changed files with 140 additions and 227 deletions

View file

@ -6,13 +6,22 @@ import (
"net/http"
"strings"
"git.agecem.com/agecem/babillard/config"
"git.agecem.com/agecem/babillard/data"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
)
func HandleAPIContenuList(c echo.Context) error {
contenu_dir := viper.GetString("server.contenu_dir")
type ContenuHandler struct {
Cfg config.Config
}
func NewContenuHandler(cfg config.Config) (h ContenuHandler) {
h.Cfg = cfg
return
}
func (h ContenuHandler) HandleAPIContenuList(c echo.Context) error {
contenu_dir := h.Cfg.ServerContenuDir
files, err := data.ListContenu(contenu_dir)
if err != nil {
@ -26,9 +35,9 @@ func HandleAPIContenuList(c echo.Context) error {
return c.String(http.StatusOK, strings.Join(files, ";"))
}
func HandleAPIContenuFile(c echo.Context) error {
func (h ContenuHandler) HandleAPIContenuFile(c echo.Context) error {
filename := c.Param("filename")
contenu_dir := viper.GetString("server.contenu_dir")
contenu_dir := h.Cfg.ServerContenuDir
if filename == ".gitkeep" {
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})

View file

@ -1,9 +1,14 @@
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 {
@ -18,3 +23,34 @@ API Specifications
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)))
}