From 17de134f7611592ca173b429d16bef2fb3d470a4 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 5 May 2023 18:59:53 -0400 Subject: [PATCH] =?UTF-8?q?D=C3=A9placer=20functions=20de=20serverCmd=20ve?= =?UTF-8?q?rs=20data/=20et=20handlers/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Déplacer ListContenu vers data/ Déplacer handlers / et /api vers handlers/handlers.go Déplacer handlers /api/contenu vers handlers/contenu.go --- cmd/server.go | 81 ++++++-------------------------------------- data/data.go | 24 +++++++++++++ handlers/contenu.go | 38 +++++++++++++++++++++ handlers/handlers.go | 20 +++++++++++ 4 files changed, 92 insertions(+), 71 deletions(-) create mode 100644 data/data.go create mode 100644 handlers/contenu.go create mode 100644 handlers/handlers.go diff --git a/cmd/server.go b/cmd/server.go index 960edba..44eba23 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -3,10 +3,8 @@ package cmd import ( "fmt" "log" - "net/http" - "os" - "strings" + "git.agecem.com/agecem/babillard/handlers" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/spf13/cobra" @@ -18,7 +16,10 @@ var serverCmd = &cobra.Command{ Use: "server", Short: "Démarrer le serveur web", Run: func(cmd *cobra.Command, args []string) { - runServer() + port := viper.GetInt("server.port") + static_dir := viper.GetString("server.static_dir") + + runServer(port, static_dir) }, } @@ -38,12 +39,9 @@ func declareFlags() { viper.BindPFlag("server.static_dir", serverCmd.Flags().Lookup("static_dir")) } -func runServer() { +func runServer(port int, static_dir string) { log.Print("[I] Starting webserver") - port := fmt.Sprintf(":%d", viper.GetInt("server.port")) - static_dir := viper.GetString("server.static_dir") - e := echo.New() g := e.Group("") @@ -52,68 +50,9 @@ func runServer() { g.Static("/", static_dir) - g.GET("/api", showAPISpecs) - g.GET("/api/contenu", showContenu) - g.GET("/api/contenu/:filename", showFile) - - e.Logger.Fatal(e.Start(port)) + g.GET("/api", handlers.HandleAPIShow) + g.GET("/api/contenu", handlers.HandleAPIContenuList) + g.GET("/api/contenu/:filename", handlers.HandleAPIContenuFile) -} - -func listContenu(path string) ([]string, error) { - var files []string - f, err_open := os.Open(path) - defer f.Close() - - if err_open != nil { - return nil, err_open - } - fileInfo, err_read := f.Readdir(-1) - if err_read != nil { - return nil, err_read - } - - for _, file := range fileInfo { - if file.Name() != ".gitkeep" && file.Name() != "messages.txt" { - files = append(files, file.Name()) - } - } - return files, nil -} - -// Handlers - -func showAPISpecs(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 showContenu(c echo.Context) error { - contenu_dir := viper.GetString("server.contenu_dir") - files, err := listContenu(contenu_dir) - - if err != nil { - log.Fatal(err) - } - - return c.String(http.StatusOK, strings.Join(files, ";")) -} - -func showFile(c echo.Context) error { - filename := c.Param("filename") - contenu_dir := viper.GetString("server.contenu_dir") - - if filename == ".gitkeep" { - return c.String(http.StatusOK, "invalid file requested") - } - - return c.File(fmt.Sprintf("%s/%s", contenu_dir, filename)) + e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", port))) } diff --git a/data/data.go b/data/data.go new file mode 100644 index 0000000..3fa8676 --- /dev/null +++ b/data/data.go @@ -0,0 +1,24 @@ +package data + +import "os" + +func ListContenu(path string) ([]string, error) { + var files []string + f, err_open := os.Open(path) + defer f.Close() + + if err_open != nil { + return nil, err_open + } + fileInfo, err_read := f.Readdir(-1) + if err_read != nil { + return nil, err_read + } + + for _, file := range fileInfo { + if file.Name() != ".gitkeep" && file.Name() != "messages.txt" { + files = append(files, file.Name()) + } + } + return files, nil +} diff --git a/handlers/contenu.go b/handlers/contenu.go new file mode 100644 index 0000000..063779c --- /dev/null +++ b/handlers/contenu.go @@ -0,0 +1,38 @@ +package handlers + +import ( + "fmt" + "log" + "net/http" + "strings" + + "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") + + files, err := data.ListContenu(contenu_dir) + if err != nil { + log.Println(err) + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Internal Server Error", + "error": err.Error(), + }) + } + + return c.String(http.StatusOK, strings.Join(files, ";")) +} + +func HandleAPIContenuFile(c echo.Context) error { + filename := c.Param("filename") + contenu_dir := viper.GetString("server.contenu_dir") + + if filename == ".gitkeep" { + return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + } + + return c.File(fmt.Sprintf("%s/%s", contenu_dir, filename)) +} diff --git a/handlers/handlers.go b/handlers/handlers.go new file mode 100644 index 0000000..d78c7f5 --- /dev/null +++ b/handlers/handlers.go @@ -0,0 +1,20 @@ +package handlers + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +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) +}