Ajouter cobra/viper pour gérer le cli
Déplacer logique de server.go vers serverCmd Ajouter .cobra.yaml pour prochaines commandes Modifier Dockerfile pour appeler serverCmd Retirer certains commentaires dans serverCmd
This commit is contained in:
parent
643100f325
commit
378db992b5
7 changed files with 578 additions and 12 deletions
60
cmd/root.go
Normal file
60
cmd/root.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
Copyright © 2023 AGECEM
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "agecem-org",
|
||||
Short: "Application du site web de l'AGECEM",
|
||||
Long: "Application du site web de l'AGECEM, l'Association Générale Étudiante du Cégep Édouard-Montpetit.",
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.agecem-org.yaml)")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// Search config in home directory with name ".agecem-org" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".agecem-org")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
142
cmd/server.go
Normal file
142
cmd/server.go
Normal file
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
Copyright © 2023 AGECEM
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"embed"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.agecem.com/agecem/agecem-org/public"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
)
|
||||
|
||||
type Template struct {
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
var embedFS embed.FS
|
||||
|
||||
// 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)
|
||||
embedFS = public.GetEmbedFS()
|
||||
}
|
||||
|
||||
func RunServer() {
|
||||
e := echo.New()
|
||||
|
||||
t := &Template{
|
||||
templates: template.Must(template.ParseFS(embedFS, "html/*.gohtml")),
|
||||
}
|
||||
|
||||
e.Renderer = t
|
||||
|
||||
e.Pre(middleware.RemoveTrailingSlash())
|
||||
|
||||
// API Routes
|
||||
|
||||
e.GET("/v1", handleV1)
|
||||
|
||||
// Static Routes
|
||||
|
||||
e.GET("/static/general.css", handleStaticCSSGeneral)
|
||||
|
||||
e.GET("/static/index.css", handleStaticCSSIndex)
|
||||
|
||||
// HTML Routes
|
||||
|
||||
e.GET("/", handleIndex)
|
||||
|
||||
e.GET("/a-propos", handleAPropos)
|
||||
|
||||
e.GET("/actualite", handleActualite)
|
||||
|
||||
e.GET("/actualite/:article", handleActualiteArticle)
|
||||
|
||||
e.GET("/vie-etudiante", handleVieEtudiante)
|
||||
|
||||
e.GET("/vie-etudiante/:organisme", handleVieEtudianteOrganisme)
|
||||
|
||||
e.GET("/documentation", handleDocumentation)
|
||||
|
||||
e.GET("/formulaires", handleFormulaires)
|
||||
|
||||
e.Logger.Fatal(e.Start(":8080"))
|
||||
}
|
||||
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
|
||||
// API Handlers
|
||||
|
||||
// handleV1 affiche les routes accessibles.
|
||||
// Les routes sont triées selon .Path, pour les rendre plus facilement navigables.
|
||||
func handleV1(c echo.Context) error {
|
||||
routes := c.Echo().Routes()
|
||||
sort.Slice(routes, func(i, j int) bool { return routes[i].Path < routes[j].Path })
|
||||
return c.JSON(http.StatusOK, routes)
|
||||
}
|
||||
|
||||
// HTML Handlers
|
||||
|
||||
func handleIndex(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "index-html", nil)
|
||||
}
|
||||
|
||||
func handleAPropos(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "a-propos-html", nil)
|
||||
}
|
||||
|
||||
func handleActualite(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "actualite-html", nil)
|
||||
}
|
||||
|
||||
func handleActualiteArticle(c echo.Context) error {
|
||||
article := c.Param("article")
|
||||
return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article))
|
||||
}
|
||||
func handleVieEtudiante(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "vie-etudiante-html", nil)
|
||||
}
|
||||
func handleVieEtudianteOrganisme(c echo.Context) error {
|
||||
organisme := c.Param("organisme")
|
||||
return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme))
|
||||
}
|
||||
func handleDocumentation(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "documentation-html", nil)
|
||||
}
|
||||
func handleFormulaires(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "formulaires-html", nil)
|
||||
}
|
||||
|
||||
// CSS Handlers
|
||||
|
||||
func handleStaticCSSIndex(c echo.Context) error {
|
||||
// TODO Ajouter gestion d'erreurs
|
||||
data, _ := embedFS.ReadFile("css/index.css")
|
||||
return c.Blob(http.StatusOK, "text/css", data)
|
||||
}
|
||||
|
||||
func handleStaticCSSGeneral(c echo.Context) error {
|
||||
// TODO Ajouter gestion d'erreurs
|
||||
data, _ := embedFS.ReadFile("css/general.css")
|
||||
return c.Blob(http.StatusOK, "text/css", data)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue