-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
91 lines (80 loc) · 1.62 KB
/
web.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
package web
import (
"fmt"
"github.com/astaxie/beego/logs"
"net/http"
"time"
)
var (
Addr string //地址
Routers []*router
NotAllow handleFunc
NotFound handleFunc
StaticService handleFunc
Logs *logs.BeeLogger
Debug bool
TemplatePath string
StaticPath string //绝对路径
)
type App struct {
handle *handle
addr string
debug bool
}
//添加静态文件目录
func Static(staticPath, staticfilePath string) {
StaticPath = staticfilePath
r := &router{
uri: staticPath,
isFile: true,
}
Routers = append(Routers, r)
}
func Init() *App {
defaultHandler := &handle{
Routers: Routers,
notAllow: NotAllow,
notFound: NotFound,
staticService: StaticService,
}
app := &App{
handle: defaultHandler,
addr: Addr,
debug: Debug,
}
return app
}
//默认404
func defaultNotFound(c *Controller) {
fmt.Fprintf(c.Response, "404")
return
}
//默认无此方法
func defaultNotAllow(c *Controller) {
fmt.Fprintf(c.Response, "mothod notAllow")
return
}
//默认静态处理
func defaultStatic(c *Controller) {
handler := http.FileServer(http.Dir(StaticPath))
handler.ServeHTTP(c.Response, c.Request)
return
}
func init() {
//先将404 等赋给默认方法
NotAllow = defaultNotAllow
NotFound = defaultNotFound
StaticService = defaultStatic
}
// run server in addr
func (a *App) Run() error {
server := &http.Server{
Addr: a.addr,
Handler: a.handle,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
Logs.Info("Run Server %s", a.addr)
return server.ListenAndServe()
}