120 lines
2.8 KiB
Go
120 lines
2.8 KiB
Go
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/labstack/echo/v4"
|
||
|
"github.com/labstack/echo/v4/middleware"
|
||
|
"github.com/spf13/cobra"
|
||
|
"github.com/spf13/viper"
|
||
|
)
|
||
|
|
||
|
// serverCmd represents the server command
|
||
|
var serverCmd = &cobra.Command{
|
||
|
Use: "server",
|
||
|
Short: "Démarrer le serveur web",
|
||
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
runServer()
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
rootCmd.AddCommand(serverCmd)
|
||
|
declareFlags()
|
||
|
}
|
||
|
|
||
|
func declareFlags() {
|
||
|
serverCmd.Flags().IntP("port", "p", 8080, "Port réseau à utiliser (config: \"server.port\")")
|
||
|
viper.BindPFlag("server.port", serverCmd.Flags().Lookup("port"))
|
||
|
|
||
|
serverCmd.Flags().String("contenu_dir", "contenu", "Répertoire du contenu à exposer (config: \"server.contenu_dir\")")
|
||
|
viper.BindPFlag("server.contenu_dir", serverCmd.Flags().Lookup("contenu_dir"))
|
||
|
|
||
|
serverCmd.Flags().String("static_dir", "static", "Répertoire des fichiers statiques à exposer (config: \"server.static_dir\")")
|
||
|
viper.BindPFlag("server.static_dir", serverCmd.Flags().Lookup("static_dir"))
|
||
|
}
|
||
|
|
||
|
func runServer() {
|
||
|
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("")
|
||
|
|
||
|
e.Pre(middleware.RemoveTrailingSlash())
|
||
|
|
||
|
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))
|
||
|
|
||
|
}
|
||
|
|
||
|
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))
|
||
|
}
|