This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
497 lines (421 loc) · 12.2 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package kwiscale
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
)
// handlerManagerRegistry is a map of [name]handlerManager.
var handlerManagerRegistry = make(map[string]*handlerManager)
// handlerRegistry keep the entire handlers - map[name]type.
var handlerRegistry = make(map[string]reflect.Type)
// regexp to find url ordered params from gorilla form.
var urlParamRegexp = regexp.MustCompile(`\{(.+?):`)
// Register takes webhandler and keep type in handlerRegistry.
// It can be called directly (to set handler accessible
// by configuration file), or implicitally by "AddRoute" and "AddNamedRoute()".
func Register(h WebHandler) {
elem := reflect.ValueOf(h).Elem().Type()
name := elem.String()
if _, exists := handlerRegistry[name]; !exists {
handlerRegistry[name] = elem
}
}
type handlerRouteMap struct {
handlername string
route string
}
// App handles router and handlers.
type App struct {
// configuration
Config *Config
// Global context shared to handlers
Context map[string]interface{}
// session store
sessionstore SessionStore
// Template engine instance.
//templateEngine Template
// The router that will be used
router *mux.Router
// List of handler "names" mapped to route (will be create by a factory)
handlers map[*mux.Route]handlerRouteMap
// Handler name for error handler.
errorHandler string
}
// NewApp Create new *App - App constructor.
func NewApp(config *Config) *App {
// fill up config for non-set values
config = initConfig(config)
Log(fmt.Sprintf("%+v\n", config))
// generate app, assign config, router and handlers map
a := &App{
Config: config,
router: mux.NewRouter(),
handlers: make(map[*mux.Route]handlerRouteMap),
Context: make(map[string]interface{}),
}
// set sessstion store
a.sessionstore = sessionEngine[config.SessionEngine]
a.sessionstore.Name(config.SessionName)
a.sessionstore.SetSecret(config.SessionSecret)
a.sessionstore.SetOptions(config.SessionEngineOptions)
a.sessionstore.Init()
if config.StaticDir != "" {
a.SetStatic(config.StaticDir)
}
a.router.StrictSlash(config.StrictSlash)
// keep config
a.Config = config
return a
}
// NewAppFromConfigFile import config file and returns *App.
func NewAppFromConfigFile(filename ...string) *App {
if len(filename) > 1 {
panic(errors.New("You should give only one file in NewAppFromConfigFile"))
}
file := "kwiscale.yml"
if len(filename) > 0 {
file = filename[0]
} else {
if _, err := os.Stat("config.yml"); err == nil {
log.Println("[WARN] - congig.yml is deprecated, please move to kwiscale.yml")
file = "config.yml"
}
}
content, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
cfg := yamlConf{}
yaml.Unmarshal(content, &cfg)
app := NewApp(cfg.parse())
for route, v := range cfg.Routes {
if handler, ok := handlerRegistry[v.Handler]; ok {
h := reflect.New(handler).Interface().(WebHandler)
log.Println(route, h, v.Alias)
app.addRoute(route, h, v.Alias)
} else {
panic("Handler not found: " + v.Handler)
}
}
return app
}
// ListenAndServe calls http.ListenAndServe method
func (app *App) ListenAndServe(port ...string) {
p := app.Config.Port
if len(port) > 0 {
p = port[0]
}
log.Println("Listening", p)
log.Fatal(http.ListenAndServe(p, app))
}
// SetStatic set the route "prefix" to serve files configured in Config.StaticDir
func (app *App) SetStatic(prefix string) {
path, _ := filepath.Abs(prefix)
prefix = filepath.Base(path)
s := &staticHandler{}
app.AddNamedRoute("/"+prefix+"/{file:.*}", s, "statics")
}
// Implement http.Handler ServeHTTP method.
func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// try to recover panic if possible, and display
// an Error page
defer func() {
if err := recover(); err != nil {
app.Error(http.StatusInternalServerError,
w,
errors.New("An unexpected error occured"),
err,
)
}
}()
var handler WebHandler
handlerName, route, match := getBestRoute(app, r)
// if non match
if _, ok := handlerManagerRegistry[handlerName]; !ok {
app.Error(http.StatusNotFound, w, ErrNotFound, r.URL)
return
}
// wait for a built handler from registry
handler = <-handlerManagerRegistry[handlerName].produce()
Log("Handler found ", handler)
//assign some vars
handler.setRoute(route)
handler.setVars(match.Vars, w, r)
handler.setApp(app)
handler.setSessionStore(app.sessionstore)
// Call Init before starting response
if code, err := handler.Init(); err != nil {
Log(err)
// if returned status is <0, let Init() method do the work
if code <= 0 {
Log("Init method returns no error but a status <= 0")
return
}
// Init() stops the request with error and with a status code to use
app.Error(code, w, err)
return
}
// Nothing stops the process calling Init(), so we can
// prepare defered destroy
defer handler.Destroy()
// Websocket case
if h, ok := handler.(WSHandler); ok {
if err := h.upgrade(); err != nil {
log.Println("Error upgrading Websocket protocol", err)
return
}
defer h.OnClose()
h.OnConnect()
switch h.(type) {
case WSServerHandler:
serveWS(h)
case WSJsonHandler:
serveJSON(h)
case WSStringHandler:
serveString(h)
default:
app.Error(http.StatusNotImplemented, w, ErrNotImplemented)
}
return
}
// Standard Request
if h, ok := handler.(HTTPRequestHandler); ok {
// RequestHandler case
w.Header().Add("Connection", "close")
Log("Respond to RequestHandler", r.Method, h)
if h == nil {
app.Error(http.StatusNotFound, w, ErrNotFound, r.Method, h)
return
}
switch r.Method {
case "GET":
h.Get()
case "PUT":
h.Put()
case "POST":
h.Post()
case "DELETE":
h.Delete()
case "HEAD":
h.Post()
case "PATCH":
h.Patch()
case "OPTIONS":
h.Options()
case "TRACE":
h.Trace()
default:
app.Error(http.StatusNotImplemented, w, ErrNotImplemented)
}
return
}
// if the method have parameters, we can try to call it.
if app.callMethodWithParameters(r, handler, route, &match) {
return
}
// we should NEVER go to this, but in case of...
details := "" +
fmt.Sprintf("Registry: %+v\n", handlerManagerRegistry) +
fmt.Sprintf("RequestWriter: %+v\n", w) +
fmt.Sprintf("Reponse: %+v", r) +
fmt.Sprintf("KwiscaleHandler: %+v\n", handler)
Log(details)
app.Error(http.StatusInternalServerError, w, ErrInternalError, details)
}
// Append handler in handlerRegistry and start producing.
// return the name of the handler.
func (app *App) handle(h WebHandler, name string) string {
handlerType := reflect.ValueOf(h).Elem().Type()
handlerName := handlerType.String()
if name == "" {
name = handlerType.String()
}
Log("Register ", name)
Register(h)
if _, ok := handlerManagerRegistry[name]; ok {
// do not create registry manager if it exists
Log("Registry manager for", name, "already exists")
return name
}
// Append a new handler manager in registry
hm := &handlerManager{
handler: handlerName,
closer: make(chan int, 0),
producer: make(chan WebHandler, app.Config.NbHandlerCache),
}
handlerManagerRegistry[name] = hm
// to be able to fetch handler by real name, only if alias is not given
if name != handlerName {
handlerManagerRegistry[handlerName] = hm
}
// start to produce handlers
go handlerManagerRegistry[name].produceHandlers()
// return the handler name
return name
}
// AddRoute appends route mapped to handler. Note that rh parameter should
// implement IRequestHandler (generally a struct composing RequestHandler or WebSocketHandler).
func (app *App) AddRoute(route string, handler WebHandler) {
app.addRoute(route, handler, "")
}
// AddNamedRoute does the same as AddRoute but set the route name instead of
// using the handler name. If the given name already exists or is empty, the method
// panics.
func (app *App) AddNamedRoute(route string, handler WebHandler, name string) {
name = strings.TrimSpace(name)
if len(name) == 0 {
panic(errors.New("The given name is empty"))
}
app.addRoute(route, handler, name)
}
// Add route to the stack.
func (app *App) addRoute(route string, handler WebHandler, routename string) {
var name string
handlerType := reflect.ValueOf(handler).Elem().Type()
if len(routename) == 0 {
name = handlerType.String()
} else {
name = routename
}
// record a route
r := app.router.NewRoute()
r.Path(route)
r.Name(name)
app.handlers[r] = handlerRouteMap{name, route}
app.handle(handler, name)
}
// SoftStop stops each handler manager goroutine (useful for testing).
func (app *App) SoftStop() chan int {
c := make(chan int, 0)
go func() {
for name, closer := range handlerManagerRegistry {
Log("Closing ", name)
closer.closer <- 1
Log("Closed ", name)
}
c <- 1
}()
return c
}
// GetRoute return the *mux.Route that have the given name.
func (app *App) GetRoute(name string) *mux.Route {
for route := range app.handlers {
if route.GetName() == name {
return route
}
}
return nil
}
// GetTemplate returns a new instance of Template.
func (app *App) GetTemplate() Template {
engine := templateEngine[app.Config.TemplateEngine]
//ttype := reflect.TypeOf(engine)
t := reflect.New(engine).Interface().(Template)
t.SetTemplateDir(app.Config.TemplateDir)
t.SetTemplateOptions(app.Config.TemplateEngineOptions)
return t
}
// GetRoutes get all routes for a handler
func (app *App) GetRoutes(name string) []*mux.Route {
routes := []*mux.Route{}
for route := range app.handlers {
if route.GetName() == name {
routes = append(routes, route)
}
}
return routes
}
// DB returns the App.database configured from Config.
/*func (app *App) DB() DB {
if app.Config.DB != "" {
dtype := dbdrivers[app.Config.DB]
database := reflect.New(dtype).Interface().(DB)
database.SetOptions(app.Config.DBOptions)
database.Init()
return database
}
Log("No db selected")
return nil
}*/
// SetErrorHandler set error handler to replace the default ErrorHandler.
func (app *App) SetErrorHandler(h WebHandler) {
app.errorHandler = app.handle(h, "")
}
// Error displays an error page with details if any.
func (app *App) Error(status int, w http.ResponseWriter, err error, details ...interface{}) {
Log(err, details)
var handler WebHandler
if app.errorHandler == "" {
handler = &ErrorHandler{}
} else {
handler = <-handlerManagerRegistry[app.errorHandler].produce()
}
handler.setApp(app)
handler.setVars(nil, w, nil)
handler.(HTTPErrorHandler).setStatus(status)
handler.(HTTPErrorHandler).setError(err)
handler.(HTTPErrorHandler).setDetails(details)
handler.(HTTPRequestHandler).Get()
}
// Try to call method with parameters (if found)
func (app *App) callMethodWithParameters(r *http.Request, handler WebHandler, route *mux.Route, match *mux.RouteMatch) bool {
h := reflect.ValueOf(handler)
method := h.MethodByName(strings.Title(strings.ToLower(r.Method)))
if method.Kind() == reflect.Invalid {
return false
}
// if method is not a parametrized method, return false
if method.Type().NumIn() == 0 {
return false
}
maps := urlParamRegexp.FindAllStringSubmatch(app.handlers[route].route, -1)
// build reflect.Value from match.Vars
args := []reflect.Value{}
for _, v := range maps {
args = append(args, reflect.ValueOf(match.Vars[v[1]]))
}
// convert parameters, for now we can manage string (default), float, int and bool
for i := 0; i < method.Type().NumIn(); i++ {
typ := method.Type().In(i) // function arg type
arg := reflect.ValueOf(args[i]) // argument to map
switch typ.Kind() {
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(args[i].String(), 10)
if err != nil {
panic(err)
}
arg = reflect.ValueOf(v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(args[i].String(), 10, 64)
if err != nil {
panic(err)
}
arg = reflect.ValueOf(v)
case reflect.Bool:
switch strings.ToLower(args[i].String()) {
case "true", "1", "yes", "on":
arg = reflect.ValueOf(true)
case "false", "0", "no", "off":
arg = reflect.ValueOf(false)
default:
panic(fmt.Errorf("Boolean URL '%s' value is not reconized", args[i]))
}
}
// convertion
if typ.Kind() != reflect.String {
args[i] = arg.Convert(typ)
}
}
method.Call(args)
return true
}