-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.go
65 lines (56 loc) · 1.73 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
package fireball
import (
"net/http"
)
// App is the main structure of fireball applications.
// It can be invoked as an http.Handler
type App struct {
// The After function is called after each request has completed
After func(http.ResponseWriter, *http.Request)
// The Before function is called before each request is routed
Before func(http.ResponseWriter, *http.Request)
// The ErrorHandler is called whenever a Handler returns a non-nil error
ErrorHandler func(http.ResponseWriter, *http.Request, error)
// The NotFoundHandler is called whenever the Router returns a nil RouteMatch
NotFoundHandler func(http.ResponseWriter, *http.Request)
// The template parser is passed into the Context
Parser TemplateParser
// The router is used to match a request to a Handler whenever a request is made
Router Router
}
// NewApp returns a new App object with all of the default fields
func NewApp(routes []*Route) *App {
return &App{
After: func(http.ResponseWriter, *http.Request) {},
Before: func(http.ResponseWriter, *http.Request) {},
ErrorHandler: DefaultErrorHandler,
NotFoundHandler: http.NotFound,
Parser: NewGlobParser("views/", "*.html"),
Router: NewBasicRouter(routes),
}
}
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.Before(w, r)
defer a.After(w, r)
match, err := a.Router.Match(r)
if err != nil {
a.ErrorHandler(w, r, err)
return
}
if match == nil {
a.NotFoundHandler(w, r)
return
}
c := &Context{
PathVariables: match.PathVariables,
Parser: a.Parser,
Request: r,
Meta: map[string]interface{}{},
}
response, err := match.Handler(c)
if err != nil {
a.ErrorHandler(w, r, err)
return
}
response.Write(w, r)
}