-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
129 lines (108 loc) · 3.08 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
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
package celerity
import (
"bytes"
"io"
"net/http"
"github.com/5Sigma/vox"
)
// Server - Main server instance
type Server struct {
Router *Router
ResponseAdapter ResponseAdapter
Log *vox.Vox
Channels map[string]*Channel
}
// NewServer - Initialize a new server
func NewServer() *Server {
router := NewRouter()
svr := &Server{
ResponseAdapter: &JSONResponseAdapter{},
Router: router,
Log: vox.New(),
Channels: map[string]*Channel{},
}
router.Root.server = svr
return svr
}
// ServeHTTP - Serves the HTTP request. Complies with http.Handler interface
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := RequestContext(r)
c.Server = s
c.Writer = w
c.Log = s.Log
c.SetQueryParamsFromURL(r.URL)
resp := s.Router.Handle(c, r)
for k, vs := range c.Response.Header {
for _, v := range vs {
w.Header().Add(k, v)
}
}
switch true {
case resp.Handled:
return
case resp.IsRaw():
io.Copy(w, bytes.NewReader(resp.Raw()))
default:
buf, err := s.ResponseAdapter.Process(c, resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(resp.StatusCode)
w.Write(buf)
}
}
// Pre - Register prehandle middleware for the root scope.
func (s *Server) Pre(mw MiddlewareHandler) {
s.Router.Root.Pre(mw)
}
// Use - Use a middleware in the root scope.
func (s *Server) Use(mw MiddlewareHandler) {
s.Router.Root.Use(mw)
}
// Start the server.
func (s *Server) Start(host string) error {
return http.ListenAndServe(host, s)
}
// Scope creates a new scope from the root scope
func (s *Server) Scope(path string) *Scope {
ss := s.Router.Root.Scope(path)
ss.server = s
return ss
}
// ServePath serves a path of static files rooted at the path given
func (s *Server) ServePath(path, rootpath string) {
s.Router.Root.ServePath(path, rootpath)
}
// ServeFile serves a static file at a given path
func (s *Server) ServeFile(path, rootpath string) {
s.Router.Root.ServeFile(path, rootpath)
}
// Channel creates a socket channel at the given path
func (s *Server) Channel(name, path string, h ChannelHandler) {
s.Router.Root.Channel(name, path, h)
}
// Route - Set a route on the root scope.
func (s *Server) Route(method, path string, h RouteHandler) {
s.Router.Root.Route(method, path, h)
}
// GET creates a route for a GET method.
func (s *Server) GET(path string, handler RouteHandler) Route {
return s.Router.Root.Route(GET, path, handler)
}
// POST creates a route for a POST method.
func (s *Server) POST(path string, handler RouteHandler) Route {
return s.Router.Root.Route(POST, path, handler)
}
// PUT creates a route for a PUT method.
func (s *Server) PUT(path string, handler RouteHandler) Route {
return s.Router.Root.Route(PUT, path, handler)
}
// PATCH creates a route for a PATCH method.
func (s *Server) PATCH(path string, handler RouteHandler) Route {
return s.Router.Root.Route(PATCH, path, handler)
}
// DELETE creates a route for a DELETE method.
func (s *Server) DELETE(path string, handler RouteHandler) Route {
return s.Router.Root.Route(DELETE, path, handler)
}