MCPcopy
hub / github.com/go-chi/chi / main

Function main

_examples/versions/main.go:22–49  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

20)
21
22func main() {
23 r := chi.NewRouter()
24
25 r.Use(middleware.RequestID)
26 r.Use(middleware.Logger)
27 r.Use(middleware.Recoverer)
28
29 // API version 3.
30 r.Route("/v3", func(r chi.Router) {
31 r.Use(apiVersionCtx("v3"))
32 r.Mount("/articles", articleRouter())
33 })
34
35 // API version 2.
36 r.Route("/v2", func(r chi.Router) {
37 r.Use(apiVersionCtx("v2"))
38 r.Mount("/articles", articleRouter())
39 })
40
41 // API version 1.
42 r.Route("/v1", func(r chi.Router) {
43 r.Use(randomErrorMiddleware) // Simulate random error, ie. version 1 is buggy.
44 r.Use(apiVersionCtx("v1"))
45 r.Mount("/articles", articleRouter())
46 })
47
48 http.ListenAndServe(":3333", r)
49}
50
51func apiVersionCtx(version string) func(next http.Handler) http.Handler {
52 return func(next http.Handler) http.Handler {

Callers

nothing calls this directly

Calls 5

UseMethod · 0.95
RouteMethod · 0.95
MountMethod · 0.95
apiVersionCtxFunction · 0.85
articleRouterFunction · 0.85

Tested by

no test coverage detected