-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (56 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"embed"
"fmt"
"html/template"
"log"
"net/http"
)
//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
}
type EditRow struct {
Value int `name:"count" template:"count"`
}
func (b *Backend) SubmitFormEditRow(fruitID int, form EditRow) EditRowPage {
if fruitID < 0 || fruitID >= len(b.data) {
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}
row := b.data[fruitID]
row.Value = form.Value
b.data[fruitID] = row
return EditRowPage{Error: nil, Row: row}
}
func (b *Backend) GetFormEditRow(fruitID int) EditRowPage {
if fruitID < 0 || fruitID >= len(b.data) {
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}
return EditRowPage{Error: nil, Row: b.data[fruitID]}
}
type Row struct {
ID int
Name string
Value int
}
func (b *Backend) List(_ context.Context) []Row { return b.data }
//go:generate go run ../cmd/muxt generate --receiver-type Backend
func main() {
backend := &Backend{
data: []Row{
{ID: 0, Name: "Peach", Value: 10},
{ID: 1, Name: "Plum", Value: 20},
{ID: 2, Name: "Pineapple", Value: 2},
},
}
mux := http.NewServeMux()
routes(mux, backend)
log.Fatal(http.ListenAndServe(":8080", mux))
}