2023-02-17 17:28:47 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-21 18:37:51 -04:00
|
|
|
"embed"
|
2023-03-21 20:29:06 -04:00
|
|
|
"fmt"
|
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-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
|
|
|
// Types
|
|
|
|
|
|
|
|
type Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
|
|
|
|
var embedFS embed.FS
|
|
|
|
|
|
|
|
// Functions
|
2023-02-23 04:24:04 -05:00
|
|
|
|
|
|
|
func init() {
|
2023-03-21 18:37:51 -04:00
|
|
|
embedFS = public.GetEmbedFS()
|
2023-02-23 04:24:04 -05:00
|
|
|
}
|
|
|
|
|
2023-02-17 17:28:47 -05:00
|
|
|
func Execute() {
|
|
|
|
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-03-21 20:26:39 -04:00
|
|
|
// Static Routes
|
|
|
|
|
|
|
|
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-02-17 17:28:47 -05:00
|
|
|
e.Logger.Fatal(e.Start(":8080"))
|
|
|
|
}
|
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-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 {
|
|
|
|
return c.String(http.StatusOK, "À Propos")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleActualite(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "Actualité")
|
|
|
|
}
|
|
|
|
|
|
|
|
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.String(http.StatusOK, "Vie Étudiante")
|
|
|
|
}
|
|
|
|
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.String(http.StatusOK, "Documentation")
|
|
|
|
}
|
2023-03-24 18:21:22 -04:00
|
|
|
func handleFormulaires(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "Formulaires")
|
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
|
|
|
|
// TODO Ajouter support pour fichiers SCSS
|
|
|
|
data, _ := embedFS.ReadFile("css/index.css")
|
|
|
|
return c.Blob(http.StatusOK, "text/css", data)
|
|
|
|
}
|