-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.go
146 lines (123 loc) · 3.55 KB
/
app.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package trygo
import (
"net"
"net/http"
"path"
"reflect"
"strings"
)
type App struct {
Handlers *ControllerRegister
Config *config
StaticDirs map[string]string
TemplateRegister *TemplateRegister
Logger LoggerInterface
Statinfo *statinfo
//filter net.Listener
FilterListener func(app *App, l net.Listener) net.Listener
//filter http.Handler
FilterHandler func(app *App, h http.Handler) http.Handler
prepared bool
}
func NewApp() *App {
app := &App{
Handlers: NewControllerRegister(nil),
TemplateRegister: NewTemplateRegister(nil),
Config: newConfig(),
StaticDirs: make(map[string]string),
Logger: Logger,
}
app.FilterListener = func(app *App, l net.Listener) net.Listener { return l }
app.FilterHandler = func(app *App, h http.Handler) http.Handler { return h }
app.Statinfo = newStatinfo(app)
app.Handlers.app = app
app.TemplateRegister.app = app
return app
}
//method - http method, GET,POST,PUT,HEAD,DELETE,PATCH,OPTIONS,*
//pattern - URL path or regexp pattern
//name - method on the container
//tags - function parameter tag info, see struct tag
func (app *App) Register(method string, pattern string, c ControllerInterface, name string, tags ...string) iRouter {
funcname, params := parseMethod(name)
return app.Handlers.Add(method, pattern, c, funcname, params, tags)
}
func (app *App) RegisterHandler(pattern string, h http.Handler) iRouter {
return app.Handlers.AddHandler(pattern, h)
}
func (app *App) RegisterFunc(methods, pattern string, f HandlerFunc) iRouter {
return app.Handlers.AddMethod(methods, pattern, f)
}
func (app *App) RegisterRESTful(pattern string, c ControllerInterface) iRouter {
//app.Register("*", pattern, c, "")
if !isPattern(pattern) {
pattern = path.Join(pattern, "(?P<id>[^/]+)$")
}
return app.Register("*", pattern, c, "")
}
func (app *App) Get(pattern string, f HandlerFunc) iRouter {
return app.Handlers.Get(pattern, f)
}
func (app *App) Post(pattern string, f HandlerFunc) iRouter {
return app.Handlers.Post(pattern, f)
}
func (app *App) Put(pattern string, f HandlerFunc) iRouter {
return app.Handlers.Put(pattern, f)
}
func (app *App) Delete(pattern string, f HandlerFunc) iRouter {
return app.Handlers.Delete(pattern, f)
}
func (app *App) Head(pattern string, f HandlerFunc) iRouter {
return app.Handlers.Head(pattern, f)
}
func (app *App) Patch(pattern string, f HandlerFunc) iRouter {
return app.Handlers.Patch(pattern, f)
}
func (app *App) Options(pattern string, f HandlerFunc) iRouter {
return app.Handlers.Options(pattern, f)
}
func (app *App) Any(pattern string, f HandlerFunc) iRouter {
return app.Handlers.Any(pattern, f)
}
func (app *App) SetStaticPath(url string, path string) *App {
if !strings.HasPrefix(url, "/") {
url = "/" + url
}
if url != "/" {
url = strings.TrimRight(url, "/")
}
app.StaticDirs[url] = path
return app
}
func (app *App) SetViewsPath(path string) *App {
app.Config.TemplatePath = path
return app
}
func (app *App) buildTemplate(files ...string) {
if app.Config.TemplatePath != "" {
err := app.TemplateRegister.buildTemplate(app.Config.TemplatePath, files...)
if err != nil {
app.Logger.Error("%v", err)
}
}
}
func (app *App) Prepare() {
if app.prepared {
return
}
app.prepared = true
app.buildTemplate()
}
func (app *App) Run(server ...Server) {
var err error
var s Server
if len(server) > 0 {
s = server[0]
} else {
s = &HttpServer{}
}
app.Prepare()
if err = s.ListenAndServe(app); err != nil {
app.Logger.Critical("%v.ListenAndServe: %v", reflect.TypeOf(s), err)
}
}