-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbooks.go
253 lines (220 loc) · 6.25 KB
/
books.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"hash/fnv"
"net/http"
"sort"
"sync"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/conditional"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
// Rating is a point-in-time rating for a book.
type Rating struct {
Date time.Time `json:"date"`
Rating float64 `json:"rating"`
}
// Book tracks metadata information about a book and its ratings.
type Book struct {
Title string `json:"title"`
Author string `json:"author,omitempty"`
Published time.Time `json:"published,omitempty"`
Ratings int `json:"ratings,omitempty"`
RatingAverage float64 `json:"rating_average,omitempty"`
RecentRatings []Rating `json:"recent_ratings,omitempty"`
modified time.Time `json:"-"`
}
// Version computes a base64 hash of the book's public fields.
func (b Book) Version() string {
data, _ := json.Marshal(b)
h := fnv.New128()
h.Write(data)
result := h.Sum(nil)
return base64.StdEncoding.EncodeToString(result)
}
// BookSummary provides a link and version for the books list response.
type BookSummary struct {
URL string `json:"url"`
Version string `json:"version"`
Modified time.Time `json:"modified"`
}
// booksMu controls access to the map/slice. This is necessary because maps &
// slices are not goroutine-safe and each incoming request may use a separate
// goroutine to handle it. The slice is used to provide a consistent list and
// deletion order since Go maps are unordered. On initial load from the
// unordered JSON an alphanumeric sort is used.
var booksMu = sync.RWMutex{}
var books map[string]*Book
var booksOrder = []string{}
//go:embed books.json
var booksBytes []byte
func init() {
go func() {
// Reset the books DB every 10 minutes to get a consistent state.
for {
// Load from the stored bytes
var loaded map[string]*Book
if err := json.Unmarshal(booksBytes, &loaded); err != nil {
panic(err)
}
for _, b := range loaded {
// Set the last-modified time for conditional update headers to service
// startup. This will rev on restarts but is good enough for demonstration
// purposes.
b.modified = time.Now()
}
booksMu.Lock()
books = loaded
booksOrder = maps.Keys(books)
sort.Strings(booksOrder)
booksMu.Unlock()
time.Sleep(10 * time.Minute)
}
}()
// Update the Sapiens latest review date every 10 seconds to generate
// frequent simulated server-side updates.
go func() {
for {
time.Sleep(10 * time.Second)
booksMu.Lock()
b := books["sapiens"]
if b != nil {
b.modified = time.Now()
b.RecentRatings = []Rating{
{Date: time.Now(), Rating: 4.6},
}
books["sapiens"] = b
}
booksMu.Unlock()
}
}()
}
type ListResponse struct {
Body []BookSummary
}
func (s *APIServer) RegisterListBooks(api huma.API) {
huma.Register(api, huma.Operation{
OperationID: "list-books",
Method: http.MethodGet,
Path: "/books",
Tags: []string{"Books"},
}, func(ctx context.Context, input *struct{}) (*ListResponse, error) {
booksMu.RLock()
defer booksMu.RUnlock()
// Return a list of summaries with metadata about each book.
l := make([]BookSummary, 0, len(books))
for _, k := range booksOrder {
b := books[k]
l = append(l, BookSummary{
URL: "/books/" + k,
Version: b.Version(),
Modified: b.modified,
})
}
return &ListResponse{Body: l}, nil
})
}
type GetBookResponse struct {
CacheControl string `header:"Cache-Control"`
ETag string `header:"Etag"`
LastModified time.Time `header:"Last-Modified"`
Vary string `header:"Vary"`
Body *Book
}
func (s *APIServer) RegisterGetBook(api huma.API) {
huma.Register(api, huma.Operation{
OperationID: "get-book",
Method: http.MethodGet,
Path: "/books/{book-id}",
Tags: []string{"Books"},
}, func(ctx context.Context, input *struct {
conditional.Params
ID string `path:"book-id"`
}) (*GetBookResponse, error) {
booksMu.RLock()
defer booksMu.RUnlock()
b := books[input.ID]
if b == nil {
return nil, huma.Error404NotFound(input.ID + " not found")
}
if err := input.PreconditionFailed(b.Version(), b.modified); err != nil {
return nil, err
}
resp := &GetBookResponse{
CacheControl: "max-age:0",
ETag: b.Version(),
LastModified: b.modified,
Vary: "Accept, Accept-Encoding, Origin",
Body: b,
}
return resp, nil
})
}
func (s *APIServer) RegisterPutBook(api huma.API) {
huma.Register(api, huma.Operation{
OperationID: "put-book",
Method: http.MethodPut,
Path: "/books/{book-id}",
Tags: []string{"Books"},
}, func(ctx context.Context, input *struct {
conditional.Params
ID string `path:"book-id"`
Body Book
}) (*struct{}, error) {
booksMu.Lock()
defer booksMu.Unlock()
if input.HasConditionalParams() {
existing := books[input.ID]
if existing != nil {
if err := input.PreconditionFailed(existing.Version(), existing.modified); err != nil {
return nil, err
}
}
}
if books[input.ID] == nil {
booksOrder = append(booksOrder, input.ID)
}
input.Body.modified = time.Now()
books[input.ID] = &input.Body
// Limit the total number of books by deleting the oldest first. These will
// get reset periodically by the goroutine in `init()` above.
for len(books) > 20 {
delete(books, booksOrder[0])
booksOrder = booksOrder[1:]
}
return nil, nil
})
}
func (s *APIServer) RegisterDeleteBook(api huma.API) {
huma.Register(api, huma.Operation{
OperationID: "delete-book",
Method: http.MethodDelete,
Path: "/books/{book-id}",
Tags: []string{"Books"},
}, func(ctx context.Context, input *struct {
conditional.Params
ID string `path:"book-id"`
}) (*struct{}, error) {
booksMu.Lock()
defer booksMu.Unlock()
if input.HasConditionalParams() {
existing := books[input.ID]
if existing != nil {
if err := input.PreconditionFailed(existing.Version(), existing.modified); err != nil {
return nil, err
}
}
}
// Remove the book from both the map and the slice.
delete(books, input.ID)
if idx := slices.Index(booksOrder, input.ID); idx > -1 {
booksOrder = slices.Delete(booksOrder, idx, idx+1)
}
return nil, nil
})
}