-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
49 lines (40 loc) · 993 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 ctp
import (
"encoding/json"
"log"
"net/http"
"time"
)
// Context is the most important part of ctp. It allows us get http request and response
type Context struct {
Request *http.Request
Writer http.ResponseWriter
}
func NewContext(r *http.Request, w http.ResponseWriter) *Context {
return &Context{
Request: r,
Writer: w,
}
}
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
return ctx.Request.Context().Deadline()
}
func (ctx *Context) Done() <-chan struct{} {
return ctx.Request.Context().Done()
}
func (ctx *Context) Err() error {
return ctx.Request.Context().Err()
}
func (ctx *Context) Value(key interface{}) interface{} {
return ctx.Request.Context().Value(key)
}
func (ctx *Context) Json(status int, obj interface{}) {
ctx.Writer.Header().Set("Content-Type", "application/json")
ctx.Writer.WriteHeader(status)
byt, err := json.Marshal(obj)
if err != nil {
ctx.Writer.WriteHeader(500)
log.Print(err)
}
ctx.Writer.Write(byt)
}