-
Notifications
You must be signed in to change notification settings - Fork 36
/
config.go
81 lines (70 loc) · 1.78 KB
/
config.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
package httpserver
import (
"log/slog"
"net/http"
)
// DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *Config {
return &Config{
Logger: slog.Default(),
HTTPServer: &http.Server{},
ServeMux: http.NewServeMux(),
URL: "/interactions/callback",
Address: ":80",
}
}
// Config lets you configure your Server instance.
type Config struct {
Logger *slog.Logger
HTTPServer *http.Server
ServeMux *http.ServeMux
URL string
Address string
CertFile string
KeyFile string
}
// ConfigOpt is a type alias for a function that takes a Config and is used to configure your Server.
type ConfigOpt func(config *Config)
// Apply applies the given ConfigOpt(s) to the Config
func (c *Config) Apply(opts []ConfigOpt) {
for _, opt := range opts {
opt(c)
}
}
// WithLogger sets the Logger of the Config.
func WithLogger(logger *slog.Logger) ConfigOpt {
return func(config *Config) {
config.Logger = logger
}
}
// WithHTTPServer sets the http.Server of the Config.
func WithHTTPServer(httpServer *http.Server) ConfigOpt {
return func(config *Config) {
config.HTTPServer = httpServer
}
}
// WithServeMux sets the http.ServeMux of the Config.
func WithServeMux(serveMux *http.ServeMux) ConfigOpt {
return func(config *Config) {
config.ServeMux = serveMux
}
}
// WithURL sets the URL of the Config.
func WithURL(url string) ConfigOpt {
return func(config *Config) {
config.URL = url
}
}
// WithAddress sets the Address of the Config.
func WithAddress(address string) ConfigOpt {
return func(config *Config) {
config.Address = address
}
}
// WithTLS sets the CertFile of the Config.
func WithTLS(certFile string, keyFile string) ConfigOpt {
return func(config *Config) {
config.CertFile = certFile
config.KeyFile = keyFile
}
}