generated from resonatecoop/user-api-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
58 lines (48 loc) · 1.56 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
package app
// func (app *App) initRouter() {
// opts := []treemux.Option{
// treemux.WithMiddleware(treemuxotel.NewMiddleware()),
// treemux.WithMiddleware(treemuxgzip.NewMiddleware()),
// }
// if app.IsDebug() {
// opts = append(opts, treemux.WithMiddleware(reqlog.NewMiddleware()))
// }
// opts = append(opts, treemux.WithMiddleware(app.errorHandler))
// app.router = treemux.New(opts...)
// app.apiRouter = app.router.NewGroup("/api",
// treemux.WithMiddleware(corsMiddleware),
// )
// }
// func (app *App) errorHandler(next treemux.HandlerFunc) treemux.HandlerFunc {
// return func(w http.ResponseWriter, req treemux.Request) error {
// err := next(w, req)
// if err == nil {
// return nil
// }
// httpErr := httperror.From(err, app.IsDebug())
// if httpErr.Status != 0 {
// w.WriteHeader(httpErr.Status)
// }
// _ = treemux.JSON(w, httpErr)
// return err
// }
// }
// func corsMiddleware(next treemux.HandlerFunc) treemux.HandlerFunc {
// return func(w http.ResponseWriter, req treemux.Request) error {
// origin := req.Header.Get("Origin")
// if origin == "" {
// return next(w, req)
// }
// h := w.Header()
// h.Set("Access-Control-Allow-Origin", origin)
// h.Set("Access-Control-Allow-Credentials", "true")
// // CORS preflight.
// if req.Method == http.MethodOptions {
// h.Set("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,HEAD")
// h.Set("Access-Control-Allow-Headers", "authorization,content-type")
// h.Set("Access-Control-Max-Age", "86400")
// return nil
// }
// return next(w, req)
// }
// }