/
wares.go
80 lines (70 loc) · 2.14 KB
/
wares.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
// Copyright 2015 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
/*
Package wares is a collection of bear.HandlerFunc middleware generators for use
with a forest.App instance.
*/
package wares
import (
"fmt"
"github.com/ursiform/bear"
"github.com/ursiform/forest"
)
type Ware func(ctx *bear.Context)
func InstallBodyParser(app *forest.App) {
app.InstallWare("BodyParser", BodyParser(app), forest.WareInstalled)
}
func InstallErrorWares(app *forest.App) {
app.InstallWare("BadRequest",
ErrorsBadRequest(app), forest.WareInstalled)
app.InstallWare("Conflict",
ErrorsConflict(app), forest.WareInstalled)
app.InstallWare("MethodNotAllowed",
ErrorsMethodNotAllowed(app), forest.WareInstalled)
app.InstallWare("NotFound",
ErrorsNotFound(app), forest.WareInstalled)
app.InstallWare("ServerError",
ErrorsServerError(app), forest.WareInstalled)
app.InstallWare("Unauthorized",
ErrorsUnauthorized(app), forest.WareInstalled)
}
func InstallSecurityWares(app *forest.App) {
app.InstallWare("Authenticate",
Authenticate(app), forest.WareInstalled)
app.InstallWare("CSRF",
CSRF(app), forest.WareInstalled)
}
func InstallSessionWares(app *forest.App, manager SessionManager) {
app.InstallWare("SessionDel",
SessionDel(app, manager), forest.WareInstalled)
app.InstallWare("SessionGet",
SessionGet(app, manager), forest.WareInstalled)
app.InstallWare("SessionSet",
SessionSet(app, manager), forest.WareInstalled)
}
func safeErrorMessage(app *forest.App, ctx *bear.Context,
friendly string) string {
if err, ok := ctx.Get(forest.SafeError).(error); ok && err != nil {
return err.Error()
} else if err, ok := ctx.Get(forest.Error).(error); ok && err != nil {
return safeErrorFilter(app, err, friendly).Error()
} else {
return friendly
}
}
func safeErrorFilter(app *forest.App, err error, friendly string) error {
if app.Config.Debug {
return err
} else {
if app.SafeErrorFilter != nil {
if err := app.SafeErrorFilter(err); err != nil {
return err
} else {
return fmt.Errorf(friendly)
}
} else {
return fmt.Errorf(friendly)
}
}
}