-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
51 lines (43 loc) · 1.29 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
package grrt
// Copyright 2022 GolangToolKits Authors. All rights reserved.
// Use of this source code is governed by the MIT License
// that can be found in the LICENSE file.
import (
"context"
"net/http"
)
// Router Router
type Router interface {
ServeHTTP(w http.ResponseWriter, r *http.Request)
Handle(path string, handler http.Handler) Route
HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) Route
PathPrefix(path string) Route
NewRoute() Route
//CORS methods
EnableCORS()
CORSAllowCredentials()
SetCorsAllowedHeaders(headers string)
SetCorsAllowedOrigins(origins string)
SetCorsAllowedMethods(methods string)
}
// NewRouter NewRouter creates new Router
func NewRouter() Router {
var rtn ReqRouter
rtn.namedRoutes = make(map[string]*[]Route)
rtn.prefixRoutes = make(map[string]Route)
return &rtn
}
// Vars Vars returns the path variables for the current request
func Vars(r *http.Request) map[string]string {
var rtn = make(map[string]string)
if rv := r.Context().Value(varsKey); rv != nil {
rtn = rv.(map[string]string)
}
return rtn
}
// SetURLVars SetURLVars
func SetURLVars(r *http.Request, vars map[string]string) *http.Request {
ctxi := context.WithValue(r.Context(), varsKey, vars)
return r.WithContext(ctxi)
}
// go mod init github.com/GolangToolKits/grrt