-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (151 loc) · 4.51 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"net/http"
"os"
"github.com/calvinmclean/babyapi"
"github.com/calvinmclean/babyapi/extensions"
"github.com/calvinmclean/babyapi/html"
"github.com/go-chi/render"
)
const (
allSNACKs html.Template = "allSNACKs"
allSNACKsTemplate string = `<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Full Stack Snacks</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/uikit.min.css" />
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]/sse.js"></script>
</head>
<style>
tr.htmx-swapping td {
opacity: 0;
transition: opacity 1s ease-out;
}
</style>
<body>
<div class="uk-container uk-margin-top text-center">
<h1>Full Stack Snacks</h1>
</div>
<table class="uk-table uk-table-divider uk-margin-left uk-margin-right">
<colgroup>
<col>
<col>
<col style="width: 300px;">
</colgroup>
<thead>
<tr>
<th>Snack</th>
<th>Description</th>
<th></th>
</tr>
</thead>
<tbody hx-ext="sse" sse-connect="/snacks/listen" sse-swap="newSNACK" hx-swap="beforeend">
<form hx-post="/snacks" hx-swap="none" hx-on::after-request="this.reset()">
<td>
<input class="uk-input" name="Title" type="text">
</td>
<td>
<input class="uk-input" name="Description" type="text">
</td>
<td>
<button type="submit" class="uk-button uk-button-primary">Add SNACK</button>
</td>
</form>
{{ range . }}
{{ template "snackRow" . }}
{{ end }}
</tbody>
</table>
</body>
</html>`
snackRow html.Template = "snackRow"
snackRowTemplate string = `<tr hx-target="this" hx-swap="outerHTML">
<td>{{ .Title }}</td>
<td>{{ .Description }}</td>
<td>
{{- $color := "primary" }}
{{- $disabled := "" }}
{{- if .Eaten }}
{{- $color = "secondary" }}
{{- $disabled = "disabled" }}
{{- end -}}
<button class="uk-button uk-button-{{ $color }}"
hx-put="/snacks/{{ .ID }}"
hx-headers='{"Accept": "text/html"}'
hx-include="this"
{{ $disabled }}>
<input type="hidden" name="Eaten" value="true">
<input type="hidden" name="Title" value="{{ .Title }}">
<input type="hidden" name="Description" value="{{ .Description }}">
<input type="hidden" name="ID" value="{{ .ID }}">
Eat
</button>
<button class="uk-button uk-button-danger" hx-delete="/snacks/{{ .ID }}" hx-swap="swap:1s">
Delete
</button>
</td>
</tr>`
)
type SNACK struct {
babyapi.DefaultResource
Title string
Description string
Eaten *bool
}
func (t *SNACK) HTML(r *http.Request) string {
return snackRow.Render(r, t)
}
type AllSNACKs struct {
babyapi.ResourceList[*SNACK]
}
func (at AllSNACKs) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
func (at AllSNACKs) HTML(r *http.Request) string {
return allSNACKs.Render(r, at.Items)
}
func createAPI() *babyapi.API[*SNACK] {
api := babyapi.NewAPI("SNACKs", "/snacks", func() *SNACK { return &SNACK{} })
api.AddCustomRootRoute(http.MethodGet, "/", http.RedirectHandler("/snacks", http.StatusFound))
// Use AllSNACKs in the GetAll response since it implements HTMLer
api.SetGetAllResponseWrapper(func(snacks []*SNACK) render.Renderer {
return AllSNACKs{ResourceList: babyapi.ResourceList[*SNACK]{snacks}}
})
api.ApplyExtension(extensions.HTMX[*SNACK]{})
// Add SSE handler endpoint which will receive events on the returned channel and write them to the front-end
snackChan := api.AddServerSentEventHandler("/listen")
// Push events onto the SSE channel when new SNACKs are created
api.SetOnCreateOrUpdate(func(r *http.Request, t *SNACK) *babyapi.ErrResponse {
if r.Method != http.MethodPost {
return nil
}
select {
case snackChan <- &babyapi.ServerSentEvent{Event: "newSNACK", Data: t.HTML(r)}:
default:
logger := babyapi.GetLoggerFromContext(r.Context())
logger.Info("no listeners for server-sent event")
}
return nil
})
// Optionally setup redis storage if environment variables are defined
api.ApplyExtension(extensions.KeyValueStorage[*SNACK]{
KVConnectionConfig: extensions.KVConnectionConfig{
RedisHost: os.Getenv("REDIS_HOST"),
RedisPassword: os.Getenv("REDIS_PASS"),
Filename: os.Getenv("STORAGE_FILE"),
Optional: true,
},
})
html.SetMap(map[string]string{
string(allSNACKs): allSNACKsTemplate,
string(snackRow): snackRowTemplate,
})
return api
}
func main() {
api := createAPI()
api.RunCLI()
}