Skip to content

Commit

Permalink
WIP Working on incorporating Templ templates
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 b2546fd commit b1b5065
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 11 deletions.
8 changes: 5 additions & 3 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ tmp_dir = "tmp"
[build]
args_bin = []
bin = "./bin/weesvc"
cmd = "go build -o ./bin/weesvc .; ./bin/weesvc migrate"
## Templ will be watching for template changes, which then regenerate Go files.
## We just need to rebuild and notify the proxy to refresh the browser.
cmd = "go build -o ./bin/weesvc . && templ generate --notify-proxy"
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"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
# Ignore other project items
artifacts/
bin/
tmp/
gorm.db
*_templ.go
*_templ.txt
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ test:

## imports: Organizes imports within the codebase.
imports:
echo "Organizing imports..."
goimports -w -l --local $(BASE_MODULE) .
echo "[SKIPPING] Organizing imports..."
# goimports -w -l --local $(BASE_MODULE) .

## fmt: Applies appropriate formatting on the codebase.
fmt:
Expand Down Expand Up @@ -109,7 +109,9 @@ release-docker: build-docker
## develop: Start the application in hot-reload mode.
develop: build-only
go install github.com/cosmtrek/air@latest
air -c ./.air.toml -- serve
go install github.com/a-h/templ/cmd/templ@latest
templ generate --watch --proxy=http://localhost:9092 &
air -c ./.air.toml -- serve --server-port=9092


.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
-
4 changes: 2 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func newServeCommand(config *config.Config) *cobra.Command {
},
}

serveCmd.PersistentFlags().IntVarP(&config.Port, "api-port", "p", 9092, "port to access the api")
serveCmd.PersistentFlags().IntVarP(&config.Port, "server-port", "p", 9092, "port to access the api")
serveCmd.PersistentFlags().StringVar(&config.Dialect, "dialect", "sqlite3", "database dialect")
serveCmd.PersistentFlags().StringVar(&config.DatabaseURI, "database-uri", "", "database connection string")

_ = viper.BindPFlag("Port", serveCmd.PersistentFlags().Lookup("api-port"))
_ = viper.BindPFlag("Port", serveCmd.PersistentFlags().Lookup("server-port"))
_ = viper.BindPFlag("Dialect", serveCmd.PersistentFlags().Lookup("dialect"))
_ = viper.BindPFlag("DatabaseURI", serveCmd.PersistentFlags().Lookup("database-uri"))

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/weesvc/weesvc-gorilla
go 1.22.3

require (
github.com/a-h/templ v0.2.680
github.com/google/uuid v1.6.0
github.com/gorilla/handlers v1.5.2
github.com/gorilla/mux v1.8.1
Expand All @@ -21,7 +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/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/containerd v1.7.11 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ 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.680 h1:TflYFucxp5rmOxAXB9Xy3+QHTk8s8xG9+nCT/cLzjeE=
github.com/a-h/templ v0.2.680/go.mod h1:NQGQOycaPKBxRB14DmAaeIpcGC1AOBPJEMO4ozS7m90=
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=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/containerd v1.7.11 h1:lfGKw3eU35sjV0aG2eYZTiwFEY1pCzxdzicHP3SZILw=
github.com/containerd/containerd v1.7.11/go.mod h1:5UluHxHTX2rdvYuZ5OJTC5m/KJNs0Zs9wVoJm9zf5ZE=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand Down
4 changes: 4 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"sync"
"time"

"github.com/a-h/templ"
"github.com/weesvc/weesvc-gorilla/internal/config"
"github.com/weesvc/weesvc-gorilla/internal/server/views"

"github.com/pkg/errors"

Expand Down Expand Up @@ -62,6 +64,8 @@ func serveAPI(ctx context.Context, app *app.App, api *api.API) {
router := mux.NewRouter()
api.Init(router.PathPrefix("/api").Subrouter())

router.Handle("/", templ.Handler(views.Index()))

s := &http.Server{
Addr: fmt.Sprintf(":%d", app.Config.Port),
Handler: cors(router),
Expand Down
13 changes: 13 additions & 0 deletions internal/server/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>
}

0 comments on commit b1b5065

Please sign in to comment.