-
Notifications
You must be signed in to change notification settings - Fork 2
/
wire_gen.go
106 lines (84 loc) · 2.55 KB
/
wire_gen.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
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
package http
import (
"context"
"crypto/tls"
"github.com/elastic/harp-plugins/server/cmd/harp-server/internal/config"
"github.com/elastic/harp-plugins/server/cmd/harp-server/internal/dispatchers/http/routes"
"github.com/elastic/harp-plugins/server/pkg/server/manager"
"github.com/elastic/harp-plugins/server/pkg/server/storage/backends/container"
"github.com/elastic/harp/pkg/sdk/log"
"github.com/elastic/harp/pkg/sdk/tlsconfig"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"go.uber.org/zap"
"net/http"
"time"
)
// Injectors from wire.go:
func setup(ctx context.Context, cfg *config.Configuration) (*http.Server, error) {
backend, err := backendManager(ctx, cfg)
if err != nil {
return nil, err
}
server, err := httpServer(ctx, cfg, backend)
if err != nil {
return nil, err
}
return server, nil
}
// wire.go:
func backendManager(ctx context.Context, cfg *config.Configuration) (manager.Backend, error) {
bm := manager.Default()
for _, b := range cfg.Backends {
if err := bm.Register(ctx, b.NS, b.URL); err != nil {
return nil, err
}
}
return bm, nil
}
func httpServer(ctx context.Context, cfg *config.Configuration, bm manager.Backend) (*http.Server, error) {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(60 * time.Second))
container.SetKeyring(cfg.Keyring)
backendRouter, err := routes.Backends(ctx, cfg, bm)
if err != nil {
return nil, err
}
r.Route("/api/v1", func(r chi.Router) {
r.Mount("/", http.StripPrefix("/api/v1", backendRouter))
})
server := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
Handler: r,
}
if cfg.HTTP.UseTLS {
clientAuth := tls.VerifyClientCertIfGiven
if cfg.HTTP.TLS.ClientAuthenticationRequired {
clientAuth = tls.RequireAndVerifyClientCert
}
tlsConfig, err := tlsconfig.Server(&tlsconfig.Options{
KeyFile: cfg.HTTP.TLS.PrivateKeyPath,
CertFile: cfg.HTTP.TLS.CertificatePath,
CAFile: cfg.HTTP.TLS.CACertificatePath,
ClientAuth: clientAuth,
})
if err != nil {
log.For(ctx).Error("Unable to build TLS configuration from settings", zap.Error(err))
return nil, err
}
server.TLSConfig = tlsConfig
} else {
log.For(ctx).Info("No transport encryption enabled for HTTP server")
}
return server, nil
}