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`
This commit is contained in:
Victor Lacasse-Beaudoin 2023-12-04 21:23:25 -05:00
parent c0f976e7cf
commit a3c2c8d1f0
2 changed files with 8 additions and 10 deletions

View file

@ -6,22 +6,21 @@ import (
"net/http" "net/http"
"strings" "strings"
"git.agecem.com/agecem/babillard/config"
"git.agecem.com/agecem/babillard/data" "git.agecem.com/agecem/babillard/data"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
type ContenuHandler struct { type ContenuHandler struct {
Cfg config.Config ContenuDir string
} }
func NewContenuHandler(cfg config.Config) (h ContenuHandler) { func NewContenuHandler(contenuDir string) (h ContenuHandler) {
h.Cfg = cfg h.ContenuDir = contenuDir
return return
} }
func (h ContenuHandler) HandleAPIContenuList(c echo.Context) error { func (h ContenuHandler) HandleAPIContenuList(c echo.Context) error {
contenu_dir := h.Cfg.ServerContenuDir contenu_dir := h.ContenuDir
files, err := data.ListContenu(contenu_dir) files, err := data.ListContenu(contenu_dir)
if err != nil { if err != nil {
@ -37,7 +36,7 @@ func (h ContenuHandler) HandleAPIContenuList(c echo.Context) error {
func (h ContenuHandler) HandleAPIContenuFile(c echo.Context) error { func (h ContenuHandler) HandleAPIContenuFile(c echo.Context) error {
filename := c.Param("filename") filename := c.Param("filename")
contenu_dir := h.Cfg.ServerContenuDir contenu_dir := h.ContenuDir
if filename == ".gitkeep" { if filename == ".gitkeep" {
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})

View file

@ -5,7 +5,6 @@ import (
"log" "log"
"net/http" "net/http"
"git.agecem.com/agecem/babillard/config"
"git.agecem.com/agecem/babillard/public" "git.agecem.com/agecem/babillard/public"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
@ -24,7 +23,7 @@ API Specifications
return c.String(http.StatusOK, apispec) return c.String(http.StatusOK, apispec)
} }
func RunServer(cfg config.Config) { func RunServer(contenuDir string, serverPort int) {
log.Print("[I] Starting webserver") log.Print("[I] Starting webserver")
e := echo.New() e := echo.New()
@ -46,11 +45,11 @@ func RunServer(cfg config.Config) {
groupAPI := e.Group("/api") groupAPI := e.Group("/api")
contenuHandler := NewContenuHandler(cfg) contenuHandler := NewContenuHandler(contenuDir)
groupAPI.GET("/", HandleAPIShow) groupAPI.GET("/", HandleAPIShow)
groupAPI.GET("/contenu/", contenuHandler.HandleAPIContenuList) groupAPI.GET("/contenu/", contenuHandler.HandleAPIContenuList)
groupAPI.GET("/contenu/:filename/", contenuHandler.HandleAPIContenuFile) 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)))
} }