-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (50 loc) · 1.12 KB
/
main.go
File metadata and controls
57 lines (50 loc) · 1.12 KB
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
package main
import (
"cmp"
"context"
"fmt"
"log"
"net/http"
"os"
"slices"
"sync"
)
type Backend struct {
sync.RWMutex
data []Row
}
func (b *Backend) List(_ context.Context) []Row {
b.RLock()
defer b.RUnlock()
return slices.Clone(b.data)
}
func (b *Backend) SubmitFormEditRow(fruitID int, form EditRow) (Row, error) {
return b.findRow(fruitID, func(row *Row) { row.Value = form.Value })
}
func (b *Backend) GetFormEditRow(fruitID int) (Row, error) { return b.findRow(fruitID, nil) }
func (b *Backend) findRow(fruitID int, update func(row *Row)) (Row, error) {
b.RLock()
defer b.RUnlock()
index := slices.IndexFunc(b.data, func(row Row) bool {
return row.ID == fruitID
})
if index < 0 {
return Row{}, fmt.Errorf("fruit not found")
}
if update != nil {
update(&b.data[index])
}
return b.data[index], nil
}
func main() {
backend := &Backend{
data: []Row{
{ID: 1, Name: "Peach", Value: 10},
{ID: 2, Name: "Plum", Value: 20},
{ID: 3, Name: "Pineapple", Value: 2},
},
}
mux := http.NewServeMux()
TemplateRoutes(mux, backend)
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), "8080"), mux))
}