-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.go
More file actions
30 lines (24 loc) · 729 Bytes
/
server.go
File metadata and controls
30 lines (24 loc) · 729 Bytes
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
package urlzap
import (
"context"
"net/http"
)
// Handler returns HTTP handler which contains set redirects.
func Handler(ctx context.Context, conf Config) func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
if err := Read(ctx, "", conf.URLs, HTTPMuxCallback(conf.HTTP.BasePath, mux)); err != nil {
panic(err)
}
return mux.ServeHTTP
}
// Server HTTP server with redirect set-up.
type Server struct {
handler func(http.ResponseWriter, *http.Request)
}
// NewServer returns an instance of Server.
func NewServer(ctx context.Context, config Config) *Server {
return &Server{handler: Handler(ctx, config)}
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.handler(w, r)
}