-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
66 lines (54 loc) · 1.53 KB
/
http.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
package middleware
import (
"fmt"
"net/http"
"time"
"github.com/evorax/middleware/internal"
)
func (ctx *Context) GetParam(key string) string {
value, ok := ctx.Params[key]
if !ok {
return fmt.Sprintf("param '%s' not found", key)
}
return value
}
func (ctx *Context) JSON(status int, obj any, headers ...map[string]string) error {
return internal.WriteJSON(ctx.Writer, status, obj, headers...)
}
func (ctx *Context) HTML(status int, obj string, headers ...map[string]string) error {
return internal.WriteHTML(ctx.Writer, status, obj, headers...)
}
func (ctx *Context) Write(status int, obj []byte, headers ...map[string]string) error {
return internal.WriteTo(ctx.Writer, status, obj, headers...)
}
func (ctx *Context) WriteString(status int, obj string, headers ...map[string]string) error {
return internal.WriteTo(ctx.Writer, status, []byte(obj), headers...)
}
func (ctx *Context) SetHeader(key, value string) {
ctx.Writer.Header().Set(key, value)
}
func (ctx *Context) SetCookie(name, value, path, domain string, date time.Duration, options ...Cookie) {
cookie := &http.Cookie{
Name: name,
Value: value,
Path: path,
Domain: domain,
Expires: time.Now().Add(date),
}
if cookie.MaxAge != 0 {
cookie.MaxAge = options[0].MaxAge
}
if cookie.Secure {
cookie.Secure = options[0].Secure
}
if cookie.Raw != "" {
cookie.Raw = options[0].Raw
}
if len(cookie.Unparsed) > 0 {
cookie.Unparsed = options[0].Unparsed
}
if cookie.HttpOnly {
cookie.HttpOnly = options[0].HttpOnly
}
http.SetCookie(ctx.Writer, cookie)
}