-
Notifications
You must be signed in to change notification settings - Fork 153
/
handler.go
71 lines (57 loc) · 2.04 KB
/
handler.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
package server
import (
"context"
"fmt"
"net/http"
"os"
"github.com/go-logr/logr"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/weaveworks/weave-gitops/core/clustersmngr"
core "github.com/weaveworks/weave-gitops/core/server"
pbapp "github.com/weaveworks/weave-gitops/pkg/api/applications"
pbprofiles "github.com/weaveworks/weave-gitops/pkg/api/profiles"
"github.com/weaveworks/weave-gitops/pkg/server/auth"
"github.com/weaveworks/weave-gitops/pkg/server/middleware"
)
const (
AuthEnabledFeatureFlag = "WEAVE_GITOPS_AUTH_ENABLED"
)
var (
PublicRoutes = []string{
"/v1/featureflags",
}
)
func AuthEnabled() bool {
return os.Getenv(AuthEnabledFeatureFlag) == "true"
}
type Config struct {
AppConfig *ApplicationsConfig
AppOptions []ApplicationsOption
ProfilesConfig ProfilesConfig
CoreServerConfig core.CoreServerConfig
AuthServer *auth.AuthServer
}
func NewHandlers(ctx context.Context, log logr.Logger, cfg *Config) (http.Handler, error) {
mux := runtime.NewServeMux(middleware.WithGrpcErrorLogging(log))
httpHandler := middleware.WithLogging(log, mux)
if AuthEnabled() {
clustersFetcher, err := clustersmngr.NewSingleClusterFetcher(cfg.CoreServerConfig.RestCfg)
if err != nil {
return nil, fmt.Errorf("failed fetching clusters: %w", err)
}
httpHandler = clustersmngr.WithClustersClients(clustersFetcher, httpHandler)
httpHandler = auth.WithAPIAuth(httpHandler, cfg.AuthServer, PublicRoutes)
}
appsSrv := NewApplicationsServer(cfg.AppConfig, cfg.AppOptions...)
if err := pbapp.RegisterApplicationsHandlerServer(ctx, mux, appsSrv); err != nil {
return nil, fmt.Errorf("could not register application: %w", err)
}
profilesSrv := NewProfilesServer(log, cfg.ProfilesConfig)
if err := pbprofiles.RegisterProfilesHandlerServer(ctx, mux, profilesSrv); err != nil {
return nil, fmt.Errorf("could not register profiles: %w", err)
}
if err := core.Hydrate(ctx, mux, cfg.CoreServerConfig); err != nil {
return nil, fmt.Errorf("could not start up core servers: %w", err)
}
return httpHandler, nil
}