-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
66 lines (56 loc) · 1.67 KB
/
routes.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
package main
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/cors"
)
func dateExtractMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
date := chi.URLParam(r, "date")
ctx := context.WithValue(r.Context(), "date", date)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func languageChecker(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lang := strings.ToLower(chi.URLParam(r, "lang"))
switch lang {
case "en", "fr", "de", "es", "pt":
ctx := context.WithValue(r.Context(), "lang", lang)
next.ServeHTTP(w, r.WithContext(ctx))
default:
w.WriteHeader(http.StatusBadRequest)
}
})
}
func useCors(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
ctx := context.WithValue(r.Context(), "cors", true)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func Router() chi.Router {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(cors.Default().Handler)
r.Use(middleware.Timeout(10 * time.Second))
dofusdudeApiMajor := 1
r.With(useCors).With(languageChecker).Route(fmt.Sprintf("/dofus3/v%d", dofusdudeApiMajor), func(r chi.Router) {
r.Route("/meta/{lang}/almanax/bonuses", func(r chi.Router) {
r.Get("/", ListBonuses)
r.Get("/search", SearchBonuses)
})
r.Route("/{lang}/almanax", func(r chi.Router) {
r.Get("/", RetrieveAlmanax)
r.With(languageChecker).Put("/{lang}", UpdateAlmanax)
})
})
return r
}