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:
parent
ee0eb11ea4
commit
17de134f76
4 changed files with 92 additions and 71 deletions
38
handlers/contenu.go
Normal file
38
handlers/contenu.go
Normal 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
20
handlers/handlers.go
Normal 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue