/
context.go
128 lines (106 loc) · 2.63 KB
/
context.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
package reverb
import (
"fmt"
"net/http"
"sync"
"time"
"github.com/influxdata/reverb/sess"
"github.com/labstack/echo/engine/standard"
"github.com/labstack/gommon/log"
"github.com/markbates/going/validate"
"github.com/labstack/echo"
elog "github.com/labstack/echo/log"
)
type Context struct {
echo.Context
Data map[string]interface{}
lg elog.Logger
Session *sess.Session
RenderedTemplates []string
err error
lock *sync.Mutex
}
func (c *Context) RawRequest() *http.Request {
return c.Request().(*standard.Request).Request
}
func (c *Context) RawResponse() http.ResponseWriter {
return c.Response().(*standard.Response).ResponseWriter
}
func (c *Context) Get(s string) interface{} {
return c.Data[s]
}
func (c *Context) Set(s string, i interface{}) {
c.Data[s] = i
}
func (c *Context) Logger() elog.Logger {
return c.lg
}
func (c *Context) Err() error {
return c.err
}
func (c *Context) Error(err error) {
c.err = err
c.Logger().Error(err)
}
func (c *Context) HandleError(err error) error {
c.err = err
c.Logger().Error(err)
return err
}
func (c *Context) ContentType() string {
return c.Request().Header().Get("Content-Type")
}
func (c *Context) Layout() string {
return c.Data["_layout"].(string)
}
func (c *Context) SetLayout(s string) {
c.Data["_layout"] = s
if s != "" {
c.Data["_layout"] = fmt.Sprintf("layouts/%s%s", s, c.Extension())
}
}
func (c *Context) SetExtension(s string) {
c.Set("extension", s)
}
func (c *Context) Extension() string {
e, ok := c.Data["extension"]
if ok {
return e.(string)
}
return ".html"
}
// SetValidationErrors will add `validate.Errors` to the context
// using the key "validation_errors".
func (c *Context) SetValidationErrors(verrs *validate.Errors) {
c.Set("validation_errors", verrs)
}
func (c *Context) LogRenderedTemplate(template string, fn func() error) error {
now := time.Now()
defer func() {
stop := time.Now()
end := stop.Sub(now)
c.lock.Lock()
c.RenderedTemplates = append(c.RenderedTemplates, template)
c.lock.Unlock()
lg := c.Get("lg").(*Logger)
lg.AddDurations("Views", end)
lg.Printf(" Rendered %s %s", template, end)
}()
return fn()
}
func NewContext(e echo.Context) *Context {
lg := log.New("")
c := &Context{
Context: e,
Data: map[string]interface{}{},
Session: sess.GetFromCtx(e),
RenderedTemplates: []string{},
lock: &sync.Mutex{},
lg: lg,
}
lg.EnableColor()
// lg.SetFormat("${level}\t${time_rfc3339} ${long_file}:${line} : ${message}\n")
c.SetExtension(".html")
c.SetLayout("application")
return c
}