-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
49 lines (38 loc) · 811 Bytes
/
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
package user
import (
"net"
"net/http"
"github.com/soulski/dmp-cli"
"github.com/unrolled/render"
)
type Context struct {
Render *render.Render
DB *DB
IP *net.IPAddr
Namespace string
}
func NewContext(namespace string, db *DB) *Context {
r := render.New()
return &Context{
Render: r,
DB: db,
Namespace: namespace,
}
}
func (c *Context) RenderError(w http.ResponseWriter, msg string, err error) {
result, err := dmpc.CreateErrorResult(&dmpc.Error{
Message: msg,
Cause: err.Error(),
})
if err != nil {
panic(err)
}
c.Render.Data(w, http.StatusOK, result)
}
func (c *Context) RenderJSON(w http.ResponseWriter, msg interface{}) {
result, err := dmpc.CreateMsgResult(msg)
if err != nil {
panic(err)
}
c.Render.Data(w, http.StatusOK, result)
}