This module is a simple example of a web server using templ
+ htmx
+ tailwind
.
The web server maintains a map of User
s and the frontend allows vistors to
the site to view, create, edit, and destroy Users
.
It is extremely basic and only intended to demonstrate some basic elements of the featured tools.
- ✨ CRUD actions implemented with htmx
- ✨ HTML elements generated with templ
- ✨ styled with Tailwind
- ✨ non-tailwind branch available
- ✨ runs with Task
- ✨ live reloads with Air
- ✨ tailwind and htmx are vendor'ed
- ✨ server interface generated with oapi-codegen
(you need
task
becauseair
uses atask
command to build the binary it runs).
air
go run cmd/main.go
Assuming you already have Go 1.21+ installed, you can just use the go install
command.
go install github.com/go-task/task/v3/cmd/task@latest
go install github.com/cosmtrek/air@latest
But there are lots of ways to install both.
You don't really need air
and task
, but they are helpful.
go install github.com/go-task/task/v3/cmd/task@latest
go install github.com/cosmtrek/air@latest
But you will need oapi-codegen
+ templ
.
go install github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@latest
go install github.com/a-h/templ/cmd/templ@latest
- Add a path to
openapi.yaml
paths:
/:
get:
operationId: HomePage
# ...
# ...
# ...
/new/route:
get:
operationId: NewRoute
- Generate the code with
task
(or look at the relevant commands in theTaskfile.yml
).
task api
- Create a whatever html you want with
templ
components.
templ newRoute() {
@baseLayout("new route") {
<div>
This is a new page.
</div>
}
}
- Implement the interface as method to the server and render the html.
func (s Server) NewRoute() {
templ.Handler(newRoutePage()).ServeHTTP(w, r)
}
- Run the server with
air
and go tolocalhost:800/new/route
air