From cae10393a6289eb1fea22600dea50790d0dc742f Mon Sep 17 00:00:00 2001 From: Chrstopher Hunter <8398225+crhntr@users.noreply.github.com> Date: Mon, 19 Aug 2024 18:49:08 -0700 Subject: [PATCH] focus on the templates --- README.md | 151 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 77 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 3069bba..f977b88 100644 --- a/README.md +++ b/README.md @@ -4,80 +4,6 @@ This is especially helpful when you are writing HTMX. Given [main.go](#main_go) and [index.gohtml](#index_gohtml), muxt will generate [template_routes.go](#template_routes_go). -### main.go -```go -package main - -import ( - "context" - "embed" - "fmt" - "html/template" - "log" - "net/http" - "strconv" -) - -//go:embed *.gohtml -var templateSource embed.FS - -var templates = template.Must(template.ParseFS(templateSource, "*")) - -type Backend struct { - data []Row -} - -type EditRowPage struct { - Row Row - Error error -} - -func (b *Backend) SubmitFormEditRow(request *http.Request, fruit string) EditRowPage { - count, err := strconv.Atoi(request.FormValue("count")) - if err != nil { - return EditRowPage{Error: err, Row: Row{Name: fruit}} - } - for i := range b.data { - if b.data[i].Name == fruit { - b.data[i].Value = count - return EditRowPage{Error: nil, Row: b.data[i]} - } - } - return EditRowPage{Error: fmt.Errorf("fruit not found")} -} - -func (b *Backend) GetFormEditRow(fruit string) EditRowPage { - for i := range b.data { - if b.data[i].Name == fruit { - return EditRowPage{Error: nil, Row: b.data[i]} - } - } - return EditRowPage{Error: fmt.Errorf("fruit not found")} -} - -type Row struct { - Name string - Value int -} - -func (b *Backend) List(_ context.Context) []Row { return b.data } - -//go:generate muxt generate --receiver Backend - -func main() { - backend := &Backend{ - data: []Row{ - {Name: "Peach", Value: 10}, - {Name: "Plum", Value: 20}, - {Name: "Pineapple", Value: 2}, - }, - } - mux := http.NewServeMux() - Routes(mux, backend) - log.Fatal(http.ListenAndServe(":8080", mux)) -} -``` - ### index.gohtml ```html @@ -158,6 +84,83 @@ func main() { {{end}} ``` +### main.go + +In your Go code focus on your domain. Let muxt generate handlers and wire them up with the HTTP mux (multiplexer). + +```go +package main + +import ( + "context" + "embed" + "fmt" + "html/template" + "log" + "net/http" + "strconv" +) + +//go:embed *.gohtml +var templateSource embed.FS + +var templates = template.Must(template.ParseFS(templateSource, "*")) + +type Backend struct { + data []Row +} + +type EditRowPage struct { + Row Row + Error error +} + +func (b *Backend) SubmitFormEditRow(request *http.Request, fruit string) EditRowPage { + count, err := strconv.Atoi(request.FormValue("count")) + if err != nil { + return EditRowPage{Error: err, Row: Row{Name: fruit}} + } + for i := range b.data { + if b.data[i].Name == fruit { + b.data[i].Value = count + return EditRowPage{Error: nil, Row: b.data[i]} + } + } + return EditRowPage{Error: fmt.Errorf("fruit not found")} +} + +func (b *Backend) GetFormEditRow(fruit string) EditRowPage { + for i := range b.data { + if b.data[i].Name == fruit { + return EditRowPage{Error: nil, Row: b.data[i]} + } + } + return EditRowPage{Error: fmt.Errorf("fruit not found")} +} + +type Row struct { + Name string + Value int +} + +func (b *Backend) List(_ context.Context) []Row { return b.data } + +//go:generate muxt generate --receiver Backend + +func main() { + backend := &Backend{ + data: []Row{ + {Name: "Peach", Value: 10}, + {Name: "Plum", Value: 20}, + {Name: "Pineapple", Value: 2}, + }, + } + mux := http.NewServeMux() + Routes(mux, backend) + log.Fatal(http.ListenAndServe(":8080", mux)) +} +``` + ### template_routes.go This file is generated by running `go generate` in the same directory as `main.go` ```go