major v7: Standardiser l'organisation du projet

This commit is contained in:
Victor Lacasse-Beaudoin 2025-01-31 16:37:16 -05:00
parent 7c4e8d8227
commit 6cdc40926b
17 changed files with 72 additions and 66 deletions

63
pkg/babillard/cmd.go Normal file
View file

@ -0,0 +1,63 @@
package babillard
import (
"flag"
"os"
"codeberg.org/vlbeaudoin/couleuvre"
)
var app couleuvre.App[Config]
func init() {
app = couleuvre.NewApp[Config]("BABILLARD_", ".", "_")
flag.StringVar(&app.Config.ServerContenuDir, ServerContenuDirName, ServerContenuDirDefault, ServerContenuDirDescription)
flag.IntVar(&app.Config.ServerPort, ServerPortName, ServerPortDefault, ServerPortDescription)
}
const (
ServerContenuDirName = "servercontenudir"
ServerContenuDirDefault = "contenu"
ServerContenuDirDescription = "Répertoire du contenu à exposer"
ServerPortName = "serverport"
ServerPortDefault int = 8080
ServerPortDescription = "Port réseau à utiliser pour le serveur"
)
const (
ServerCmdName = "server"
ServerCmdDesc = "Démarrer le serveur web"
)
func ServerCmdExecuter() error {
RunServer(app.Config.ServerContenuDir, app.Config.ServerPort)
return nil
}
func Execute() error {
if err := app.Parse(); err != nil {
return err
}
if err := app.NewCommand(ServerCmdName, ServerCmdDesc, ServerCmdExecuter); err != nil {
return err
}
var commandName string
if len(os.Args) > 1 {
commandName = flag.Arg(0)
}
cmd, err := app.ParseCommand(commandName)
if err != nil {
return err
}
if err := cmd.Execute(); err != nil {
return err
}
return nil
}

6
pkg/babillard/config.go Normal file
View file

@ -0,0 +1,6 @@
package babillard
type Config struct {
ServerContenuDir string
ServerPort int
}

45
pkg/babillard/contenu.go Normal file
View file

@ -0,0 +1,45 @@
package babillard
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
type ContenuHandler struct {
ContenuDir string
}
func NewContenuHandler(contenuDir string) (h ContenuHandler) {
h.ContenuDir = contenuDir
return
}
func (h ContenuHandler) HandleAPIContenuList(c echo.Context) error {
contenu_dir := h.ContenuDir
files, err := 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 (h ContenuHandler) HandleAPIContenuFile(c echo.Context) error {
filename := c.Param("filename")
contenu_dir := h.ContenuDir
if filename == ".gitkeep" {
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
}
return c.File(fmt.Sprintf("%s/%s", contenu_dir, filename))
}

24
pkg/babillard/data.go Normal file
View file

@ -0,0 +1,24 @@
package babillard
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
}

55
pkg/babillard/handlers.go Normal file
View file

@ -0,0 +1,55 @@
package babillard
import (
"fmt"
"log"
"net/http"
"git.agecem.com/agecem/babillard/v7/ui"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
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)
}
func RunServer(contenuDir string, serverPort int) {
log.Print("[I] Starting webserver")
e := echo.New()
e.Pre(middleware.AddTrailingSlash())
e.Group("").Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "/html/",
Filesystem: http.FS(ui.HTMLFS()),
}))
e.Group("/public/css").Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "/css/",
Filesystem: http.FS(ui.CSSFS()),
}))
e.Group("/public/js").Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "/js/",
Filesystem: http.FS(ui.JSFS()),
}))
groupAPI := e.Group("/api")
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", serverPort)))
}