Skip to content

Commit

Permalink
begin creating examples
Browse files Browse the repository at this point in the history
  • Loading branch information
will-wow committed Feb 26, 2024
1 parent 5cd489b commit c960689
Show file tree
Hide file tree
Showing 27 changed files with 2,206 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/templ/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.task
bin
62 changes: 62 additions & 0 deletions examples/templ/.vscode/examples.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
// Place your templ workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
"handler": {
"scope": "go",
"prefix": "handler",
"body": [
"package ${TM_DIRECTORY/.*\\/(.*)$/$1/}",
"",
"import \"net/http\"",
"",
"func Handler() http.Handler {",
" mux := http.NewServeMux()",
"",
" mux.HandleFunc(\"GET /\", demo)",
"",
" return mux",
"}",
"",
"func demo(w http.ResponseWriter, r *http.Request) {",
" component := page()",
" w.WriteHeader(http.StatusOK)",
" _ = component.Render(r.Context(), w)",
"}",
"",
],
"description": "example handler file",
},
"page": {
"scope": "templ",
"prefix": "page",
"body": [
"package bulkupdate",
"",
"import (",
" \"github.com/lithammer/dedent\"",
"",
" \"github.com/will-wow/typed-htmx-go/examples/templ/internal/web/layout\"",
" \"github.com/will-wow/typed-htmx-go/hx\"",
" \"github.com/will-wow/typed-htmx-go/hx/swap\"",
")",
"",
"templ page() {",
" @layout.Base(\"$1\") {",
" <h1>$1</h1>",
" <p>",
" Desc",
" </p>",
" <pre>",
" { dedent.Dedent(`",
" `) }",
" </pre>",
" }",
"}",
],
},
}
56 changes: 56 additions & 0 deletions examples/templ/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
version: "3"

tasks:
build:
desc: Build the server binary
deps: [gen]
cmds:
- go build -v -o ./bin/server ./cmd/server/main.go

start:
desc: Run built server
cmds:
- ./bin/server

dev:
desc: Run the server with a watch for templ, go, and static files
cmds:
- "wgo -file=.go -file=.templ -xfile=_templ.go -file=static/.+.js -file=static/.+.css task gen :: go run cmd/server/main.go"

lint:
desc: Run golangci-lint
cmds:
- go run github.com/golangci/golangci-lint/cmd/[email protected] run

fmt:
desc: Run goimports
cmds:
- go mod tidy
- go run golang.org/x/tools/cmd/[email protected] -w -local github.com/will-wow/typed-htmx-go/examples/templ .

test:
desc: Run test suite
cmds:
- go test -v {{.CLI_ARGS}} ./...

test-cover:
desc: Run test suite with coverage
cmds:
- go test -coverpkg ./... -coverprofile=coverage.txt ./...
- go tool cover -html=coverage.txt

gen:
desc: Generate templ components
cmds:
- templ generate
# Re-format generated code
- go run golang.org/x/tools/cmd/[email protected] -w -local github.com/will-wow/typed-htmx-go/examples/templ ./
sources:
- "**/*.templ"
generates:
- "**/*_templ.go"

tools:
desc: Install tools
cmds:
- go install github.com/a-h/templ/cmd/templ@latest
5 changes: 5 additions & 0 deletions examples/templ/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# api

Exposes the templ/examples as a Vercel function.

See the [Vercel docs](https://vercel.com/docs/functions/runtimes/go) for more details.
12 changes: 12 additions & 0 deletions examples/templ/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api

import (
"net/http"

"github.com/will-wow/typed-htmx-go/examples/templ/internal/web"
)

func Handler(w http.ResponseWriter, r *http.Request) {
handler := web.NewHttpHandler()
handler.ServeHTTP(w, r)
}
28 changes: 28 additions & 0 deletions examples/templ/cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"net/http"
"os"
"time"

"github.com/will-wow/typed-htmx-go/examples/templ/internal/web"
)

func main() {
handler := web.NewHttpHandler()

//nolint:exhaustruct
server := &http.Server{
Addr: "localhost:8080",
Handler: handler,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}

fmt.Printf("Listening on %v\n", server.Addr)
err := server.ListenAndServe()
if err != nil {
os.Exit(1)
}
}
10 changes: 10 additions & 0 deletions examples/templ/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/will-wow/typed-htmx-go/examples/templ

go 1.22.0

require (
github.com/a-h/templ v0.2.543
github.com/will-wow/typed-htmx-go v0.0.3
)

require github.com/lithammer/dedent v1.1.0
8 changes: 8 additions & 0 deletions examples/templ/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/a-h/templ v0.2.543 h1:8YyLvyUtf0/IE2nIwZ62Z/m2o2NqwhnMynzOL78Lzbk=
github.com/a-h/templ v0.2.543/go.mod h1:jP908DQCwI08IrnTalhzSEH9WJqG/Q94+EODQcJGFUA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/will-wow/typed-htmx-go v0.0.3 h1:zF0ESFMm2Ry2OEe+mQ+2zcULM1aEqWaDwdyGEZWC/1w=
github.com/will-wow/typed-htmx-go v0.0.3/go.mod h1:vBV9acu4/cjaeLc1Z2z4ZruJYpF1KdCoFebp3CWNaPA=
65 changes: 65 additions & 0 deletions examples/templ/internal/web/bulkupdate/bulkupdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package bulkupdate

import (
"fmt"
"net/http"
)

func Handler() http.Handler {
mux := http.NewServeMux()

mux.HandleFunc("GET /", demo)
mux.HandleFunc("POST /", post)

return mux
}

type userModel struct {
name string
email string
active bool
}

func demo(w http.ResponseWriter, r *http.Request) {
users := defaultUsers()

component := page(users)
w.WriteHeader(http.StatusOK)
_ = component.Render(r.Context(), w)
}

func post(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

users := defaultUsers()

var additions int
var removals int

for _, user := range users {
if r.Form.Has(user.email) && !user.active {
additions++
} else if !r.Form.Has(user.email) && user.active {
removals++
}
}

toast := fmt.Sprintf("Activated %d and deactivated %d users", additions, removals)

component := updateToast(toast)
w.WriteHeader(http.StatusOK)
_ = component.Render(r.Context(), w)
}

func defaultUsers() []userModel {
return []userModel{
{name: "Joe Smith", email: "[email protected]", active: true},
{name: "Angie MacDowell", email: "[email protected]", active: true},
{name: "Fuqua Tarkenton", email: "[email protected]", active: true},
{name: "Kim Yee", email: "[email protected]", active: false},
}
}
Loading

0 comments on commit c960689

Please sign in to comment.