-
Notifications
You must be signed in to change notification settings - Fork 8
/
definitions.go
119 lines (100 loc) · 2.87 KB
/
definitions.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
package anyhttp
import (
"io"
"mime/multipart"
"net"
"net/url"
hum "github.com/grokify/simplego/net/httputilmore"
)
type Request interface {
Header(s string) []byte
HeaderString(s string) string
RemoteAddr() net.Addr
RemoteAddress() string
UserAgent() []byte
Method() []byte
ParseForm() error
AllArgs() Args
QueryArgs() Args
PostArgs() Args
MultipartForm() (*multipart.Form, error)
RequestURI() []byte
RequestURIVar(s string) string
PostBody() ([]byte, error)
}
type Args interface {
GetBytes(key string) []byte
GetBytesSlice(key string) [][]byte
GetString(key string) string
GetStringSlice(key string) []string
GetURLValues() url.Values
}
type Response interface {
SetStatusCode(int)
SetContentType(string)
SetCookie(cookie *Cookie)
GetHeader(key string) []byte
SetHeader(key, val string)
SetBodyBytes([]byte) (int, error)
SetBodyStream(bodyStream io.Reader, bodySize int) error
}
func WriteSimpleJson(w Response, status int, message string) {
w.SetStatusCode(status)
w.SetContentType(hum.ContentTypeAppJsonUtf8)
resInfo := hum.ResponseInfo{
StatusCode: status,
Body: message}
w.SetBodyBytes(resInfo.ToJSON())
}
type MapStringString map[string]string
func (m MapStringString) Get(key string) string {
if val, ok := m[key]; ok {
return val
}
return ""
}
func (m MapStringString) GetSlice(key string) []string {
return []string{m.Get(key)}
}
type ArgsMapStringString struct{ Raw MapStringString }
func NewArgsMapStringString(args MapStringString) ArgsMapStringString {
return ArgsMapStringString{Raw: args}
}
func (args ArgsMapStringString) GetBytes(key string) []byte { return []byte(args.Raw.Get(key)) }
func (args ArgsMapStringString) GetBytesSlice(key string) [][]byte {
output := make([][]byte, 1)
output[0] = args.GetBytes(key)
return output
}
func (args ArgsMapStringString) GetString(key string) string { return args.Raw.Get(key) }
func (args ArgsMapStringString) GetStringSlice(key string) []string { return args.Raw.GetSlice(key) }
type ArgsUrlValues struct{ Raw url.Values }
func NewArgsUrlValues(args url.Values) ArgsUrlValues {
return ArgsUrlValues{Raw: args}
}
func (args ArgsUrlValues) GetBytes(key string) []byte { return []byte(args.Raw.Get(key)) }
func (args ArgsUrlValues) GetBytesSlice(key string) [][]byte {
newSlice := [][]byte{}
if slice, ok := args.Raw[key]; ok {
for _, item := range slice {
newSlice = append(newSlice, []byte(item))
}
}
return newSlice
}
func (args ArgsUrlValues) GetString(key string) string { return args.Raw.Get(key) }
func (args ArgsUrlValues) GetStringSlice(key string) []string {
if slice, ok := args.Raw[key]; ok {
return slice
}
return []string{}
}
func (args ArgsUrlValues) GetURLValues() url.Values {
return args.Raw
}
type Addr struct {
Protocol string
Address string
}
func (addr Addr) Network() string { return addr.Protocol }
func (addr Addr) String() string { return addr.Address }