Skip to content

Commit

Permalink
restructure and add tailwind, daisy-ui and htmx
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Werner authored and Dennis Werner committed Jun 5, 2024
1 parent 448a350 commit cf3c948
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*_templ.go
*_templ.go
blokhouse
4 changes: 2 additions & 2 deletions cmd/blokhouse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ 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() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
db := storage.NewMemoryStorage()
svc := service.NewAssetSvc(db)
s := api.NewServer(":9000", svc)
s := web.NewServer(":9000", svc)
log.Fatal(s.Start())
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down
7 changes: 7 additions & 0 deletions web/api/v1/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package api

type API struct{}

func NewAPI() API {
return API{}
}
8 changes: 4 additions & 4 deletions api/v1/asset_handler.go → web/api/v1/asset_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -34,15 +34,15 @@ 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)
as, _ := s.List()
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 {
Expand Down
11 changes: 11 additions & 0 deletions web/ui/view/index.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ui

templ Index() {
@template(){
<div> hello world </div>
<div><button hx-post="/clicked" hx-swap="outerHTML">
Click Me
</button>
</div>
}
}
15 changes: 15 additions & 0 deletions web/ui/view/template.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ui

templ template() {
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/[email protected]"></script>
</head>
<body>
{ children... }
</body>
</html>
}
25 changes: 25 additions & 0 deletions web/ui/web_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ui

import (
"net/http"

"github.com/a-h/templ"
ui "github.com/d4sw4r/blokhouse/web/ui/view"

Check failure on line 7 in web/ui/web_handler.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/d4sw4r/blokhouse/web/ui/view; to add it:
"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())
}
16 changes: 9 additions & 7 deletions api/v1/apiserver.go → web/web.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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())
Expand All @@ -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)
}
Expand Down
11 changes: 0 additions & 11 deletions web/web_handler.go

This file was deleted.

0 comments on commit cf3c948

Please sign in to comment.