From c3ee26b8243eabaca8d4f7a3019dfa8f472147a7 Mon Sep 17 00:00:00 2001 From: Paul Balogh Date: Sun, 19 May 2024 10:42:42 -0500 Subject: [PATCH] WIP To be squashed Signed-off-by: Paul Balogh --- .air.toml | 6 +++--- Makefile | 6 +++++- NOTES-stlgo-htmx.md | 20 ++++++++++++++++++++ cmd/serve.go | 8 ++++++-- go.mod | 1 + go.sum | 2 ++ views/index.templ | 13 +++++++++++++ views/routes.go | 31 +++++++++++++++++++++++++++++++ 8 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 NOTES-stlgo-htmx.md create mode 100644 views/index.templ create mode 100644 views/routes.go diff --git a/.air.toml b/.air.toml index 4c53850..d88c57b 100644 --- a/.air.toml +++ b/.air.toml @@ -5,16 +5,16 @@ tmp_dir = "tmp" [build] args_bin = [] bin = "./bin/weesvc" - cmd = "go build -o ./bin/weesvc .; ./bin/weesvc migrate" + cmd = "templ generate --notify-proxy && go build -o ./bin/weesvc .; ./bin/weesvc migrate" delay = 1000 exclude_dir = ["assets", "tmp", "vendor", "testdata"] exclude_file = [] - exclude_regex = ["_test.go"] + exclude_regex = [".*_test.go", ".*_templ.go"] exclude_unchanged = false follow_symlink = false full_bin = "" include_dir = [] - include_ext = ["go", "tpl", "tmpl", "html"] + include_ext = ["go", "tpl", "tmpl", "templ", "html", "css", "js", "png"] include_file = [] kill_delay = "0s" log = "build-errors.log" diff --git a/Makefile b/Makefile index 57ae0ff..f01c47d 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,7 @@ setup: echo "Installing tools..." go install golang.org/x/tools/cmd/goimports@latest go install github.com/cosmtrek/air@latest + go install github.com/a-h/templ/cmd/templ@latest ## build: Build the application. build: deps imports fmt vet build-only @@ -109,7 +110,10 @@ release-docker: build-docker ## develop: Start the application in hot-reload mode. develop: build-only - air -c ./.air.toml -- serve + go install github.com/cosmtrek/air@latest + go install github.com/a-h/templ/cmd/templ@latest + templ generate --watch --proxy=http://localhost:9092 & + air -c ./.air.toml - serve .PHONY: build build-all \ diff --git a/NOTES-stlgo-htmx.md b/NOTES-stlgo-htmx.md new file mode 100644 index 0000000..57eb78f --- /dev/null +++ b/NOTES-stlgo-htmx.md @@ -0,0 +1,20 @@ +# Getting Started with Go, Templ, HTMX, and Air...Oh my! + +## Installing Air +- https://github.com/cosmtrek/air +- As a Go module: `go install github.com/cosmtrek/air@latest` (v1.51.0 at this time) +- Run `air init` to create a basic `.air.toml` configuration file +- I'm adding to Makefile as `make develop` target +- Updated the TOML config for `build.cmd` to trigger a database migration on each restart +- Updated the TOML config for `misc.clean_on_exit=true` +- From terminal, run a request as `http POST :9092/api/places name=NISC description="NISC Lake St. Louis Office" latitude:=38.7839 longitude:=90.7878` +- Run `http GET :9092/api/places/1` to retrieve listing +- Change a JSON attribute name for `model/place.go`, then run GET again to show changed attribute + + +## Starting with Templ +- https://github.com/a-h/templ, https://templ.guide/ +- Install Templ CLI as a Go module `go install github.com/a-h/templ/cmd/templ@latest` +- Add use of the CLI to `make develop` to have a watcher on templates to auto-generate Go code from templates +- Update Air configuration to let Templ watch for template changes, then Air will watch for Go changes +- \ No newline at end of file diff --git a/cmd/serve.go b/cmd/serve.go index 2aa09b7..57c913b 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -9,6 +9,8 @@ import ( "sync" "time" + "github.com/weesvc/weesvc-gorilla/views" + "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/sirupsen/logrus" @@ -18,7 +20,7 @@ import ( "github.com/weesvc/weesvc-gorilla/app" ) -func serveAPI(ctx context.Context, api *api.API) { +func serveAPI(ctx context.Context, api *api.API, ui *views.UI) { cors := handlers.CORS( handlers.AllowedOrigins([]string{"*"}), handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "OPTIONS"}), @@ -26,6 +28,7 @@ func serveAPI(ctx context.Context, api *api.API) { router := mux.NewRouter() api.Init(router.PathPrefix("/api").Subrouter()) + ui.Init(router.PathPrefix("/").Subrouter()) s := &http.Server{ Addr: fmt.Sprintf(":%d", api.Config.Port), @@ -60,6 +63,7 @@ var serveCmd = &cobra.Command{ } api := api.New(a) + ui := views.New(a) ctx, cancel := context.WithCancel(context.Background()) @@ -77,7 +81,7 @@ var serveCmd = &cobra.Command{ go func() { defer wg.Done() defer cancel() - serveAPI(ctx, api) + serveAPI(ctx, api, ui) }() wg.Wait() diff --git a/go.mod b/go.mod index 42ef15b..ddaf319 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect github.com/Microsoft/hcsshim v0.11.4 // indirect + github.com/a-h/templ v0.2.663 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/containerd/containerd v1.7.11 // indirect github.com/containerd/log v0.1.0 // indirect diff --git a/go.sum b/go.sum index d6eb406..01fc469 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5 github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7fz8= github.com/Microsoft/hcsshim v0.11.4/go.mod h1:smjE4dvqPX9Zldna+t5FG3rnoHhaB7QYxPRqGcpAD9w= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= +github.com/a-h/templ v0.2.663 h1:aa0WMm27InkYHGjimcM7us6hJ6BLhg98ZbfaiDPyjHE= +github.com/a-h/templ v0.2.663/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= diff --git a/views/index.templ b/views/index.templ new file mode 100644 index 0000000..73f3acc --- /dev/null +++ b/views/index.templ @@ -0,0 +1,13 @@ +package views + +templ Index() { + + + + StL Go! + + + Hello STLGo From Templ! + + +} diff --git a/views/routes.go b/views/routes.go new file mode 100644 index 0000000..ed09602 --- /dev/null +++ b/views/routes.go @@ -0,0 +1,31 @@ +package views + +import ( + "github.com/a-h/templ" + "github.com/gorilla/mux" + + "github.com/weesvc/weesvc-gorilla/app" +) + +// UI represents the context for the public interface. +type UI struct { + App *app.App + //Config *Config +} + +// New creates a new API instance. +func New(a *app.App) (ui *UI) { + ui = &UI{App: a} + // ui.Config = initConfig() + return ui +} + +// Init is where we define the routes our API will support. +func (ui *UI) Init(r *mux.Router) { + //places, err := ui.App.NewContext().GetPlaces() + //if err != nil { + // + //} + + r.Handle("/", templ.Handler(Index())) +}