-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
517 lines (448 loc) · 15.2 KB
/
main.go
File metadata and controls
517 lines (448 loc) · 15.2 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// @title Gorack API
// @version 1.0
// @description A simple API for calculating barbell weight plates.
// @termsOfService http://example.com/terms/
// @contact.name Your Name
// @contact.url http://www.github.com/pachev
// @contact.email your.email@example.com
// @license.name MIT
// @license.url https://opensource.org/licenses/MIT
// @host localhost:8080
// @BasePath /v1/api
// @tag.name Rack
// @tag.description Operations for calculating barbell weight plates
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"reflect"
"strconv"
"sync"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/render"
"github.com/mitchellh/mapstructure"
httpSwagger "github.com/swaggo/http-swagger/v2"
_ "github.com/pachev/gorack/docs"
)
// WeightCache provides in-memory caching for weight calculations
type WeightCache struct {
cache map[string]*ReturnedValueStandard
mu sync.RWMutex
ttl time.Duration
}
// NewWeightCache creates a new cache with the specified TTL
func NewWeightCache(ttl time.Duration) *WeightCache {
return &WeightCache{
cache: make(map[string]*ReturnedValueStandard),
ttl: ttl,
}
}
// Get retrieves a cached result if it exists
func (wc *WeightCache) Get(key string) (*ReturnedValueStandard, bool) {
wc.mu.RLock()
defer wc.mu.RUnlock()
result, found := wc.cache[key]
return result, found
}
// Set stores a calculation result in the cache
func (wc *WeightCache) Set(key string, value *ReturnedValueStandard) {
wc.mu.Lock()
defer wc.mu.Unlock()
wc.cache[key] = value
// Set up automatic expiration if TTL is positive
if wc.ttl > 0 {
go func(k string) {
time.Sleep(wc.ttl)
wc.mu.Lock()
delete(wc.cache, k)
wc.mu.Unlock()
}(key)
}
}
// Clear empties the cache
func (wc *WeightCache) Clear() {
wc.mu.Lock()
defer wc.mu.Unlock()
wc.cache = make(map[string]*ReturnedValueStandard)
}
// Global cache instance
var weightCache *WeightCache
// Routes function that sets up the initial Chi Router
func Routes() *chi.Mux {
router := chi.NewRouter()
corsMiddleware := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
router.Use(corsMiddleware.Handler)
router.Use(
render.SetContentType(render.ContentTypeJSON),
middleware.Logger,
middleware.RedirectSlashes,
middleware.Recoverer,
middleware.RequestID,
)
// Health check endpoints
router.Get("/health", HealthCheck)
router.Get("/status", HealthCheck)
router.Get("/", HealthCheck)
router.Get("/docs/*", httpSwagger.Handler())
return router
}
// WeightAmounts is a translation for keynames in amounts (weight per single plate)
var WeightAmounts = map[string]float32{
"Hundos": 100,
"FortyFives": 45,
"ThirtyFives": 35,
"TwentyFives": 25,
"Tens": 10,
"Fives": 5,
"TwoDotFives": 2.5,
"OneDotTwoFives": 1.25,
}
func main() {
// Initialize cache with TTL from environment or 1 hour if not set
cacheTTL := getEnvDuration("CACHE_TTL", 1*time.Hour)
weightCache = NewWeightCache(cacheTTL)
router := Routes()
router.Route("/v1/api", func(r chi.Router) {
r.Post("/rack", RackEmPost)
r.Get("/rack", RackEmGet)
})
walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
log.Printf("%s %s\n", method, route)
return nil
}
if err := chi.Walk(router, walkFunc); err != nil {
log.Panicf("Logging err: %s\n", err.Error())
}
port := getEnv("API_PORT", "8080")
log.Printf("Starting server on :%s\n", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}
// RackEmPost godoc
// @Summary Calculate plates with custom plate availability
// @Description Returns an optimal plate configuration for a given target weight with custom available plates
// @Tags Rack
// @Accept json
// @Produce json
// @Param request body RackInputStandard true "Desired weight and available plates"
// @Success 200 {object} ReturnedValueStandard
// @Failure 400 {object} ErrResponse
// @Failure 500 {object} ErrResponse
// @Router /rack [post]
func RackEmPost(w http.ResponseWriter, r *http.Request) {
input := &RackInputStandard{}
if err := render.Bind(r, input); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
return
}
// Generate cache key for this specific input
cacheKey := generateCacheKey(input)
// Check cache first
if cachedResult, found := weightCache.Get(cacheKey); found {
render.JSON(w, r, cachedResult)
return
}
// Cache miss, calculate and store
results, err := CalculateWeight(input)
if err != nil {
log.Printf("Error calculating weight for POST: %v\nInput: %+v\n", err, input)
render.Render(w, r, ErrInternal())
return
}
// Store in cache
weightCache.Set(cacheKey, results)
render.JSON(w, r, results)
}
// RackEmGet godoc
// @Summary Calculate plates using default plate availability
// @Description Returns an optimal plate configuration for a given target weight
// @Tags Rack
// @Accept json
// @Produce json
// @Param weight query int true "Desired weight in pounds"
// @Success 200 {object} ReturnedValueStandard
// @Failure 400 {object} ErrResponse
// @Failure 500 {object} ErrResponse
// @Router /rack [get]
func RackEmGet(w http.ResponseWriter, r *http.Request) {
inputWithDefaults := AssumeDefaults()
weightStr := r.URL.Query().Get("weight")
if weightStr == "" {
render.Render(w, r, ErrInvalidRequest(errors.New("query parameter 'weight' is required")))
return
}
weight, err := strconv.ParseInt(weightStr, 10, 64)
if err != nil {
render.Render(w, r, ErrInvalidRequest(errors.New("invalid 'weight' parameter: must be an integer")))
return
}
if int(weight) <= 0 {
render.Render(w, r, ErrInvalidRequest(errors.New("'weight' must be a positive integer")))
return
}
inputWithDefaults.DesiredWeight = int(weight)
// Validate DesiredWeight against BarWeight for GET requests
if inputWithDefaults.DesiredWeight <= inputWithDefaults.BarWeight {
render.Render(w, r, ErrInvalidRequest(errors.New("desired weight must be greater than bar weight")))
return
}
// Check cache for GET request with standard plates
cacheKey := fmt.Sprintf("get:%d", inputWithDefaults.DesiredWeight)
if cachedResult, found := weightCache.Get(cacheKey); found {
render.JSON(w, r, cachedResult)
return
}
results, calcErr := CalculateWeight(&inputWithDefaults)
if calcErr != nil {
log.Printf("Error calculating weight for GET: %v\nInput: %+v\n", calcErr, inputWithDefaults)
render.Render(w, r, ErrInternal())
return
}
// Store in cache
weightCache.Set(cacheKey, results)
render.JSON(w, r, results)
}
// generateCacheKey creates a unique key for caching based on input parameters
func generateCacheKey(input *RackInputStandard) string {
return fmt.Sprintf("post:bar=%d:desired=%d:h=%d:45=%d:35=%d:25=%d:10=%d:5=%d:2.5=%d:1.25=%d",
input.BarWeight,
input.DesiredWeight,
input.Hundos,
input.FortyFives,
input.ThirtyFives,
input.TwentyFives,
input.Tens,
input.Fives,
input.TwoDotFives,
input.OneDotTwoFives,
)
}
// CalculateWeight is the core logic for calculating plates needed.
// Input represents available plates. Output represents plates to use.
func CalculateWeight(inputAvailablePlates *RackInputStandard) (*ReturnedValueStandard, error) {
platesToUse := map[string]int{} // Stores count of each plate type (pair) to load
currentBarWeight := inputAvailablePlates.BarWeight
if currentBarWeight < 0 { // Ensure bar weight is not negative
currentBarWeight = 0
}
leftOver := inputAvailablePlates.DesiredWeight - currentBarWeight
achievedWeight := currentBarWeight
// Create a mutable copy of the input available plates for deduction during calculation
currentAvailablePlates := *inputAvailablePlates
// Define order of plates to try, from heaviest to lightest.
orderedPlateNames := []string{
"Hundos", "FortyFives", "ThirtyFives", "TwentyFives",
"Tens", "Fives", "TwoDotFives", "OneDotTwoFives",
}
for leftOver > 0 {
foundPlateInIteration := false
for _, plateName := range orderedPlateNames {
var plateAvailableCount int
val := reflect.ValueOf(¤tAvailablePlates).Elem()
fieldVal := val.FieldByName(plateName)
if fieldVal.IsValid() {
plateAvailableCount = int(fieldVal.Int())
} else {
// This should not happen if orderedPlateNames matches RackInputStandard fields
return nil, errors.New("internal error: plate name mismatch: " + plateName)
}
if plateAvailableCount == 0 {
continue
}
plateWeightPerSingle, ok := WeightAmounts[plateName]
if !ok {
return nil, errors.New("internal error: weight definition missing for " + plateName)
}
weightOfPair := int(plateWeightPerSingle * 2)
if weightOfPair > 0 && weightOfPair <= leftOver {
leftOver -= weightOfPair
platesToUse[plateName]++
achievedWeight += weightOfPair
currentAvailablePlates.DecreaseWeight(plateName)
foundPlateInIteration = true
break // Greedily take the heaviest possible, then restart outer loop for next heaviest
}
}
if !foundPlateInIteration {
break // No suitable plate could be added in this pass
}
}
// Prepare the output struct containing plates to use
outputPlates := RackInputStandard{
BarWeight: currentBarWeight,
DesiredWeight: inputAvailablePlates.DesiredWeight,
}
// Populate outputPlates with the counts from platesToUse map
decoderConfig := &mapstructure.DecoderConfig{
Result: &outputPlates,
TagName: "json",
Squash: true,
}
decoder, err := mapstructure.NewDecoder(decoderConfig)
if err != nil {
log.Printf("Error creating mapstructure decoder: %v", err)
return nil, errors.New("internal error creating decoder")
}
if err := decoder.Decode(platesToUse); err != nil {
log.Printf("Error decoding plates map to struct: %v", err)
return nil, errors.New("internal error decoding result")
}
return &ReturnedValueStandard{
RackInputStandard: &outputPlates,
AchievedWeight: achievedWeight,
Message: "You got this!",
}, nil
}
/* Models */
// RackInputStandard defines the structure for API input (available plates)
// and also for the plates to be used in the output.
// Plate counts are number of PAIRS.
type RackInputStandard struct {
BarWeight int `json:"barWeight,omitempty"`
Hundos int `json:"hundreds,omitempty"` // JSON tag "hundreds" for API compatibility
FortyFives int `json:"fortyFives,omitempty"`
ThirtyFives int `json:"thirtyFives,omitempty"`
TwentyFives int `json:"twentyFives,omitempty"`
Tens int `json:"tens,omitempty"`
Fives int `json:"fives,omitempty"`
TwoDotFives int `json:"twoDotFives,omitempty"`
OneDotTwoFives int `json:"oneDotTwoFives,omitempty"`
DesiredWeight int `json:"desiredWeight"` // Required in input
}
// Bind is a method on RackInputStandard to process and validate the request payload.
func (ris *RackInputStandard) Bind(r *http.Request) error {
if ris.BarWeight == 0 { // If not provided, default to standard Olympic bar
ris.BarWeight = AssumeDefaults().BarWeight
}
if ris.BarWeight < 0 {
return errors.New("bar weight cannot be negative")
}
if ris.DesiredWeight == 0 {
return errors.New("a valid desired weight must be provided")
}
if ris.DesiredWeight <= ris.BarWeight {
return errors.New("desired weight must be greater than bar weight")
}
// Plate counts (Hundos, FortyFives, etc.) default to 0 if not in payload,
// meaning "0 pairs available" for POST requests.
return nil
}
// DecreaseWeight reduces the count of a specific plate type.
// This is called on the *copy* of available plates during calculation.
func (ris *RackInputStandard) DecreaseWeight(plateName string) {
switch plateName {
case "Hundos":
ris.Hundos--
case "FortyFives":
ris.FortyFives--
case "ThirtyFives":
ris.ThirtyFives--
case "TwentyFives":
ris.TwentyFives--
case "Tens":
ris.Tens--
case "Fives":
ris.Fives--
case "TwoDotFives":
ris.TwoDotFives--
case "OneDotTwoFives":
ris.OneDotTwoFives--
}
}
// ReturnedValueStandard is the structure of the JSON response.
type ReturnedValueStandard struct {
*RackInputStandard // Embeds the plates *to use* for the lift
AchievedWeight int `json:"achievedWeight"`
Message string `json:"message,omitempty"`
}
// HealthCheck godoc
// @Summary Health check endpoint
// @Description Returns status of the API server
// @Tags Health
// @Produce json
// @Success 200 {object} map[string]string
// @Router /health [get]
// @Router /status [get]
// @Router /v1/api/health [get]
func HealthCheck(w http.ResponseWriter, r *http.Request) {
status := map[string]string{
"status": "ok",
"service": "gorack-api",
"version": "1.0",
}
render.JSON(w, r, status)
}
/* Util Functions */
// getEnv retrieves an environment variable or returns a fallback value.
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
// getEnvDuration retrieves a time.Duration environment variable or returns a fallback value.
func getEnvDuration(key string, fallback time.Duration) time.Duration {
if value, ok := os.LookupEnv(key); ok {
if duration, err := time.ParseDuration(value); err == nil {
return duration
}
}
return fallback
}
// ErrResponse is a generic renderer for API error responses.
type ErrResponse struct {
Err error `json:"-"` // Low-level runtime error (not exposed to client)
HTTPStatusCode int `json:"-"` // HTTP response status code
StatusText string `json:"status"` // User-level status message
AppCode int64 `json:"code,omitempty"` // Application-specific error code
ErrorText string `json:"error,omitempty"` // Application-level error message for debugging
}
// Render sets the HTTP status code for the error response.
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
render.Status(r, e.HTTPStatusCode)
return nil
}
// ErrInvalidRequest creates a standardized "400 Bad Request" response.
func ErrInvalidRequest(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: http.StatusBadRequest,
StatusText: "Invalid request.",
ErrorText: err.Error(),
}
}
// ErrInternal creates a standardized "500 Internal Server Error" response.
func ErrInternal() render.Renderer {
return &ErrResponse{
Err: nil, // Specific error is logged server-side
HTTPStatusCode: http.StatusInternalServerError,
StatusText: "Internal Server Error.",
ErrorText: "An unexpected error occurred. Please try again later.",
}
}
// AssumeDefaults provides a default set of available plates and bar weight.
// Used for GET requests where the user doesn't specify their available equipment.
func AssumeDefaults() RackInputStandard {
return RackInputStandard{
BarWeight: 45, // Standard Olympic bar weight in lbs
Hundos: 10,
FortyFives: 10,
ThirtyFives: 10,
TwentyFives: 10,
Tens: 10,
Fives: 10,
TwoDotFives: 10,
OneDotTwoFives: 10,
}
}