-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_router.go
146 lines (121 loc) · 3.52 KB
/
http_router.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
package router
import (
"context"
"crypto/tls"
"fmt"
"log/slog"
"net"
"net/http"
"github.com/coreos/go-systemd/daemon"
chi "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/spf13/afero"
"github.com/theduckcompany/duckcloud/internal/tools"
"go.uber.org/fx"
)
type API struct{}
type Config struct {
Addr string
CertFile string
KeyFile string
HostNames []string
TLS bool
Secure bool
}
type Registerer interface {
Register(r chi.Router, mids *Middlewares)
}
//nolint:gochecknoinits // This is the only way to ensure that we register the methods only once into the global router
func init() {
chi.RegisterMethod("ACL")
chi.RegisterMethod("CANCELUPLOAD")
chi.RegisterMethod("CHECKIN")
chi.RegisterMethod("CHECKOUT")
chi.RegisterMethod("COPY")
chi.RegisterMethod("MKCALENDAR")
chi.RegisterMethod("MKCOL")
chi.RegisterMethod("MOVE")
chi.RegisterMethod("OPTIONS")
chi.RegisterMethod("PROPFIND")
chi.RegisterMethod("PROPPATCH")
chi.RegisterMethod("REPORT")
chi.RegisterMethod("SEARCH")
chi.RegisterMethod("UNCHECKOUT")
chi.RegisterMethod("VERSION-CONTROL")
}
func NewServer(routes []Registerer, cfg Config, lc fx.Lifecycle, mids *Middlewares, tools tools.Tools, fs afero.Fs) (*API, error) {
handler, err := createHandler(cfg, routes, mids)
if err != nil {
return nil, fmt.Errorf("failed to create the listener: %w", err)
}
httpLogger := slog.NewLogLogger(tools.Logger().Handler(), slog.LevelError)
srv := &http.Server{
Addr: cfg.Addr,
Handler: handler,
ErrorLog: httpLogger,
}
if cfg.TLS {
cert, err := afero.ReadFile(fs, cfg.CertFile)
if err != nil {
return nil, fmt.Errorf("failed to load the TLS certification file: %w", err)
}
key, err := afero.ReadFile(fs, cfg.KeyFile)
if err != nil {
return nil, fmt.Errorf("failed to load the TLS key file: %w", err)
}
certif, err := tls.X509KeyPair(cert, key)
if err != nil {
return nil, fmt.Errorf("failed to generate the X509 key pair: %w", err)
}
srv.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{certif},
}
}
lc.Append(fx.Hook{
OnStart: func(_ context.Context) error {
ln, err := net.Listen("tcp", cfg.Addr)
if err != nil {
return err
}
tools.Logger().Info("start listening", slog.String("host", ln.Addr().String()), slog.Int("routes", len(handler.Routes())))
if cfg.TLS {
go srv.ServeTLS(ln, "", "")
} else {
go srv.Serve(ln)
}
for _, route := range handler.Routes() {
tools.Logger().Debug("expose endpoint", slog.String("host", ln.Addr().String()), slog.String("route", route.Pattern))
}
return nil
},
OnStop: func(ctx context.Context) error {
return srv.Shutdown(ctx)
},
})
daemon.SdNotify(false, daemon.SdNotifyReady)
return &API{}, nil
}
func createHandler(cfg Config, routes []Registerer, mids *Middlewares) (chi.Router, error) {
r := chi.NewMux()
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
})
if cfg.Secure {
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Strict-Transport-Security", "max-age=15768000; preload")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Referrer-Policy", "same-origin")
w.Header().Set("Permissions-Policy", "")
next.ServeHTTP(w, r)
})
})
}
r.Use(mids.CORS)
r.Use(middleware.RequestID)
for _, svc := range routes {
svc.Register(r, mids)
}
return r, nil
}