-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
59 lines (46 loc) · 1.22 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
package golibre
import (
"fmt"
"log/slog"
"net/http"
)
type configOption func(s *config)
type config struct {
transport *http.Transport
existingJWTToken string
requestPreProcessors []RequestPreProcessor
}
type RequestPreProcessor interface {
ProcessRequest(r *http.Request) error
}
type RequestPreProcessorFunc func(*http.Request) error
func (p RequestPreProcessorFunc) ProcessRequest(r *http.Request) error {
return p(r)
}
func WithExistingJWTToken(existingToken string) configOption {
return func(s *config) {
s.existingJWTToken = existingToken
}
}
func WithTLSInsecureSkipVerify() configOption {
return func(s *config) {
s.transport.TLSClientConfig.InsecureSkipVerify = true
}
}
func WithRequestPreProcessor(requestPreProcessor RequestPreProcessor) configOption {
return func(s *config) {
s.requestPreProcessors = append(s.requestPreProcessors, requestPreProcessor)
}
}
func WithSlogger(logger *slog.Logger) configOption {
return WithRequestPreProcessor(&sloggerWrapper{
logger: logger,
})
}
type sloggerWrapper struct {
logger *slog.Logger
}
func (s *sloggerWrapper) ProcessRequest(r *http.Request) error {
s.logger.Debug(fmt.Sprintf("Request: %s %s", r.Method, r.URL.String()))
return nil
}