From 1022dd05db9220622d5bbfc83f2ed10929249b01 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 21 Mar 2023 18:37:51 -0400 Subject: [PATCH] Ajouter templating go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Déplacer embed/ vers public/ Ajouter public/js et public/scss Ajouter templating pour public/html/*.gohtml Ajouter --build à procédure dans README.md Déplacer contenu de index.html vers template `index-html` Ajouter middleware RemoveTrailingSlash Déplacer anonymous function de route `/` vers handler `handleIndex` --- Dockerfile | 2 +- README.md | 4 +- embed/embed.go | 12 ----- go.mod | 2 + go.sum | 4 ++ .../index.html => public/html/index.gohtml | 2 + public/js/index.js | 0 public/public.go | 10 +++++ public/scss/index.scss | 0 server.go | 45 ++++++++++++++++--- 10 files changed, 60 insertions(+), 21 deletions(-) delete mode 100644 embed/embed.go rename embed/html/index.html => public/html/index.gohtml (88%) create mode 100644 public/js/index.js create mode 100644 public/public.go create mode 100644 public/scss/index.scss diff --git a/Dockerfile b/Dockerfile index 93e299e..bd1f3c4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ WORKDIR /go/src/app COPY go.mod go.sum main.go server.go ./ -ADD embed/ embed/ +ADD public/ public/ RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o app . diff --git a/README.md b/README.md index b3c776f..332a1a9 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,9 @@ Le lifecycle du serveur est maintenu par containers, en utilisant `docker-compos ### Exemples d'utilisation de docker-compose -Déployer le ou les containers en mode détaché +Déployer le ou les containers en mode détaché, en s'assurant de rebâtir l'image. -`$ docker-compose up -d` +`$ docker-compose up -d --build` Voir l'état des containers diff --git a/embed/embed.go b/embed/embed.go deleted file mode 100644 index 02eed70..0000000 --- a/embed/embed.go +++ /dev/null @@ -1,12 +0,0 @@ -package embed - -import ( - _ "embed" -) - -//go:embed html/index.html -var html_index string - -func ReadHtml() string { - return html_index -} diff --git a/go.mod b/go.mod index c710d71..4e7420c 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require github.com/labstack/echo/v4 v4.10.0 require ( + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/labstack/gommon v0.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.16 // indirect @@ -14,4 +15,5 @@ require ( golang.org/x/net v0.4.0 // indirect golang.org/x/sys v0.3.0 // indirect golang.org/x/text v0.5.0 // indirect + golang.org/x/time v0.2.0 // indirect ) diff --git a/go.sum b/go.sum index 70809c2..d82d74e 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/labstack/echo/v4 v4.10.0 h1:5CiyngihEO4HXsz3vVsJn7f8xAlWwRr3aY6Ih280ZKA= github.com/labstack/echo/v4 v4.10.0/go.mod h1:S/T/5fy/GigaXnHTkh0ZGe4LpkkQysvRjFMSUTkDRNQ= github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= @@ -33,6 +35,8 @@ golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/time v0.2.0 h1:52I/1L54xyEQAYdtcSuxtiT84KGYTBGXwayxmIpNJhE= +golang.org/x/time v0.2.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/embed/html/index.html b/public/html/index.gohtml similarity index 88% rename from embed/html/index.html rename to public/html/index.gohtml index 49fa95f..329c882 100644 --- a/embed/html/index.html +++ b/public/html/index.gohtml @@ -1,3 +1,4 @@ +{{ define "index-html" }} @@ -11,3 +12,4 @@

Association Générale Étudiante du Cégep Édouard-Montpetit

+{{ end }} diff --git a/public/js/index.js b/public/js/index.js new file mode 100644 index 0000000..e69de29 diff --git a/public/public.go b/public/public.go new file mode 100644 index 0000000..92df5a0 --- /dev/null +++ b/public/public.go @@ -0,0 +1,10 @@ +package public + +import "embed" + +//go:embed html/* scss/* js/* +var embedFS embed.FS + +func GetEmbedFS() embed.FS { + return embedFS +} diff --git a/public/scss/index.scss b/public/scss/index.scss new file mode 100644 index 0000000..e69de29 diff --git a/server.go b/server.go index 7c52fee..5f67ef0 100644 --- a/server.go +++ b/server.go @@ -1,23 +1,56 @@ package main import ( + "embed" + "html/template" + "io" "net/http" - "git.agecem.com/agecem/agecem-org/embed" + "git.agecem.com/agecem/agecem-org/public" "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" ) -var html string +// Types + +type Template struct { + templates *template.Template +} + +// Variables + +var embedFS embed.FS + +// Functions func init() { - html = embed.ReadHtml() + embedFS = public.GetEmbedFS() } func Execute() { e := echo.New() - e.GET("/", func(c echo.Context) error { - return c.HTML(http.StatusOK, html) - }) + t := &Template{ + templates: template.Must(template.ParseFS(embedFS, "html/*.gohtml")), + } + + e.Renderer = t + + e.Pre(middleware.RemoveTrailingSlash()) + + e.GET("/", handleIndex) + 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 + +// HTML Handlers + +func handleIndex(c echo.Context) error { + return c.Render(http.StatusOK, "index-html", nil) +}