-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctx.go
113 lines (92 loc) · 2.38 KB
/
ctx.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
package engine
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
"simpaix.net/simpa/v2/engine/parsers/bodyparser"
"simpaix.net/simpa/v2/engine/sessions"
)
type H map[string]any
/*
request and response context
*/
type Ctx struct {
Error error // represents an error
Req *http.Request // http request
Res http.ResponseWriter // http response
Params httprouter.Params // http params
BodyParser bodyparser.BodyParserI // body parser
engine *Engine // underlying app engine
}
// Sends string with custom status code
func (c *Ctx) String(status int, data string) error {
c.Res.WriteHeader(status)
if _, err := c.Res.Write([]byte(data)); err != nil {
return err
}
return nil
}
// Sends JSON with 'application/json' content type.
// 'data' is a pointer to the struct, and it is a JSON unmarshalled object
// this function marshalls the JSON and sends it to the client
func (c *Ctx) JSON(status int, data interface{}) error {
c.Res.WriteHeader(status)
c.Res.Header().Set("content-type", "application/json")
b, err := json.Marshal(data)
if err != nil {
return err
}
if _, err := c.Res.Write(b); err != nil {
return err
}
return nil
}
/*
Renders given HTML template file with the std go templating engine
*/
func (c *Ctx) RenderHTML(name string, data H) error {
c.Res.Header().Set("content-type", "text/html")
return c.engine.template.ExecuteTemplate(c.Res, name, data)
}
/*
Sets secure cookie
*/
func (c *Ctx) SetCookie(cookie *http.Cookie) error {
m, err := json.Marshal(cookie)
if err != nil {
return err
}
enc, err := c.engine.SecureCookie.Encrypt(string(m))
if err != nil {
return err
}
cookie.Value = enc
http.SetCookie(c.Res, cookie)
return nil
}
/*
Decodes encrypted cookie named 'name' to dest
*/
func (c *Ctx) DecodeCookie(name string, dest *http.Cookie) error {
cookie, err := c.Req.Cookie(name)
if err != nil {
return err
}
decr, err := c.engine.SecureCookie.Decrypt(cookie.Value)
if err != nil {
return err
}
if err := json.Unmarshal([]byte(decr), dest); err != nil {
return err
}
return nil
}
func (c *Ctx) Session(config *sessions.Config) (*sessions.Session, error) {
sess := new(sessions.Session)
{
sess.Values = map[string]interface{}{}
}
sess.SetStore(c.engine.Storage)
sess.SetCrypter(c.engine.SecureCookie)
return sess.New(c.Req, config)
}