-
Notifications
You must be signed in to change notification settings - Fork 724
Expand file tree
/
Copy pathserver.go
More file actions
102 lines (84 loc) · 2.66 KB
/
server.go
File metadata and controls
102 lines (84 loc) · 2.66 KB
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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package admin
import (
"context"
"net/http"
"net/http/pprof"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/envoyproxy/gateway/internal/admin/console"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/message"
)
type Config struct {
Server config.Server
ProviderResources *message.ProviderResources
RunnerErrors *message.RunnerErrors
}
type Runner struct {
cfg *config.Server
server *http.Server
providerResources *message.ProviderResources
}
func New(cfg *Config) *Runner {
return &Runner{
cfg: &cfg.Server,
providerResources: cfg.ProviderResources,
}
}
func (r *Runner) Start(_ context.Context) error {
if r.cfg.EnvoyGateway.GetEnvoyGatewayAdmin().EnableDumpConfig {
spewConfig := spew.NewDefaultConfig()
spewConfig.DisableMethods = true
spewConfig.Dump(r.cfg)
}
return r.start()
}
func (r *Runner) Name() string {
return "admin"
}
func (r *Runner) Close() error {
if r.server != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return r.server.Shutdown(ctx)
}
return nil
}
func (r *Runner) start() error {
handlers := http.NewServeMux()
address := r.cfg.EnvoyGateway.GetEnvoyGatewayAdminAddress()
adminConfig := r.cfg.EnvoyGateway.GetEnvoyGatewayAdmin()
enablePprof := adminConfig.EnablePprof
adminLogger := r.cfg.Logger.WithName("admin")
adminLogger.Info("starting admin server", "address", address, "enablePprof", enablePprof, "enableConsole", true)
// Register console handlers (always enabled)
consoleHandler := console.NewHandler(r.cfg, r.providerResources)
consoleHandler.RegisterRoutes(handlers)
if enablePprof {
// Serve pprof endpoints to aid in live debugging.
handlers.HandleFunc("/debug/pprof/", pprof.Index)
handlers.HandleFunc("/debug/pprof/profile", pprof.Profile)
handlers.HandleFunc("/debug/pprof/trace", pprof.Trace)
handlers.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
handlers.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
}
r.server = &http.Server{
Handler: handlers,
Addr: address,
ReadTimeout: 5 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
// Listen And Serve Admin Server.
go func() {
if err := r.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
adminLogger.Error(err, "start admin server failed")
}
}()
return nil
}