-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.go
137 lines (119 loc) · 3.3 KB
/
httpserver.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
package httputil
import (
"context"
"net"
"net/http"
"sync/atomic"
"time"
"github.com/gopherd/doge/net/netutil"
)
var pong = []byte{'p', 'o', 'n', 'g'}
func PingHandler(w http.ResponseWriter, r *http.Request) {
w.Write(pong)
}
type Config struct {
Address string `json:"address"`
StaticDir string `json:"static_dir"`
StaticPath string `json:"static_path"`
ConnTimeout time.Duration `json:"conn_timeout"`
ReadHeaderTimeout time.Duration `json:"read_header_timeout"`
ReadTimeout time.Duration `json:"read_timeout"`
WriteTimeout time.Duration `json:"write_timeout"`
MaxConns int `json:"max_conns"`
Headers map[string]string `json:"headers"`
}
func (cfg *Config) autofix() {
if cfg.ConnTimeout <= 0 {
cfg.ConnTimeout = 60 * time.Second
}
if cfg.ReadHeaderTimeout <= 0 {
cfg.ReadHeaderTimeout = 30 * time.Second
}
if cfg.ReadTimeout <= 0 {
cfg.ReadTimeout = 30 * time.Second
}
if cfg.WriteTimeout <= 0 {
cfg.WriteTimeout = 30 * time.Second
}
if cfg.MaxConns <= 0 {
cfg.MaxConns = 1 << 15
}
}
// HTTPServer ...
type HTTPServer struct {
cfg Config
mux http.ServeMux
server *http.Server
numHandling int64
}
func NewHTTPServer(cfg Config) *HTTPServer {
cfg.autofix()
httpd := &HTTPServer{
cfg: cfg,
}
httpd.server = &http.Server{
Addr: httpd.cfg.Address,
Handler: &httpd.mux,
ReadHeaderTimeout: httpd.cfg.ReadHeaderTimeout,
ReadTimeout: httpd.cfg.ReadTimeout,
WriteTimeout: httpd.cfg.WriteTimeout,
}
return httpd
}
func (httpd *HTTPServer) NumHandling() int64 {
return atomic.LoadInt64(&httpd.numHandling)
}
func (httpd *HTTPServer) Addr() string {
return httpd.server.Addr
}
func (httpd *HTTPServer) Listen() (net.Listener, error) {
addr := httpd.server.Addr
if addr == "" {
addr = ":http"
}
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
if httpd.cfg.ConnTimeout > 0 {
l = netutil.NewTCPKeepAliveListener(l.(*net.TCPListener), httpd.cfg.ConnTimeout)
}
if httpd.cfg.MaxConns <= 0 {
return l, nil
}
return LimitListener(l, httpd.cfg.MaxConns), nil
}
func (httpd *HTTPServer) Serve(l net.Listener) error {
return httpd.server.Serve(l)
}
func (httpd *HTTPServer) ListenAndServe() error {
l, err := httpd.Listen()
if err != nil {
return err
}
return httpd.Serve(l)
}
func (httpd *HTTPServer) Shutdown(ctx context.Context) error {
return httpd.server.Shutdown(ctx)
}
func (httpd *HTTPServer) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request), middlewares ...Middleware) {
httpd.Handle(pattern, http.HandlerFunc(handler), middlewares...)
}
func (httpd *HTTPServer) Handle(pattern string, handler http.Handler, middlewares ...Middleware) {
for _, m := range middlewares {
handler = m.Apply(handler)
}
httpd.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&httpd.numHandling, 1)
defer atomic.AddInt64(&httpd.numHandling, -1)
if httpd.cfg.Headers != nil {
for k, v := range httpd.cfg.Headers {
w.Header().Add(k, v)
}
}
handler.ServeHTTP(w, r)
})
}
func (httpd *HTTPServer) JSONResponse(w http.ResponseWriter, r *http.Request, data any, options ...ResponseOptions) error {
return JSONResponse(w, data, options...)
}