-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
163 lines (129 loc) · 2.95 KB
/
server.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package http
import (
"encoding/json"
"io"
"net"
"net/http"
"net/url"
"github.com/o8x/jk/v2/response"
"github.com/o8x/jk/v2/x"
)
type Handler func(Request) *response.Response
type Request struct {
*http.Request
Writer http.ResponseWriter
Query url.Values
}
func (r *Request) Unmarshal(v any) error {
all, err := r.ReadBody()
if err != nil {
return err
}
return json.Unmarshal(all, v)
}
func (r *Request) ReadBody() ([]byte, error) {
return io.ReadAll(r.Request.Body)
}
func (r *Request) ReadBodyReckless() []byte {
all, _ := io.ReadAll(r.Request.Body)
return all
}
func (r *Request) RemoteHost() string {
host, _, _ := net.SplitHostPort(r.RemoteAddr)
return host
}
func (r *Request) RemoteHostIn(list ...string) bool {
host := r.RemoteHost()
for _, it := range list {
if it == host {
return true
}
}
return false
}
func (r *Request) RemotePort() int {
_, port, _ := net.SplitHostPort(r.RemoteAddr)
return x.ParseInt(port, 0)
}
type Mux struct {
mux *http.ServeMux
}
func NewMux() *Mux {
return &Mux{
mux: http.NewServeMux(),
}
}
func (m *Mux) ListenAndServe(listen string) error {
return ListenAndServe(listen, m)
}
func (m *Mux) Post(name string, fn Handler) {
m.RegisterRoute(http.MethodPost, name, fn)
}
func (m *Mux) Get(name string, fn Handler) {
m.RegisterRoute(http.MethodGet, name, fn)
}
func (m *Mux) Put(name string, fn Handler) {
m.RegisterRoute(http.MethodPut, name, fn)
}
func (m *Mux) Trace(name string) {
m.mux.HandleFunc(name, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodTrace {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
all, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
_, _ = w.Write(all)
})
}
func (m *Mux) Delete(name string, fn Handler) {
m.RegisterRoute(http.MethodDelete, name, fn)
}
func (m *Mux) Any(name string, fn Handler) {
m.RegisterRoute("any", name, fn)
}
func (m *Mux) RegisterRoute(method, name string, fn Handler) {
m.mux.HandleFunc(name, func(w http.ResponseWriter, r *http.Request) {
if method != "any" && r.Method != method {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
resp := fn(Request{
Request: r,
Writer: w,
Query: r.URL.Query(),
})
if resp == nil {
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = w.Write(resp.Dump())
})
}
func ListenAndServe(listen string, mux *Mux) error {
srv := &http.Server{
Addr: listen,
Handler: mux.mux,
}
return srv.ListenAndServe()
}
func PostServer(listen, path string, fn Handler) error {
mux := NewMux()
mux.Post(path, fn)
return ListenAndServe(listen, mux)
}
func GetServer(listen, path string, fn Handler) error {
mux := NewMux()
mux.Get(path, fn)
return ListenAndServe(listen, mux)
}
func AnyServer(listen, path string, fn Handler) error {
mux := NewMux()
mux.Any(path, fn)
return ListenAndServe(listen, mux)
}