-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgomvc.go
303 lines (251 loc) · 6.85 KB
/
gomvc.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//Copyright
//go mvc web framework
//
package gomvc
import (
"bytes"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/http/pprof"
"path"
"runtime/debug"
"strings"
"time"
//"io/ioutil"
"os"
//"time"
)
// http verbs
const (
HttpVerbsGet = "GET" //http get
HttpVerbsPost = "POST" //http post
HttpVerbsPut = "PUT" //http put
HttpVerbsDelete = "DELETE" //http delete
HttpVerbsHead = "HEAD" //http head
HttpVerbsTrace = "TRACE" //http trace
HttpVerbsConnect = "CONNECT" //http connect
HttpVerbsOptions = "OPTIONS" //http options
)
//content type
const (
ContentTypeJson = "application/json" //json
ContentTypeXml = "application/xml" //xml
)
//gomvc system messages
const (
MsgServerTimeout = "server timeout" //server execute timeout
MsgServerInternalErr = "server internal error" //5xx, server internal error
MsgNotFound = "404 not found" //404
)
//gomvc const var
const (
MvcFilterName = "mvc" //default mvc filter name
MvcDefaultAction = "Index" // default action name
MvcActionName = "action" // default action name
)
//gomvc errors
var (
ErrViewNotFound = errors.New("can not find view") // can not find a view
ErrInvalidFilters = errors.New("invalid http filters") // invalid http filter
ErrInvalidRoute = errors.New("invalid route") //
)
//gomvc system result
var (
ResultVoid = &VoidResult{} //action does have return value
)
//global
var (
Logger *log.Logger //server logger
EnableDebug bool = false //enable debug or not
EnableProfile bool = false //enable http package profile or not
)
// http error
type HttpError struct {
Message string
}
func NewHttpError(format string, args ...interface{}) HttpError {
return HttpError{fmt.Sprintf(format, args...)}
}
func (self HttpError) Error() string {
return self.Message
}
//http server
type HttpServer struct {
Config WebConfig //config of server
Listener net.Listener //listener
Filters []FilterItem //http filters
}
func DefaultServer() *HttpServer {
return NewHttpServer(WebConfig{})
}
//create a http server
func NewHttpServer(config WebConfig) *HttpServer {
s := &HttpServer{Config: config, Filters: []FilterItem{}}
s.appendFilter("staic", "/", "GET", true, newStaticFilter(s))
s.appendFilter(MvcFilterName, "/", "*", true, newMvcFilter(s))
s.appendFilter("render", "/", "*", false, newRenderFilter(s))
return s
}
//append a filter
func (s *HttpServer) appendFilter(name, path, method string, p bool, filter HttpFilter) {
f := FilterItem{Name: name, PathPrefix: path, Method: method, PassOnResult: p, Handler: filter}
s.Filters = append(s.Filters, f)
}
//insert http filter
func (s *HttpServer) AddFiler(index int, f FilterItem) {
s.Filters = append(s.Filters[:index], append([]FilterItem{f}, s.Filters[index:]...)...)
}
//remove filter
func (s *HttpServer) RemoveFiler(index int) {
//todo:
}
//add route
func (s *HttpServer) Route(name string, pattern string, method string, controller interface{}) error {
for _, r := range s.Filters {
if r.Name == MvcFilterName {
if mvc, ok := r.Handler.(*MvcFilter); ok {
return mvc.Route(name, pattern, method, controller)
}
}
}
return ErrInvalidRoute
}
//start server TODO: run server with goroutine
func (s *HttpServer) Start() error {
var err error
if Logger == nil {
Logger = log.New(os.Stdout, "gomvc", log.Ldate|log.Ltime)
}
Logger.Println("gomvc http server start")
err = s.Config.Check()
if err != nil {
Logger.Fatalln("gomvc check config error:", err)
return err
}
Logger.Println("gomvc http server config:")
Logger.Println(s.Config)
if s.Filters == nil || len(s.Filters) == 0 {
Logger.Println("gomvc http server invalid http filters")
return ErrInvalidFilters
}
mux := http.NewServeMux()
if s.Config.EnableProfile {
Logger.Println("handle http profile on /debug/pprof")
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
}
if s.Config.Timeout > 0 {
http.TimeoutHandler(s, time.Duration(s.Config.Timeout)*time.Second, MsgServerTimeout)
} else {
mux.Handle("/", s)
}
l, err := net.Listen("tcp", s.Config.Address)
if err != nil {
Logger.Fatalln("gomvc http server listen error:", err)
return err
}
//TODO: run server with go func(){...}()
s.Listener = l
err = http.Serve(s.Listener, mux)
if err != nil {
Logger.Fatalln("gomvc http server start error:", err)
return err
}
return nil
}
// close server TODO: send signal
func (s *HttpServer) Close() error {
Logger.Println("gomvc http server is closing")
if s.Listener != nil {
s.Listener.Close()
}
Logger.Println("gomvc http server closed")
return nil
}
//build context
func (s *HttpServer) buildContext(w http.ResponseWriter, r *http.Request) *HttpContext {
_ = r.ParseForm()
return &HttpContext{
Resonse: w,
Request: r,
Method: r.Method,
URL: r.URL,
RemoteAddr: r.RemoteAddr,
RequestPath: cleanPath(strings.TrimSpace(r.URL.Path))}
}
func handleError(err *error) {
if x := recover(); x != nil {
var buf bytes.Buffer
fmt.Fprintln(&buf, "gomvc http server panic handle", x)
buf.Write(debug.Stack())
Logger.Fatalln(buf.String())
*err = NewHttpError("internal error :{0}", x)
}
}
//execute filter handle
func (s *HttpServer) exeFilter(ctx *HttpContext, f *FilterItem) (err error) {
if EnableDebug {
Logger.Println("filter begin:", f.Name)
}
defer func() {
handleError(&err)
}()
if f.Match(ctx) {
if EnableDebug {
Logger.Println("execute filter:", f.Name)
}
f.Handler.Execute(ctx)
} else {
if EnableDebug {
Logger.Println("filter pass:", f.Name)
}
}
return nil
}
//handle http request
func (s *HttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := s.buildContext(w, r)
if EnableDebug {
Logger.Println("request", ctx.Method, ctx.URL, ctx.RemoteAddr)
Logger.Printf("context:%+v", ctx)
}
defer func() {
if EnableDebug {
Logger.Println("request end", ctx.Method, ctx.URL, ctx.RemoteAddr)
}
}()
defer func() {
if x := recover(); x != nil {
Logger.Fatalln("gomvc ServeHTTP internal error", x)
s.InternalError(ctx)
}
}()
for _, f := range s.Filters {
e := s.exeFilter(ctx, &f)
if e != nil {
ctx.LastError = e
}
}
if ctx.Result == nil {
Logger.Println("http result is nil", ctx.Method, ctx.URL, ctx.RemoteAddr)
s.InternalError(ctx)
}
}
//server internal error (StatusInternalServerError)
func (s *HttpServer) InternalError(ctx *HttpContext) {
http.Error(ctx.Resonse, MsgServerInternalErr, http.StatusInternalServerError)
}
//map physical path
func (s *HttpServer) MapPath(p string) string {
f := path.Join(s.Config.PublicPath(), p)
info, err := os.Stat(f)
if err != nil || info.IsDir() {
return ""
}
return f
}