-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
73 lines (59 loc) · 1.58 KB
/
server.go
File metadata and controls
73 lines (59 loc) · 1.58 KB
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
package web
import (
"net"
"net/http"
)
// Ensure HTTPServer implements Server
var _ Server = &HTTPServer{}
// HandleFunc is the type of handler function
type HandleFunc func(ctx *Context)
type param map[string]string
// Server is an abstract type for any kind of server
type Server interface {
http.Handler
Start(addr string) error
AddRoute(method string, path string, handleFunc HandleFunc)
}
// HTTPServer is a server handling HTTP request
type HTTPServer struct {
*router
}
// NewHTTPServer constructs a http server
func NewHTTPServer() *HTTPServer {
return &HTTPServer{
router: newRouter(),
}
}
type HTTPSServer struct {
}
// ServeHTTP serves an HTTP request: parses route and executes handler
func (h *HTTPServer) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
ctx := &Context{
Req: request,
Resp: writer,
}
routeNode, pathParam := h.router.FindRoute(ctx.Req.Method, ctx.Req.URL.Path)
if routeNode == nil || routeNode.handler == nil {
http.NotFound(ctx.Resp, ctx.Req)
return
}
ctx.Param = pathParam
routeNode.handler(ctx)
}
// Start the HTTPServer
func (h *HTTPServer) Start(addr string) error {
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
// here you are allowed to register your own actions after net.Listen and before http.Serve
return http.Serve(l, h)
}
// Get request tool function
func (h *HTTPServer) Get(path string, handler HandleFunc) {
h.AddRoute(http.MethodGet, path, handler)
}
// Post request tool function
func (h *HTTPServer) Post(path string, handler HandleFunc) {
h.AddRoute(http.MethodPost, path, handler)
}