-
Notifications
You must be signed in to change notification settings - Fork 0
/
gee.go
51 lines (42 loc) · 1.24 KB
/
gee.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 gee
import (
"fmt"
"net/http"
)
//HandlerFunc 定义了 gee 使用的请求处理程序
type HandlerFunc func(http.ResponseWriter, *http.Request)
//Engine 实现了 ServeHandler 接口
type Engine struct {
router map[string]HandlerFunc
}
//New Engine 的构造函数
func New() *Engine {
return &Engine{router: make(map[string]HandlerFunc)}
}
//addRoute Engine 添加路由的函数, 私有
func (e *Engine) addRoute(method string, pattern string, handler HandlerFunc) {
key := method + "-" + pattern
e.router[key] = handler
}
//GET 定义添加 GET 请求的方法
func (e *Engine) GET(pattern string, handler HandlerFunc) {
e.addRoute("GET", pattern, handler)
}
//POST 定义添加 POST 请求的方法
func (e *Engine) POST(pattern string, handler HandlerFunc) {
e.addRoute("POST", pattern, handler)
}
//Run 定义启动 http 服务器的方法
func (e *Engine) Run(addr string) (err error) {
return http.ListenAndServe(addr, e)
}
//ServeHTTP 自定义实现的的 ServeHTTP 方法
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
key := r.Method + "-" + r.URL.Path
if hander, ok := e.router[key]; ok {
hander(w, r)
} else {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "404 Not Found : %s\n", r.URL.Path)
}
}