Merge branch 'feature/basic-webcontent' into main
This commit is contained in:
commit
27d5f19071
5 changed files with 75 additions and 0 deletions
|
@ -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))
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1 +1,5 @@
|
|||
<h2>agecem/bottin-ag</h2>
|
||||
|
||||
<h3>StatusCode: {{ .StatusCode }}</h3>
|
||||
<h3>Message: {{ .Message }}</h3>
|
||||
<h3>Error: {{ .Error }}</h3>
|
||||
|
|
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
7
webresponse/webresponse.go
Normal file
7
webresponse/webresponse.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package webresponse
|
||||
|
||||
import "git.agecem.com/agecem/bottin-ag/apiresponse"
|
||||
|
||||
type IndexGET struct {
|
||||
apiresponse.Response
|
||||
}
|
Reference in a new issue