From cf3c948d7f31dea820fc57deefd031770ee56ff5 Mon Sep 17 00:00:00 2001 From: Dennis Werner Date: Wed, 5 Jun 2024 15:33:45 +0200 Subject: [PATCH] restructure and add tailwind, daisy-ui and htmx --- .gitignore | 3 ++- cmd/blokhouse/main.go | 4 ++-- go.mod | 8 ++++---- go.sum | 14 ++++++++------ web/api/v1/api.go | 7 +++++++ {api => web/api}/v1/asset_handler.go | 8 ++++---- web/ui/view/index.templ | 11 +++++++++++ web/ui/view/template.templ | 15 +++++++++++++++ web/ui/web_handler.go | 25 +++++++++++++++++++++++++ api/v1/apiserver.go => web/web.go | 16 +++++++++------- web/web_handler.go | 11 ----------- 11 files changed, 87 insertions(+), 35 deletions(-) create mode 100644 web/api/v1/api.go rename {api => web/api}/v1/asset_handler.go (83%) create mode 100644 web/ui/view/index.templ create mode 100644 web/ui/view/template.templ create mode 100644 web/ui/web_handler.go rename api/v1/apiserver.go => web/web.go (83%) delete mode 100644 web/web_handler.go diff --git a/.gitignore b/.gitignore index 26db00f..1d5c56a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*_templ.go \ No newline at end of file +*_templ.go +blokhouse diff --git a/cmd/blokhouse/main.go b/cmd/blokhouse/main.go index f1d949f..6a76522 100644 --- a/cmd/blokhouse/main.go +++ b/cmd/blokhouse/main.go @@ -5,9 +5,9 @@ import ( "log/slog" "os" - "github.com/d4sw4r/blokhouse/api/v1" "github.com/d4sw4r/blokhouse/internal/service" "github.com/d4sw4r/blokhouse/internal/storage" + "github.com/d4sw4r/blokhouse/web" ) func main() { @@ -15,6 +15,6 @@ func main() { slog.SetDefault(logger) db := storage.NewMemoryStorage() svc := service.NewAssetSvc(db) - s := api.NewServer(":9000", svc) + s := web.NewServer(":9000", svc) log.Fatal(s.Start()) } diff --git a/go.mod b/go.mod index fbcccbc..10199d7 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/d4sw4r/blokhouse go 1.22 require ( + github.com/a-h/templ v0.2.707 github.com/google/uuid v1.5.0 github.com/labstack/echo-contrib v0.15.0 github.com/labstack/echo/v4 v4.11.4 @@ -13,7 +14,6 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/golang/protobuf v1.5.2 // indirect - github.com/google/go-cmp v0.6.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -27,9 +27,9 @@ require ( github.com/prometheus/procfs v0.9.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect - golang.org/x/crypto v0.21.0 // indirect - golang.org/x/net v0.23.0 // indirect - golang.org/x/sys v0.18.0 // indirect + golang.org/x/crypto v0.22.0 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sys v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.5.0 // indirect google.golang.org/protobuf v1.33.0 // indirect diff --git a/go.sum b/go.sum index 8967330..8b86962 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/a-h/templ v0.2.707 h1:T1Gkd2ugbRglZ9rYw/VBchWOSZVKmetDbBkm4YubM7U= +github.com/a-h/templ v0.2.707/go.mod h1:5cqsugkq9IerRNucNsI4DEamdHPsoGMQy99DzydLhM8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= @@ -56,15 +58,15 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= -golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= -golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= -golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= diff --git a/web/api/v1/api.go b/web/api/v1/api.go new file mode 100644 index 0000000..f78ca6f --- /dev/null +++ b/web/api/v1/api.go @@ -0,0 +1,7 @@ +package api + +type API struct{} + +func NewAPI() API { + return API{} +} diff --git a/api/v1/asset_handler.go b/web/api/v1/asset_handler.go similarity index 83% rename from api/v1/asset_handler.go rename to web/api/v1/asset_handler.go index b44593a..c130850 100644 --- a/api/v1/asset_handler.go +++ b/web/api/v1/asset_handler.go @@ -9,7 +9,7 @@ import ( "github.com/labstack/echo/v4" ) -func createAsset(c echo.Context) error { +func (a API) CreateAsset(c echo.Context) error { // lock.Lock() // defer lock.Unlock() s := c.Get("svc").(service.AssetSvc) @@ -20,7 +20,7 @@ func createAsset(c echo.Context) error { } -func findAsset(c echo.Context) error { +func (a API) FindAsset(c echo.Context) error { id := c.Param("id") myid, err := uuid.Parse(id) if err != nil { @@ -34,7 +34,7 @@ func findAsset(c echo.Context) error { return c.JSON(http.StatusOK, as) } -func listAssets(c echo.Context) error { +func (a API) ListAssets(c echo.Context) error { // lock.Lock() // defer lock.Unlock() s := c.Get("svc").(service.AssetSvc) @@ -42,7 +42,7 @@ func listAssets(c echo.Context) error { return c.JSON(http.StatusOK, as) } -func deleteAsset(c echo.Context) error { +func (a API) DeleteAsset(c echo.Context) error { id := c.Param("id") myid, err := uuid.Parse(id) if err != nil { diff --git a/web/ui/view/index.templ b/web/ui/view/index.templ new file mode 100644 index 0000000..ee79d0a --- /dev/null +++ b/web/ui/view/index.templ @@ -0,0 +1,11 @@ +package ui + +templ Index() { + @template(){ +
hello world
+
+
+ } +} \ No newline at end of file diff --git a/web/ui/view/template.templ b/web/ui/view/template.templ new file mode 100644 index 0000000..bfee098 --- /dev/null +++ b/web/ui/view/template.templ @@ -0,0 +1,15 @@ +package ui + +templ template() { + + + + + + + + + { children... } + + +} \ No newline at end of file diff --git a/web/ui/web_handler.go b/web/ui/web_handler.go new file mode 100644 index 0000000..507985e --- /dev/null +++ b/web/ui/web_handler.go @@ -0,0 +1,25 @@ +package ui + +import ( + "net/http" + + "github.com/a-h/templ" + ui "github.com/d4sw4r/blokhouse/web/ui/view" + "github.com/labstack/echo/v4" +) + +// This custom Render replaces Echo's echo.Context.Render() with templ's templ.Component.Render(). +func Render(ctx echo.Context, statusCode int, t templ.Component) error { + buf := templ.GetBuffer() + defer templ.ReleaseBuffer(buf) + + if err := t.Render(ctx.Request().Context(), buf); err != nil { + return err + } + + return ctx.HTML(statusCode, buf.String()) +} + +func IndexHandler(c echo.Context) error { + return Render(c, http.StatusOK, ui.Index()) +} diff --git a/api/v1/apiserver.go b/web/web.go similarity index 83% rename from api/v1/apiserver.go rename to web/web.go index e8985df..f25ea9d 100644 --- a/api/v1/apiserver.go +++ b/web/web.go @@ -1,10 +1,11 @@ -package api +package web import ( "crypto/subtle" "github.com/d4sw4r/blokhouse/internal/service" - "github.com/d4sw4r/blokhouse/web" + api "github.com/d4sw4r/blokhouse/web/api/v1" + "github.com/d4sw4r/blokhouse/web/ui" "github.com/labstack/echo-contrib/echoprometheus" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -21,8 +22,9 @@ func NewServer(ListenAddr string, svc service.AssetSvc) Server { func (s Server) Start() error { e := echo.New() + api := api.NewAPI() e.Use(bindApp(s.Svc)) - e.GET("/", web.Index) + e.GET("/", ui.IndexHandler) e.Use(echoprometheus.NewMiddleware("blockhouse")) e.GET("/metrics", echoprometheus.NewHandler()) e.Use(middleware.Logger()) @@ -32,11 +34,11 @@ func (s Server) Start() error { })) e.Static("/static", "assets") - e.GET("/assets", listAssets) - e.POST("/assets", createAsset) - e.GET("/assets/:id", findAsset) + e.GET("/assets", api.ListAssets) + e.POST("/assets", api.CreateAsset) + e.GET("/assets/:id", api.FindAsset) //e.PUT("/assets/:id", updateUser) - e.DELETE("/assets/:id", deleteAsset) + e.DELETE("/assets/:id", api.DeleteAsset) return e.Start(s.ListenAddr) } diff --git a/web/web_handler.go b/web/web_handler.go deleted file mode 100644 index df939da..0000000 --- a/web/web_handler.go +++ /dev/null @@ -1,11 +0,0 @@ -package web - -import ( - "net/http" - - "github.com/labstack/echo/v4" -) - -func Index(c echo.Context) error { - return c.String(http.StatusOK, "Hello, World!") -}