-
Notifications
You must be signed in to change notification settings - Fork 43
/
application.go
177 lines (144 loc) · 4.35 KB
/
application.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
package neo
import (
"fmt"
"net/http"
"github.com/ivpusic/golog"
"github.com/ivpusic/neo/ebus"
)
// Representing Neo application instance
type Application struct {
// Event emmiter/receiver
ebus.EBus
router
static *Static
// Application Configuration parameters
Conf *Conf
// Application logger instance
Logger *golog.Logger
}
// Application initialization.
func (a *Application) init(confFile string) {
a.InitEBus()
a.initRouter()
a.initConf(confFile)
// neo logger
lvl, err := parseLogLevel(a.Conf.Neo.Logger.Level)
if err != nil {
log.Warn(err)
} else {
log.Level = lvl
}
// application logger
lvl, err = parseLogLevel(a.Conf.App.Logger.Level)
a.Logger = golog.GetLogger(a.Conf.App.Logger.Name)
if err != nil {
log.Warn(err)
} else {
a.Logger.Level = lvl
}
}
// Handler interface ``ServeHTTP`` implementation.
// Method will accept all incomming HTTP requests, and pass requests to appropriate handlers if they are defined.
func (a *Application) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// log all unhandled panic's
defer func() {
if r := recover(); r != nil {
fmt.Printf("%v", r)
a.Emit("error", r)
log.Panic(r)
}
}()
ctx := makeCtx(req, w)
request := ctx.Req
response := ctx.Res
defer response.flush()
if req.ContentLength > a.Conf.App.MaxBodyBytes {
log.Errorf("Received too large body. Size: %d, URL %s, Method: %s", req.ContentLength, req.URL.Path, req.Method)
response.Status = http.StatusExpectationFailed
response.Json(map[string]string{
"error": "request body too large",
})
return
}
req.Body = http.MaxBytesReader(w, req.Body, a.Conf.App.MaxBodyBytes)
///////////////////////////////////////////////////////////////////
// Static File Serving
///////////////////////////////////////////////////////////////////
if a.static != nil {
// check if file can be served
file, err := a.static.match(req.URL.Path)
if err == nil {
h := func(ctx *Ctx) (int, error) {
response.SkipFlush()
return 200, response.serveFile(file)
}
fn := compose(merge(a.middlewares, []appliable{handler(h)}))
fn(ctx)
return
}
log.Debug("result not found in static")
}
///////////////////////////////////////////////////////////////////
// Route Matching
///////////////////////////////////////////////////////////////////
route, err := a.match(request)
if err != nil {
log.Debugf("route %s not found. Error: %s", req.URL.Path, err.Error())
// dummy route handler
h := func(ctx *Ctx) (int, error) {
return http.StatusNotFound, nil
}
compose(merge(a.middlewares, []appliable{handler(h)}))(ctx)
} else {
ctx.Req.ResolvedRoute = route.path
route.fnChain(ctx)
}
}
// Starting application instance. This will run application on port defined by configuration.
func (a *Application) Start() {
a.flush()
log.Infof("Starting application on address `%s`", a.Conf.App.Addr)
s := &http.Server{
Handler: a,
Addr: a.Conf.App.Addr,
ReadTimeout: a.Conf.App.ReadTimeout,
WriteTimeout: a.Conf.App.WriteTimeout,
MaxHeaderBytes: a.Conf.App.MaxHeaderBytes,
}
err := s.ListenAndServe()
if err != nil {
panic(err.Error())
}
}
// Defining paths for serving static files. For example if we say:
// a.Serve("/some", "./mypath")
// then if we require ``/some/js/file.js``, Neo will look for file at ``./mypath/js/file.js``.
func (a *Application) Serve(url string, path string) {
if a.static == nil {
log.Debug("creating `Static` instance")
a.static = &Static{}
a.static.init()
}
a.static.Serve(url, path)
}
// If you are planning to return templates from Neo route handler, then you have to compile them.
// This method will accept list of paths/files and compile them.
// You can use also paths with wildcards (example: /some/path/*).
func (a *Application) Templates(templates ...string) {
compileTpl(templates...)
}
// Making new region instance. You can create multiple regions.
func (a *Application) Region() *Region {
return a.makeRegion()
}
///////////////////////////////////////////////////////////////////
// Configuration
///////////////////////////////////////////////////////////////////
// Parsing configuration file for this application instance.
// If configuration file is not provided, then application will use default settings.
func (a *Application) initConf(confFile string) {
if a.Conf == nil {
a.Conf = new(Conf)
a.Conf.Parse(confFile)
}
}