Skip to content

Commit

Permalink
Merge pull request #12 from weesvc/stlgo/htmx-starter-03-htmx
Browse files Browse the repository at this point in the history
Adding Air, Templ, and HTMX
  • Loading branch information
javaducky authored May 23, 2024
2 parents 20826ee + d556fa3 commit 01a78ec
Show file tree
Hide file tree
Showing 37 changed files with 18,962 additions and 71 deletions.
48 changes: 48 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./bin/weesvc"
## 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", "_templ.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "templ", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = true

[screen]
clear_on_rebuild = false
keep_scroll = true
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: 1.22.x
- name: Generate templates and build binary
run: |
make build-only
- name: Run unit tests
run: go test -test.short -v -cover -race ./...
run: go test -test.short -v -cover ./...

check-compliance:
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: 1.22.x
- name: Generate templates and build binary
run: |
make build-only
- name: Check module dependencies
run: |
go version
Expand All @@ -31,6 +34,9 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: 1.22.x
- name: Generate templates and build binary
run: |
make build-only
- name: Retrieve golangci-lint version
run: |
echo "Version=$(head -n 1 "${GITHUB_WORKSPACE}/.golangci.yml" | tr -d '# ')" >> $GITHUB_OUTPUT
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
19 changes: 14 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DOCKER_IMAGE := $(PROJECT_MODULE)
DOCKER_TAG := $(BUILD_VERSION)

# Linker Flags
LINKER_FLAGS := "-X $(PROJECT_MODULE)/env.Version=$(BUILD_VERSION) -X $(PROJECT_MODULE)/env.Revision=$(BUILD_REVISION)"
LINKER_FLAGS := "-X $(PROJECT_MODULE)/internal/env.Version=$(BUILD_VERSION) -X $(PROJECT_MODULE)/internal/env.Revision=$(BUILD_REVISION)"


all: imports fmt vet build
Expand Down 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 All @@ -76,21 +76,24 @@ setup:
echo "Installing tools..."
go install golang.org/x/tools/cmd/goimports@latest


## build: Build the application.
build: deps imports fmt vet build-only

## build-only: Build without prerequisite steps
build-only:
echo "Building '${PROJECT_NAME}'..."
mkdir -v -p $(CURDIR)/bin
go install github.com/a-h/templ/cmd/templ@latest
templ generate .
go build -v \
-ldflags $(LINKER_FLAGS) \
-o "bin/$(PROJECT_NAME)" .

## build-all: Builds all architectures of the application.
build-all: deps imports fmt vet
mkdir -v -p $(CURDIR)/artifacts
go install github.com/a-h/templ/cmd/templ@latest
templ generate .
gox -verbose \
-os "$(BUILD_OS)" -arch "$(BUILD_ARCH)" \
-ldflags $(LINKER_FLAGS) \
Expand All @@ -107,8 +110,14 @@ build-docker:
release-docker: build-docker
docker push $(DOCKER_IMAGE):$(DOCKER_TAG)

## develop: Start the application in hot-reload mode.
develop: build-only
go install github.com/cosmtrek/air@latest
templ generate --watch --proxy=http://localhost:9092 &
air -c ./.air.toml -- serve -c config-postgres.yaml --server-port=9092 --resource-caching-enabled=false


.PHONY: build build-all \
clean clean-all clean-artifacts \
deps fmt help imports \
deps develop fmt help imports \
setup test vet
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func newRootCommand() *cobra.Command {
},
}

rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file")
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "config file")
rootCmd.PersistentFlags().BoolVar(&cfg.Verbose, "verbose", false, "verbose output")

_ = viper.BindPFlag("Verbose", rootCmd.PersistentFlags().Lookup("verbose"))
Expand Down
13 changes: 9 additions & 4 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ func newServeCommand(config *config.Config) *cobra.Command {
serveCmd := &cobra.Command{
Use: "serve",
Short: "Starts the application server",
RunE: func(_ *cobra.Command, _ []string) error {
return server.StartServer(config)
Run: func(_ *cobra.Command, _ []string) {
server.StartServer(config)
},
}

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")
serveCmd.PersistentFlags().BoolVar(&config.ResourceCachingEnabled,
"resource-caching-enabled",
true,
"enable browser caching of web resources")

_ = 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"))
_ = viper.BindPFlag("ResourceCachingEnabled", serveCmd.PersistentFlags().Lookup("resource-caching-enabled"))

return serveCmd
}
4 changes: 4 additions & 0 deletions config-postgres.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DatabaseURI: "postgres://postgres:postgres@localhost:5432/weesvc?sslmode=disable"
# Specify dialect as "postgres" or "sqlite3". "sqlite3" is default.
Dialect: postgres
Verbose: false
12 changes: 12 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
db:
image: postgres:15.3-alpine
volumes:
- ./testdata/:/docker-entrypoint-initdb.d/
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: weesvc
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
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.697
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.697 h1:OILxtWvD0NRJaoCOiZCopRDPW8paroKlGsrAiHLykNE=
github.com/a-h/templ v0.2.697/go.mod h1:5cqsugkq9IerRNucNsI4DEamdHPsoGMQy99DzydLhM8=
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
5 changes: 5 additions & 0 deletions internal/app/place.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ func (ctx *Context) GetPlaces() ([]*model.Place, error) {
return ctx.Database.GetPlaces()
}

// SearchPlaces returns available places.
func (ctx *Context) SearchPlaces(s string) ([]*model.Place, error) {
return ctx.Database.SearchPlaces(s)
}

// GetPlaceByID returns the place specified by the provided identifier.
func (ctx *Context) GetPlaceByID(id uint) (*model.Place, error) {
place, err := ctx.Database.GetPlaceByID(id)
Expand Down
5 changes: 4 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ type Config struct {
Dialect string
DatabaseURI string
Verbose bool
// The port to bind the web application api to
// Port to bind the web application api to
Port int
// ResourceCachingEnabled denotes caching headers are applied to static web resources
// Tip: disable during development to immediately reflect resource changes, e.g. CSS and images
ResourceCachingEnabled bool
}

// NewConfig returns an initialized, but empty, configuration object.
Expand Down
11 changes: 11 additions & 0 deletions internal/db/place.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package db

import (
"strings"

"github.com/pkg/errors"

"github.com/weesvc/weesvc-gorilla/internal/model"
Expand Down Expand Up @@ -32,3 +34,12 @@ func (db *Database) UpdatePlace(place *model.Place) error {
func (db *Database) DeletePlaceByID(id uint) error {
return errors.Wrap(db.Delete(&model.Place{}, id).Error, "unable to delete place")
}

func (db *Database) SearchPlaces(s string) ([]*model.Place, error) {
var places []*model.Place
s = strings.ToLower(s)
return places, errors.Wrap(
db.Where("LOWER(name) LIKE ? OR LOWER(description) LIKE ?",
"%"+s+"%", "%"+s+"%").Limit(10).Find(&places).Error,
"unable to find places")
}
4 changes: 2 additions & 2 deletions internal/db/place_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestDatabase_GetPlaces(t *testing.T) {

places, err := placeDB.GetPlaces()
assert.NoError(t, err)
assert.Equal(t, 10, len(places))
assert.Equal(t, 20, len(places))
}

func TestDatabase_GetPlaceByID(t *testing.T) {
Expand All @@ -45,7 +45,7 @@ func TestDatabase_CreatePlace(t *testing.T) {
placeDB := setupDatabase(t)

newPlace := &model.Place{
ID: 20,
ID: 9999,
Name: "Kerid Crater",
Description: "Kerid Crater, Iceland",
Latitude: 64.04126,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 01a78ec

Please sign in to comment.