-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
125 lines (110 loc) · 3.24 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
package rio
import (
"log/slog"
"net/http"
"slices"
"time"
)
// ------------------------------------------------------------------
//
//
// Type: Server
//
//
// ------------------------------------------------------------------
// Server is a wrapper around the standard http.ServeMux.
//
// It can register routes for Handlers and HandlerFuncs.
// It can also register middleware for the entire ServeMux.
type Server struct {
mux *http.ServeMux
middleware []func(http.Handler) http.Handler
}
// NewServer constructs and returns a new *Server.
//
// The underlying mux is the standard http.ServeMux.
// The LogRequest, RecoverPanic and SecureHeaders middleware are
// automatically registered on construction.
func NewServer(middleware ...func(http.Handler) http.Handler) *Server {
s := &Server{
mux: http.NewServeMux(),
}
if middleware == nil {
s.Use(LogRequest, RecoverPanic, SecureHeaders)
} else {
s.Use(middleware...)
}
return s
}
// Handler registers the handler for the given pattern.
// It is a proxy for http.ServeMux.Handle().
func (s *Server) Handle(pattern string, handler http.Handler) {
s.mux.Handle(pattern, handler)
}
// HandleFunc registers the handler function for the given pattern.
// It is a proxy for http.ServeMux.HandleFunc().
func (s *Server) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
s.mux.HandleFunc(pattern, handler)
}
// Use registers one or more handlers as middleware for the Server.
func (s *Server) Use(middleware ...func(http.Handler) http.Handler) {
s.middleware = append(s.middleware, middleware...)
}
// Handler returns the Server as an http.Handler.
//
// It wraps the ServeMux with the middleware handlers, and returns
// the result as an http.Handler.
//
// The middleware is reversed, so that the earliest registered
// middleware is wrapped last.
func (s *Server) Handler() http.Handler {
slices.Reverse(s.middleware)
var h http.Handler = s.mux
for i := range s.middleware {
m := s.middleware[i]
h = m(h)
}
return h
}
// Serve starts an http server on the given address.
func (s *Server) Serve(addr string) error {
LogInfo("starting server", slog.String("port", addr))
return Serve(addr, s.Handler())
}
// ------------------------------------------------------------------
//
//
// Type: RouteMap
//
//
// ------------------------------------------------------------------
// RouteMap defines a mapping of routes to http handlers.
type RouteMap map[string]http.Handler
// RegisterRoutes registers all routes of the map to the Server.
func (rm RouteMap) RegisterRoutes(s *Server) {
for route := range rm {
s.Handle(route, rm[route])
}
}
// ------------------------------------------------------------------
//
//
// Serve Helper Function
//
//
// ------------------------------------------------------------------
// Serve starts an http server.
//
// The addr is the address to listen to. The addr assumes the format "host:port".
// The handler is the http.Handler to serve.
func Serve(addr string, handler http.Handler) error {
httpServer := &http.Server{
Addr: addr,
Handler: handler,
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 524288,
}
return httpServer.ListenAndServe()
}