Victor Lacasse-Beaudoin
ce387d3028
Il a été décidé de mettre en suspens le support pour transpilage automatique de SCSS et d'utiliser directement CSS pour l'instant.
132 lines
2.8 KiB
Go
132 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"html/template"
|
|
"io"
|
|
"net/http"
|
|
"sort"
|
|
|
|
"git.agecem.com/agecem/agecem-org/public"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
)
|
|
|
|
// Types
|
|
|
|
type Template struct {
|
|
templates *template.Template
|
|
}
|
|
|
|
// Variables
|
|
|
|
var embedFS embed.FS
|
|
|
|
// Functions
|
|
|
|
func init() {
|
|
embedFS = public.GetEmbedFS()
|
|
}
|
|
|
|
func Execute() {
|
|
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.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")
|
|
}
|
|
func handleFormulaires(c echo.Context) error {
|
|
return c.String(http.StatusOK, "Formulaires")
|
|
}
|
|
|
|
// 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)
|
|
}
|