From a3c2c8d1f08d6f1bd5c8bf2bd40fc090668c5f3e Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 4 Dec 2023 21:23:25 -0500 Subject: [PATCH 1/3] 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` --- handlers/contenu.go | 11 +++++------ handlers/handlers.go | 7 +++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/handlers/contenu.go b/handlers/contenu.go index a64a7ed..0b8b553 100644 --- a/handlers/contenu.go +++ b/handlers/contenu.go @@ -6,22 +6,21 @@ import ( "net/http" "strings" - "git.agecem.com/agecem/babillard/config" "git.agecem.com/agecem/babillard/data" "github.com/labstack/echo/v4" ) type ContenuHandler struct { - Cfg config.Config + ContenuDir string } -func NewContenuHandler(cfg config.Config) (h ContenuHandler) { - h.Cfg = cfg +func NewContenuHandler(contenuDir string) (h ContenuHandler) { + h.ContenuDir = contenuDir return } func (h ContenuHandler) HandleAPIContenuList(c echo.Context) error { - contenu_dir := h.Cfg.ServerContenuDir + contenu_dir := h.ContenuDir files, err := data.ListContenu(contenu_dir) if err != nil { @@ -37,7 +36,7 @@ func (h ContenuHandler) HandleAPIContenuList(c echo.Context) error { func (h ContenuHandler) HandleAPIContenuFile(c echo.Context) error { filename := c.Param("filename") - contenu_dir := h.Cfg.ServerContenuDir + contenu_dir := h.ContenuDir if filename == ".gitkeep" { return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) diff --git a/handlers/handlers.go b/handlers/handlers.go index b571711..16f8aeb 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -5,7 +5,6 @@ import ( "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" @@ -24,7 +23,7 @@ API Specifications return c.String(http.StatusOK, apispec) } -func RunServer(cfg config.Config) { +func RunServer(contenuDir string, serverPort int) { log.Print("[I] Starting webserver") e := echo.New() @@ -46,11 +45,11 @@ func RunServer(cfg config.Config) { groupAPI := e.Group("/api") - contenuHandler := NewContenuHandler(cfg) + 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", cfg.ServerPort))) + e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", serverPort))) } From 72ff53f846bfdf73840a17d0cd5047f3f44b146e Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 4 Dec 2023 21:26:50 -0500 Subject: [PATCH 2/3] change!: move config to `cmd.app.Config` bump: couleuvre -> `v0.10.0` Move `couleuvre.NewApp` to `cmd.init` Make flags target `&cmd.app.Config` Fix `ServerCmdExecuter`'s `RunServer` parameters Fix call to `app.Parse` in `cmd.Execute` BREAKING: Remove `cmd.Cfg()` BREAKING: Remove `cmd.cfg config.Config` --- cmd/cmd.go | 16 +++++++--------- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 93f6ff7..6a84fff 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -9,13 +9,13 @@ import ( "git.agecem.com/agecem/babillard/handlers" ) -var cfg config.Config - -func Cfg() config.Config { return cfg } +var app couleuvre.App[config.Config] func init() { - flag.StringVar(&cfg.ServerContenuDir, ServerContenuDirName, ServerContenuDirDefault, ServerContenuDirDescription) - flag.IntVar(&cfg.ServerPort, ServerPortName, ServerPortDefault, ServerPortDescription) + app = couleuvre.NewApp[config.Config]("BABILLARD_", ".", "_") + + flag.StringVar(&app.Config.ServerContenuDir, ServerContenuDirName, ServerContenuDirDefault, ServerContenuDirDescription) + flag.IntVar(&app.Config.ServerPort, ServerPortName, ServerPortDefault, ServerPortDescription) } const ( @@ -34,14 +34,12 @@ const ( ) func ServerCmdExecuter() error { - handlers.RunServer(Cfg()) + handlers.RunServer(app.Config.ServerContenuDir, app.Config.ServerPort) return nil } func Execute() error { - app := couleuvre.NewApp("BABILLARD_", ".", "_") - - if err := app.Parse(&cfg); err != nil { + if err := app.Parse(); err != nil { return err } diff --git a/go.mod b/go.mod index 9720156..8579358 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21.3 toolchain go1.21.4 require ( - codeberg.org/vlbeaudoin/couleuvre v0.8.0 + codeberg.org/vlbeaudoin/couleuvre v0.10.0 github.com/labstack/echo/v4 v4.11.3 ) diff --git a/go.sum b/go.sum index fceb914..e6aedc2 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -codeberg.org/vlbeaudoin/couleuvre v0.8.0 h1:61lJ8zICGWiBX2Ao0+p1L+fU78KKibgJ5M6kCFHXjN0= -codeberg.org/vlbeaudoin/couleuvre v0.8.0/go.mod h1:+M8nPA/3LknsY72RP0ZHCZycQ1SPxxRoXpnyHeSNE7U= +codeberg.org/vlbeaudoin/couleuvre v0.10.0 h1:Uk6795M7ziZPu1Fv8KgGNEbRjc1u4NPdPU4Tip0IpHU= +codeberg.org/vlbeaudoin/couleuvre v0.10.0/go.mod h1:+M8nPA/3LknsY72RP0ZHCZycQ1SPxxRoXpnyHeSNE7U= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= From 3b965105fa483cbd5286883ec544673477f0f44b Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 4 Dec 2023 21:31:01 -0500 Subject: [PATCH 3/3] docs: add `examples/babillard.yaml` --- examples/babillard.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 examples/babillard.yaml diff --git a/examples/babillard.yaml b/examples/babillard.yaml new file mode 100644 index 0000000..312757f --- /dev/null +++ b/examples/babillard.yaml @@ -0,0 +1,2 @@ +serverport: 9999 +servercontenudir: '/dev/null'