Ajouter templating go #15

Merged
vlbeaudoin merged 1 commit from feature/templating into main 2023-03-21 18:43:20 -04:00
10 changed files with 60 additions and 21 deletions

View file

@ -6,7 +6,7 @@ WORKDIR /go/src/app
COPY go.mod go.sum main.go server.go ./ 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 . RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o app .

View file

@ -17,9 +17,9 @@ Le lifecycle du serveur est maintenu par containers, en utilisant `docker-compos
### Exemples d'utilisation de docker-compose ### 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 Voir l'état des containers

View file

@ -1,12 +0,0 @@
package embed
import (
_ "embed"
)
//go:embed html/index.html
var html_index string
func ReadHtml() string {
return html_index
}

2
go.mod
View file

@ -5,6 +5,7 @@ go 1.19
require github.com/labstack/echo/v4 v4.10.0 require github.com/labstack/echo/v4 v4.10.0
require ( require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/labstack/gommon v0.4.0 // indirect github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // 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/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect golang.org/x/text v0.5.0 // indirect
golang.org/x/time v0.2.0 // indirect
) )

4
go.sum
View file

@ -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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:5CiyngihEO4HXsz3vVsJn7f8xAlWwRr3aY6Ih280ZKA=
github.com/labstack/echo/v4 v4.10.0/go.mod h1:S/T/5fy/GigaXnHTkh0ZGe4LpkkQysvRjFMSUTkDRNQ= github.com/labstack/echo/v4 v4.10.0/go.mod h1:S/T/5fy/GigaXnHTkh0ZGe4LpkkQysvRjFMSUTkDRNQ=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= 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/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 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 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/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-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -1,3 +1,4 @@
{{ define "index-html" }}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="fr">
<head> <head>
@ -11,3 +12,4 @@
<h1>Association Générale Étudiante du Cégep Édouard-Montpetit</h1> <h1>Association Générale Étudiante du Cégep Édouard-Montpetit</h1>
</body> </body>
</html> </html>
{{ end }}

0
public/js/index.js Normal file
View file

10
public/public.go Normal file
View file

@ -0,0 +1,10 @@
package public
import "embed"
//go:embed html/* scss/* js/*
var embedFS embed.FS
func GetEmbedFS() embed.FS {
return embedFS
}

0
public/scss/index.scss Normal file
View file

View file

@ -1,23 +1,56 @@
package main package main
import ( import (
"embed"
"html/template"
"io"
"net/http" "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"
"github.com/labstack/echo/v4/middleware"
) )
var html string // Types
type Template struct {
templates *template.Template
}
// Variables
var embedFS embed.FS
// Functions
func init() { func init() {
html = embed.ReadHtml() embedFS = public.GetEmbedFS()
} }
func Execute() { func Execute() {
e := echo.New() e := echo.New()
e.GET("/", func(c echo.Context) error { t := &Template{
return c.HTML(http.StatusOK, html) templates: template.Must(template.ParseFS(embedFS, "html/*.gohtml")),
}) }
e.Renderer = t
e.Pre(middleware.RemoveTrailingSlash())
e.GET("/", handleIndex)
e.Logger.Fatal(e.Start(":8080")) 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)
}