-
Notifications
You must be signed in to change notification settings - Fork 117
/
option.go
41 lines (37 loc) · 1.15 KB
/
option.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
package option
import (
"context"
"fmt"
"net/http"
"connectrpc.com/connect"
log "github.com/sirupsen/logrus"
libClient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/akuity/kargo/internal/api/config"
"github.com/akuity/kargo/internal/logging"
)
func NewHandlerOption(
ctx context.Context,
cfg config.ServerConfig,
internalClient libClient.Client,
) (connect.HandlerOption, error) {
interceptors := []connect.Interceptor{
newLogInterceptor(logging.LoggerFromContext(ctx), loggingIgnorableMethods),
newErrorInterceptor(),
}
if !cfg.LocalMode {
authInterceptor, err := newAuthInterceptor(ctx, cfg, internalClient)
if err != nil {
return nil, fmt.Errorf("initialize authentication interceptor: %w", err)
}
interceptors = append(interceptors, authInterceptor)
}
return connect.WithHandlerOptions(
connect.WithInterceptors(interceptors...),
connect.WithRecover(
func(ctx context.Context, _ connect.Spec, _ http.Header, r any) error {
logging.LoggerFromContext(ctx).Log(log.ErrorLevel, takeStacktrace(defaultStackLength, 3))
return connect.NewError(
connect.CodeInternal, fmt.Errorf("panic: %v", r))
}),
), nil
}