Skip to content

Commit

Permalink
WIP To be squashed
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Balogh <[email protected]>
  • Loading branch information
javaducky committed May 19, 2024
1 parent 45c3c78 commit c3ee26b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
20 changes: 20 additions & 0 deletions NOTES-stlgo-htmx.md
Original file line number Diff line number Diff line change
@@ -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
-
8 changes: 6 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"time"

"github.com/weesvc/weesvc-gorilla/views"

Check failure on line 12 in cmd/serve.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/weesvc/weesvc-gorilla/views (-: # github.com/weesvc/weesvc-gorilla/views

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
Expand All @@ -18,14 +20,15 @@ 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"}),
)

router := mux.NewRouter()
api.Init(router.PathPrefix("/api").Subrouter())
ui.Init(router.PathPrefix("/").Subrouter())

s := &http.Server{
Addr: fmt.Sprintf(":%d", api.Config.Port),
Expand Down Expand Up @@ -60,6 +63,7 @@ var serveCmd = &cobra.Command{
}

api := api.New(a)
ui := views.New(a)

ctx, cancel := context.WithCancel(context.Background())

Expand All @@ -77,7 +81,7 @@ var serveCmd = &cobra.Command{
go func() {
defer wg.Done()
defer cancel()
serveAPI(ctx, api)
serveAPI(ctx, api, ui)
}()

wg.Wait()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 13 additions & 0 deletions views/index.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package views

templ Index() {
<!DOCTYPE html>
<html>
<head>
<title>StL Go!</title>
</head>
<body>
Hello STLGo From Templ!
</body>
</html>
}
31 changes: 31 additions & 0 deletions views/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package views

Check failure on line 1 in views/routes.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/weesvc/weesvc-gorilla/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()))

Check failure on line 30 in views/routes.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Index (typecheck)

Check failure on line 30 in views/routes.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Index) (typecheck)
}

0 comments on commit c3ee26b

Please sign in to comment.