Déplacer functions de serverCmd vers data/ et handlers/

Déplacer ListContenu vers data/

Déplacer handlers / et /api vers handlers/handlers.go

Déplacer handlers /api/contenu vers handlers/contenu.go
This commit is contained in:
Victor Lacasse-Beaudoin 2023-05-05 18:59:53 -04:00
parent ee0eb11ea4
commit 17de134f76
4 changed files with 92 additions and 71 deletions

View file

@ -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)))
}

24
data/data.go Normal file
View file

@ -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
}

38
handlers/contenu.go Normal file
View file

@ -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))
}

20
handlers/handlers.go Normal file
View file

@ -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)
}