-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
77 lines (66 loc) · 1.71 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
package main
import (
"fmt"
"github.com/go-chi/chi"
httpSwagger "github.com/swaggo/http-swagger"
"log"
"net/http"
_ "swgo/docs"
)
func Route() error {
router := chi.NewRouter()
router.Use(corsMiddleware.Handler)
router.Get("/info", infoHandler)
router.With(auth).Get("/private", privateHandler)
// SWAGGER DOCS
sw := chi.NewRouter()
sw.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("http://localhost:1323/swagger/doc.json"),
))
go http.ListenAndServe(":1323", sw)
http.Handle("/", router)
server := &http.Server{Addr: fmt.Sprintf(":%d", 8801)}
return server.ListenAndServe()
}
// ApplicationsList godoc
// @Summary Show public info
// @Description
// @Tags applications
// @Produce json
// @Success 200 {string} string "ok"
// @Router /info [get]
func infoHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("info"))
}
// ApplicationsList godoc
// @Summary Show private info
// @Description
// @Tags applications
// @Produce json
// @Success 200 {string} string "ok"
// @Router /private [get]
// @Security ApiKeyAuth
func privateHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("private"))
}
func auth(next http.Handler) http.Handler {
var isValidToken = func(token string) bool {
return len(token) > 0
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("auth check")
token := r.Header.Get("X-JWT")
if !isValidToken(token) {
http.Error(w, "not authorized", http.StatusUnauthorized)
return
}
log.Printf("X-JWT: %s", token)
//a := r.Header.Get("Authorization")
//if a == "" {
// http.Error(w, "not authorized", http.StatusUnauthorized)
// return
//}
//log.Printf("Authorization: %s", a)
next.ServeHTTP(w, r)
})
}