2023-03-28 02:35:42 -04:00
|
|
|
/*
|
|
|
|
Copyright © 2023 AGECEM
|
|
|
|
*/
|
|
|
|
package cmd
|
2023-02-17 17:28:47 -05:00
|
|
|
|
|
|
|
import (
|
2023-03-21 20:29:06 -04:00
|
|
|
"fmt"
|
2023-03-28 02:35:42 -04:00
|
|
|
|
|
|
|
"embed"
|
2023-03-21 18:37:51 -04:00
|
|
|
"html/template"
|
|
|
|
"io"
|
2023-02-17 17:28:47 -05:00
|
|
|
"net/http"
|
2023-03-21 18:48:23 -04:00
|
|
|
"sort"
|
2023-02-17 17:28:47 -05:00
|
|
|
|
2023-03-28 02:35:42 -04:00
|
|
|
"github.com/spf13/cobra"
|
2023-04-21 16:41:50 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-03-28 02:35:42 -04:00
|
|
|
|
2023-04-21 16:41:50 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/data"
|
2023-03-21 18:37:51 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/public"
|
2023-02-17 17:28:47 -05:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-03-21 18:37:51 -04:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-02-17 17:28:47 -05:00
|
|
|
)
|
|
|
|
|
2023-03-21 18:37:51 -04:00
|
|
|
type Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
var embedFS embed.FS
|
|
|
|
|
2023-03-28 02:35:42 -04:00
|
|
|
// 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()
|
|
|
|
},
|
|
|
|
}
|
2023-02-23 04:24:04 -05:00
|
|
|
|
|
|
|
func init() {
|
2023-03-28 02:35:42 -04:00
|
|
|
rootCmd.AddCommand(serverCmd)
|
2023-03-21 18:37:51 -04:00
|
|
|
embedFS = public.GetEmbedFS()
|
2023-04-21 16:41:50 -04:00
|
|
|
|
|
|
|
// server.port - --server-port
|
|
|
|
serverCmd.Flags().Int("server-port", 8080, "Port to run the webserver on (config: server.port)")
|
|
|
|
viper.BindPFlag("server.port", serverCmd.Flags().Lookup("server-port"))
|
|
|
|
|
|
|
|
// server.documents.location - --server-documents-location
|
|
|
|
serverCmd.Flags().String("server-documents-location", "us-east", "Storage bucket location (config: server.documents.location)")
|
|
|
|
viper.BindPFlag("server.documents.location", serverCmd.Flags().Lookup("server-documents-location"))
|
|
|
|
|
|
|
|
// server.documents.endpoint - --server-documents-endpoint
|
|
|
|
serverCmd.Flags().String("server-documents-endpoint", "minio:9000", "Storage server endpoint (config: server.documents.endpoint)")
|
|
|
|
viper.BindPFlag("server.documents.endpoint", serverCmd.Flags().Lookup("server-documents-endpoint"))
|
|
|
|
|
|
|
|
// server.documents.access_key_id - --server-documents-access-key-id
|
|
|
|
serverCmd.Flags().String("server-documents-access-key-id", "", "Storage server access key id (config: server.documents.access_key_id)")
|
|
|
|
viper.BindPFlag("documents.accessKeyID", serverCmd.Flags().Lookup("documents-access-key-id"))
|
|
|
|
|
|
|
|
// server.documents.secret_access_key - --server-documents-secret-access-key
|
|
|
|
serverCmd.Flags().String("server-documents-secret-access-key", "", "Storage server secret access key (config: server.documents.secret_access_key)")
|
|
|
|
viper.BindPFlag("server.documents.secret_access_key", serverCmd.Flags().Lookup("server-documents-secret-access-key"))
|
|
|
|
|
|
|
|
// server.documents.use_ssl - --server-documents-use-ssl
|
|
|
|
serverCmd.Flags().Bool("server-documents-use-ssl", true, "Storage server SSL status (config: server.documents.use_ssl)")
|
|
|
|
viper.BindPFlag("server.documents.use_ssl", serverCmd.Flags().Lookup("server-documents-use-ssl"))
|
|
|
|
|
|
|
|
// server.documents.buckets - --server-documents-buckets
|
|
|
|
serverCmd.Flags().StringSlice("server-documents-buckets", nil, "Buckets that are allowed to be accessed by the API (config: server.documents.buckets)")
|
|
|
|
viper.BindPFlag("server.documents.buckets", serverCmd.Flags().Lookup("server-documents-buckets"))
|
2023-02-23 04:24:04 -05:00
|
|
|
}
|
|
|
|
|
2023-03-28 02:35:42 -04:00
|
|
|
func RunServer() {
|
2023-02-17 17:28:47 -05:00
|
|
|
e := echo.New()
|
2023-02-23 04:24:04 -05:00
|
|
|
|
2023-03-21 18:37:51 -04:00
|
|
|
t := &Template{
|
|
|
|
templates: template.Must(template.ParseFS(embedFS, "html/*.gohtml")),
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Renderer = t
|
|
|
|
|
|
|
|
e.Pre(middleware.RemoveTrailingSlash())
|
|
|
|
|
2023-03-21 18:48:23 -04:00
|
|
|
// API Routes
|
|
|
|
|
|
|
|
e.GET("/v1", handleV1)
|
|
|
|
|
2023-04-21 16:41:50 -04:00
|
|
|
e.GET("/v1/health", handleV1Health)
|
|
|
|
|
2023-03-21 20:26:39 -04:00
|
|
|
// Static Routes
|
|
|
|
|
2023-03-24 20:02:46 -04:00
|
|
|
e.GET("/static/general.css", handleStaticCSSGeneral)
|
|
|
|
|
2023-03-21 20:26:39 -04:00
|
|
|
e.GET("/static/index.css", handleStaticCSSIndex)
|
|
|
|
|
2023-03-21 18:48:23 -04:00
|
|
|
// HTML Routes
|
|
|
|
|
2023-03-21 18:37:51 -04:00
|
|
|
e.GET("/", handleIndex)
|
|
|
|
|
2023-03-21 20:29:06 -04:00
|
|
|
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)
|
|
|
|
|
2023-03-24 18:21:22 -04:00
|
|
|
e.GET("/formulaires", handleFormulaires)
|
2023-03-21 20:29:06 -04:00
|
|
|
|
2023-04-21 16:41:50 -04:00
|
|
|
e.Logger.Fatal(e.Start(
|
|
|
|
fmt.Sprintf(":%d", viper.GetInt("server.port"))))
|
2023-02-17 17:28:47 -05:00
|
|
|
}
|
2023-03-21 18:37:51 -04:00
|
|
|
|
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// API Handlers
|
|
|
|
|
2023-03-21 18:48:23 -04:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-04-21 16:41:50 -04:00
|
|
|
// handleV1Health affiche l'état de connexion à la base de données
|
|
|
|
func handleV1Health(c echo.Context) error {
|
|
|
|
documents_buckets := viper.GetStringSlice("server.documents.buckets")
|
|
|
|
|
|
|
|
documents_endpoint := viper.GetString("server.documents.endpoint")
|
|
|
|
documents_access_key_id := viper.GetString("server.documents.access_key_id")
|
|
|
|
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
|
|
|
|
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
|
|
|
|
|
|
|
|
client, err := data.NewClient(documents_buckets, documents_endpoint, documents_access_key_id, documents_secret_access_key, documents_use_ssl)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(documents_buckets) == 0 {
|
|
|
|
return c.JSON(http.StatusOK, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
message := make(map[string]interface{}, len(documents_buckets))
|
|
|
|
|
|
|
|
for _, bucket := range documents_buckets {
|
|
|
|
|
|
|
|
exists, err := client.BucketExists(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
message[bucket] = exists
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, message)
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:37:51 -04:00
|
|
|
// HTML Handlers
|
|
|
|
|
|
|
|
func handleIndex(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "index-html", nil)
|
|
|
|
}
|
2023-03-21 19:59:41 -04:00
|
|
|
|
2023-03-21 20:29:06 -04:00
|
|
|
func handleAPropos(c echo.Context) error {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "a-propos-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleActualite(c echo.Context) error {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "actualite-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "vie-etudiante-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
|
|
|
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 {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "documentation-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
2023-03-24 18:21:22 -04:00
|
|
|
func handleFormulaires(c echo.Context) error {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "formulaires-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 19:59:41 -04:00
|
|
|
// 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)
|
|
|
|
}
|
2023-03-24 20:02:46 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|