-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
65 lines (54 loc) · 1.25 KB
/
server.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
package ez
import (
"context"
"fmt"
"net/http"
)
type EZServer struct {
s *http.Server
r []Route
}
func New(s *http.Server) *EZServer {
return &EZServer{
s: s,
r: make([]Route, 0),
}
}
func (ez *EZServer) NotFound(rw http.ResponseWriter, r *http.Request) {
http.NotFound(rw, r)
}
func matchesPattern(path, pattern string) bool {
return path == pattern
}
func (ez *EZServer) HandleFunc(route Route) func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
if contains(route.Method, r.Method) && matchesPattern(r.URL.Path, route.Pattern) {
ctx := context.WithValue(r.Context(), RouteKey, route)
r = r.WithContext(ctx)
route.Handler(rw, r)
} else {
ez.NotFound(rw, r)
}
}
}
func (ez *EZServer) RegisterRoute(route Route) {
ez.r = append(ez.r, route)
http.HandleFunc(route.Pattern, ez.HandleFunc(route))
}
func (ez *EZServer) GetRoutes() []Route {
return ez.r
}
func (ez *EZServer) ListenAndServe() {
fmt.Println("Running server on", ez.s.Addr)
err := ez.s.ListenAndServe()
if err != nil {
fmt.Println(err)
}
fmt.Println("Shutdown") // Todo register shutdown method
}
func (ez *EZServer) GenerateDocs() {
generator := DocsGenerator{
server: ez,
}
generator.GenerateDocs()
}