-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.go
114 lines (104 loc) · 3.42 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
package server
import (
"log"
"net/http"
"os"
"strings"
"firebase.google.com/go/auth"
"github.com/PurplePalette/sonolus-uploader-core/potato"
"github.com/PurplePalette/sonolus-uploader-core/utils/request"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"golang.org/x/net/context"
)
// injectUserToContext injects firebase user info to context
func injectUserToContext(auth *auth.Client, route potato.Route, sonolusVersion string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
authorized := false
if token != "" {
bearerToken := strings.TrimPrefix(token, "Bearer ")
firebaseToken, err := auth.VerifyIDTokenAndCheckRevoked(context.Background(), bearerToken)
if err == nil {
ctx := context.WithValue(r.Context(), request.CtxUserID, firebaseToken.UID)
r = r.WithContext(ctx)
authorized = true
}
}
if route.Method != "GET" && !authorized {
w.WriteHeader(http.StatusUnauthorized)
} else {
w.Header().Set("Sonolus-Version", sonolusVersion)
route.HandlerFunc.ServeHTTP(w, r)
}
}
}
// injectTestUserToContext injects firebase user id to context (for test purpose)
func injectTestUserToContext(uid string, next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), request.CtxUserID, uid)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
}
}
// indexHandler handles index page request.
// It returns redirect if INDEX_CONTENT was url.
// Otherwise, it return text directly.
// This endpoint won't support to specify html file, to prevent directory traversal.
func indexHandler(indexContent string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(indexContent, "http") {
http.Redirect(w, r, indexContent, http.StatusMovedPermanently)
} else {
w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte(indexContent))
}
}
}
// NewRouterWithInject creates a new router with inject user middleware
func NewRouterWithInject(auth *auth.Client, routers ...potato.Router) *mux.Router {
err := godotenv.Load(".env")
if err != nil {
log.Print("Failed to load .env, using os environment")
}
indexContent := os.Getenv("INDEX_CONTENT")
sonolusVersion := os.Getenv("SONOLUS_VERSION")
router := mux.NewRouter().StrictSlash(true)
for _, api := range routers {
for _, route := range api.Routes() {
var handler http.Handler
handler = injectUserToContext(auth, route, sonolusVersion)
handler = potato.Logger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
}
router.HandleFunc("/", indexHandler(indexContent))
return router
}
// NewRouterWithTestInject creates a new router with inject testUser middleware
func NewRouterWithTestInject(auth *auth.Client, routers ...potato.Router) *mux.Router {
err := godotenv.Load(".env")
if err != nil {
log.Print("Failed to load .env, using os environment")
}
uid := os.Getenv("TEST_UID")
router := mux.NewRouter().StrictSlash(true)
for _, api := range routers {
for _, route := range api.Routes() {
var handler http.Handler
handler = injectTestUserToContext(uid, route.HandlerFunc)
handler = potato.Logger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
}
return router
}