diff --git a/cmd/web.go b/cmd/web.go index 08c1286..37a678a 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -8,6 +8,8 @@ import ( "log" "git.agecem.com/agecem/bottin-ag/config" + "git.agecem.com/agecem/bottin-ag/webcontent" + "git.agecem.com/agecem/bottin-ag/webhandler" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/spf13/cobra" @@ -25,8 +27,14 @@ var webCmd = &cobra.Command{ e := echo.New() + e.Renderer = webcontent.TemplateHTMLFS() + e.Pre(middleware.AddTrailingSlash()) + handler := webhandler.New() + + webhandler.DeclareRoutes(e, &handler) + e.Start(fmt.Sprintf(":%d", cfg.Web.Port)) }, } diff --git a/webcontent/html/index.html b/webcontent/html/index.html index 6cecd06..2a2f9de 100644 --- a/webcontent/html/index.html +++ b/webcontent/html/index.html @@ -1 +1,5 @@

agecem/bottin-ag

+ +

StatusCode: {{ .StatusCode }}

+

Message: {{ .Message }}

+

Error: {{ .Error }}

diff --git a/webcontent/webcontent.go b/webcontent/webcontent.go index 462e56d..135e4fe 100644 --- a/webcontent/webcontent.go +++ b/webcontent/webcontent.go @@ -3,3 +3,32 @@ Package webcontent provides the content to be embedded in the binary executable for the web app */ package webcontent + +import ( + "embed" + "io" + "text/template" + + "github.com/labstack/echo/v4" +) + +//go:embed html/*html +var htmlFS embed.FS + +func HTMLFS() embed.FS { + return htmlFS +} + +type Template struct { + templates *template.Template +} + +func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + return t.templates.ExecuteTemplate(w, name, data) +} + +func TemplateHTMLFS() *Template { + return &Template{ + templates: template.Must(template.ParseFS(HTMLFS(), "html/*.html")), + } +} diff --git a/webhandler/webhandler.go b/webhandler/webhandler.go index 56f4475..006f5fe 100644 --- a/webhandler/webhandler.go +++ b/webhandler/webhandler.go @@ -1,2 +1,29 @@ // Package webhandler provides handlers for the web app routes package webhandler + +import ( + "net/http" + + "git.agecem.com/agecem/bottin-ag/webresponse" + "github.com/labstack/echo/v4" +) + +func DeclareRoutes(e *echo.Echo, h *WebHandler) { + e.GET("/", h.IndexGET) +} + +type WebHandler struct { +} + +func New() WebHandler { + return WebHandler{} +} + +func (w *WebHandler) IndexGET(c echo.Context) error { + var r webresponse.IndexGET + + r.Message = "foo" + r.StatusCode = http.StatusOK + + return c.Render(r.StatusCode, "index-html", r) +} diff --git a/webresponse/webresponse.go b/webresponse/webresponse.go new file mode 100644 index 0000000..88289ba --- /dev/null +++ b/webresponse/webresponse.go @@ -0,0 +1,7 @@ +package webresponse + +import "git.agecem.com/agecem/bottin-ag/apiresponse" + +type IndexGET struct { + apiresponse.Response +}