This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
h2.mux.gorilla.go
142 lines (116 loc) · 3.45 KB
/
h2.mux.gorilla.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package svr
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net"
"net/http"
"strings"
)
// https://github.com/gorilla/mux
func newGorilla() *gorillaImpl {
d := &gorillaImpl{}
d.init()
return d
}
type gorillaImpl struct {
router *mux.Router
}
func (d *gorillaImpl) init() {
d.router = mux.NewRouter()
// r := d.Handler().(*mux.Router)
// // Only matches if domain is "www.example.com".
// r.Host("www.example.com")
// // Matches a dynamic subdomain.
// r.Host("{subdomain:[a-z]+}.example.com")
}
//func (d *gorillaImpl) Shutdown(ctx context.Context) error {
// return d.router.Shutdown(ctx)
//}
func (d *gorillaImpl) Handler() http.Handler {
return d.router
}
func (d *gorillaImpl) App() http.Handler {
return d.router
}
func (d *gorillaImpl) PreServe() (err error) {
return
}
func (d *gorillaImpl) PostServe() (err error) {
return
}
func (d *gorillaImpl) Serve(srv *http.Server, listener net.Listener, certFile, keyFile string) (err error) {
d.walkRoutes()
http.Handle("/", d.router)
if certFile != "" && keyFile != "" {
// NOTE that the h2listener have not been reassigned to the exact tlsListener
return srv.ServeTLS(listener, certFile, keyFile)
}
return srv.Serve(listener)
}
func (d *gorillaImpl) walkRoutes() {
r := d.Handler().(*mux.Router)
err := r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
pathTemplate, err := route.GetPathTemplate()
if err == nil {
fmt.Println("ROUTE:", pathTemplate)
}
pathRegexp, err := route.GetPathRegexp()
if err == nil {
fmt.Println("Path regexp:", pathRegexp)
}
queriesTemplates, err := route.GetQueriesTemplates()
if err == nil {
fmt.Println("Queries templates:", strings.Join(queriesTemplates, ","))
}
queriesRegexps, err := route.GetQueriesRegexp()
if err == nil {
fmt.Println("Queries regexps:", strings.Join(queriesRegexps, ","))
}
methods, err := route.GetMethods()
if err == nil {
fmt.Println("Methods:", strings.Join(methods, ","))
}
fmt.Println()
return nil
})
if err != nil {
fmt.Println(err)
}
}
func (d *gorillaImpl) BuildRoutes() {
r := d.Handler().(*mux.Router)
dir := "./public"
// This will serve files under http://localhost:8000/static/<filename>
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
r.HandleFunc("/", muxHomeHandler)
r.HandleFunc("/products", muxProductsHandler)
r.HandleFunc("/articles", muxArticlesHandler)
r.HandleFunc("/products/{key}", muxProductHandler)
r.HandleFunc("/articles/{category}/", muxArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", muxArticleHandler)
r.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
})
}
func muxHomeHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Gorilla!\n"))
}
func muxProductsHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Gorilla!\n"))
}
func muxArticlesHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Gorilla!\n"))
}
func muxProductHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Gorilla!\n"))
}
func muxArticlesCategoryHandler(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "Category: %v\n", vars["category"])
}
func muxArticleHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Gorilla!\n"))
}