forked from nytimes/gizmo
/
config.go
83 lines (71 loc) · 2.5 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package kit
import (
"runtime"
"time"
"github.com/kelseyhightower/envconfig"
)
// Config holds info required to configure a gizmo kit.Server.
//
// This struct is loaded from the environment at Run and only made public to expose
// for documentation.
type Config struct {
// HealthCheckPath is used by server to init the proper HealthCheckHandler.
// If empty, this will default to '/healthz'.
HealthCheckPath string `envconfig:"GIZMO_HEALTH_CHECK_PATH"`
// MaxHeaderBytes can be used to override the default of 1<<20.
MaxHeaderBytes int `envconfig:"GIZMO_MAX_HEADER_BYTES"`
// ReadTimeout can be used to override the default http server timeout of 10s.
// The string should be formatted like a time.Duration string.
ReadTimeout time.Duration `envconfig:"GIZMO_READ_TIMEOUT"`
// WriteTimeout can be used to override the default http server timeout of 10s.
// The string should be formatted like a time.Duration string.
WriteTimeout time.Duration `envconfig:"GIZMO_WRITE_TIMEOUT"`
// IdleTimeout can be used to override the default http server timeout of 120s.
// The string should be formatted like a time.Duration string.
IdleTimeout time.Duration `envconfig:"GIZMO_IDLE_TIMEOUT"`
// ShutdownTimeout can be used to override the default http server shutdown timeout
// of 5m.
ShutdownTimeout time.Duration `envconfig:"GIZMO_SHUTDOWN_TIMEOUT"`
// GOMAXPROCS can be used to override the default GOMAXPROCS.
GOMAXPROCS int `envconfig:"GIZMO_GOMAXPROCS"`
// HTTPPort is the port the server implementation will serve HTTP over.
// The default is 8080
HTTPPort int `envconfig:"HTTP_PORT"`
// RPCPort is the port the server implementation will serve RPC over.
// The default is 8081.
RPCPort int `envconfig:"RPC_PORT"`
// Enable pprof Profiling. Off by default.
EnablePProf bool `envconfig:"ENABLE_PPROF"`
}
func loadConfig() Config {
var cfg Config
envconfig.MustProcess("", &cfg)
if cfg.HTTPPort == 0 {
cfg.HTTPPort = 8080
}
if cfg.RPCPort == 0 {
cfg.RPCPort = 8081
}
if cfg.MaxHeaderBytes == 0 {
cfg.MaxHeaderBytes = 1 << 20
}
if cfg.ReadTimeout.Nanoseconds() == 0 {
cfg.ReadTimeout = 10 * time.Second
}
if cfg.IdleTimeout.Nanoseconds() == 0 {
cfg.IdleTimeout = 120 * time.Second
}
if cfg.WriteTimeout.Nanoseconds() == 0 {
cfg.WriteTimeout = 10 * time.Second
}
if cfg.GOMAXPROCS > 0 {
runtime.GOMAXPROCS(cfg.GOMAXPROCS)
}
if cfg.HealthCheckPath == "" {
cfg.HealthCheckPath = "/healthz"
}
if cfg.ShutdownTimeout.Nanoseconds() == 0 {
cfg.ShutdownTimeout = 5 * time.Minute
}
return cfg
}