-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
115 lines (93 loc) · 3.07 KB
/
router.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
package main
import (
"go-rest-api/api/web"
"io"
"log"
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/render"
)
func NewRouter(api *web.API) *chi.Mux {
// e := echo.New()
// // Debug mode
// e.Debug = true
// // Middleware TODO:auth
// e.Use(middleware.Logger())
// e.Use(middleware.Recover())
// // e.Use(web.WithSessionUser)
// // Health check
// e.GET("/health", healthCheckHandler)
// // Routes
// // TODO: versioning
// e.GET("v1/users", api.GetAllUser)
// e.POST("v1/users", api.CreateUser)
// e.GET("v1/users/:id", api.GetUser)
// e.PATCH("v1/users/:id", api.UpdateUser)
// e.DELETE("v1/users/:id", api.DeleteUser)
// return e
// }
// func healthCheckHandler(ctx echo.Context) error {
// ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
// return ctx.String(http.StatusOK, "{alive:true}")
r := chi.NewRouter()
middleware.DefaultLogger = middleware.RequestLogger(
&middleware.DefaultLogFormatter{
Logger: newLogger(),
},
)
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(web.PanicHandler)
r.Use(render.SetContentType(render.ContentTypeJSON))
// Basic CORS
// for more ideas, see: https://developer.github.com/v3/#cross-origin-resource-sharing
r.Use(cors.Handler(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"https://*", "http://*"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
// healthcheck
r.Get("/", healthCheckHandler)
r.Route("/v1", func(r chi.Router) {
r.Use(web.VersionHeader("1"))
r.Route("/users", func(r chi.Router) {
r.Get("/", api.GetAllUser)
r.Post("/", api.CreateUser)
r.Route("/{id}", func(r chi.Router) {
r.Use(api.RequireUserID)
r.Patch("/", api.UpdateUser)
r.Delete("/", api.DeleteUser)
})
})
})
return r
}
// paginate is a stub, but very possible to implement middleware logic
// to handle the request params for handling a paginated request.
func paginate(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// just a stub.. some ideas are to look at URL query params for something like
// the page number, or the limit, and send a query cursor down the chain
next.ServeHTTP(w, r)
})
}
func newLogger() *log.Logger {
return log.New(os.Stdout, "chi-log: ", log.Lshortfile)
}
// healthCheckHandler ...
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
// simple health check
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
io.WriteString(w, `{"alive": true}`)
}