Skip to content

Commit 8cc73d9

Browse files
committed
http_server and log config sections
1 parent c22f2d4 commit 8cc73d9

File tree

10 files changed

+81
-72
lines changed

10 files changed

+81
-72
lines changed

internal/app/mux.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,10 @@ func runHTTPServers(
358358

359359
apiDisabled := cfg.HttpAPI.Disabled
360360

361-
httpAddress := cfg.Address
362-
httpPort := strconv.Itoa(cfg.Port)
363-
httpInternalAddress := cfg.InternalAddress
364-
httpInternalPort := cfg.InternalPort
361+
httpAddress := cfg.HTTP.Address
362+
httpPort := strconv.Itoa(cfg.HTTP.Port)
363+
httpInternalAddress := cfg.HTTP.InternalAddress
364+
httpInternalPort := cfg.HTTP.InternalPort
365365

366366
if httpInternalAddress == "" && httpAddress != "" {
367367
// If custom internal address not explicitly set we try to reuse main
@@ -387,7 +387,7 @@ func runHTTPServers(
387387
portFlags |= HandlerWebsocket
388388
}
389389
if cfg.WebTransport.Enabled {
390-
if !cfg.HTTP3.Enabled {
390+
if !cfg.HTTP.HTTP3.Enabled {
391391
log.Fatal().Msg("can not enable webtransport without experimental HTTP/3")
392392
}
393393
portFlags |= HandlerWebtransport
@@ -448,8 +448,8 @@ func runHTTPServers(
448448
log.Fatal().Err(err).Msg("can not get TLS config")
449449
}
450450
var internalTLSConfig *tls.Config
451-
if cfg.InternalTLS.Enabled {
452-
internalTLSConfig, err = cfg.InternalTLS.ToGoTLSConfig("internal_tls")
451+
if cfg.HTTP.InternalTLS.Enabled {
452+
internalTLSConfig, err = cfg.HTTP.InternalTLS.ToGoTLSConfig("internal_tls")
453453
if err != nil {
454454
log.Fatal().Err(err).Msg("can not get internal TLS config")
455455
}
@@ -463,14 +463,14 @@ func runHTTPServers(
463463
continue
464464
}
465465
var addrTLSConfig *tls.Config
466-
if !cfg.TLSExternal || addr == externalAddr {
466+
if !cfg.HTTP.TLSExternal || addr == externalAddr {
467467
addrTLSConfig = tlsConfig
468468
}
469-
if addr != externalAddr && cfg.InternalTLS.Enabled {
469+
if addr != externalAddr && cfg.HTTP.InternalTLS.Enabled {
470470
addrTLSConfig = internalTLSConfig
471471
}
472472

473-
useHTTP3 := cfg.HTTP3.Enabled && addr == externalAddr
473+
useHTTP3 := cfg.HTTP.HTTP3.Enabled && addr == externalAddr
474474

475475
var wtServer *webtransport.Server
476476
if useHTTP3 {
@@ -516,7 +516,7 @@ func runHTTPServers(
516516
if addrTLSConfig == nil {
517517
log.Fatal().Msg("HTTP/3 requires TLS configured")
518518
}
519-
if cfg.TLSAutocert.Enabled {
519+
if cfg.HTTP.TLSAutocert.Enabled {
520520
log.Fatal().Msg("can not use HTTP/3 with autocert")
521521
}
522522

internal/app/node.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func centrifugeNodeConfig(version string, edition string, cfgContainer *config.C
3434
cfg.RecoveryMaxPublicationLimit = appCfg.Client.RecoveryMaxPublicationLimit
3535
cfg.HistoryMetaTTL = appCfg.Channel.HistoryMetaTTL.ToDuration()
3636
cfg.ClientConnectIncludeServerTime = appCfg.Client.ConnectIncludeServerTime
37-
cfg.LogLevel = logging.CentrifugeLogLevel(strings.ToLower(appCfg.LogLevel))
37+
cfg.LogLevel = logging.CentrifugeLogLevel(strings.ToLower(appCfg.Log.Level))
3838
cfg.LogHandler = logHandler
3939
if appCfg.Client.ConnectCodeToUnidirectionalDisconnect.Enabled {
4040
uniCodeTransforms := make(map[uint32]centrifuge.Disconnect)
@@ -53,7 +53,7 @@ func nodeName(cfg config.Config) string {
5353
if name != "" {
5454
return name
5555
}
56-
port := strconv.Itoa(cfg.Port)
56+
port := strconv.Itoa(cfg.HTTP.Port)
5757
var hostname string
5858
hostname, err := os.Hostname()
5959
if err != nil {

internal/app/tls.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import (
1616
var startHTTPChallengeServerOnce sync.Once
1717

1818
func GetTLSConfig(cfg config.Config) (*tls.Config, error) {
19-
tlsEnabled := cfg.TLS.Enabled
20-
tlsAutocertEnabled := cfg.TLSAutocert.Enabled
21-
tlsAutocertHostWhitelist := cfg.TLSAutocert.HostWhitelist
22-
tlsAutocertCacheDir := cfg.TLSAutocert.CacheDir
23-
tlsAutocertEmail := cfg.TLSAutocert.Email
24-
tlsAutocertServerName := cfg.TLSAutocert.ServerName
25-
tlsAutocertHTTP := cfg.TLSAutocert.HTTP
26-
tlsAutocertHTTPAddr := cfg.TLSAutocert.HTTPAddr
19+
tlsEnabled := cfg.HTTP.TLS.Enabled
20+
tlsAutocertEnabled := cfg.HTTP.TLSAutocert.Enabled
21+
tlsAutocertHostWhitelist := cfg.HTTP.TLSAutocert.HostWhitelist
22+
tlsAutocertCacheDir := cfg.HTTP.TLSAutocert.CacheDir
23+
tlsAutocertEmail := cfg.HTTP.TLSAutocert.Email
24+
tlsAutocertServerName := cfg.HTTP.TLSAutocert.ServerName
25+
tlsAutocertHTTP := cfg.HTTP.TLSAutocert.HTTP
26+
tlsAutocertHTTPAddr := cfg.HTTP.TLSAutocert.HTTPAddr
2727

2828
if tlsAutocertEnabled {
2929
certManager := autocert.Manager{
@@ -69,7 +69,7 @@ func GetTLSConfig(cfg config.Config) (*tls.Config, error) {
6969

7070
} else if tlsEnabled {
7171
// Autocert disabled - just try to use provided SSL cert and key files.
72-
return cfg.TLS.ToGoTLSConfig("http_server")
72+
return cfg.HTTP.TLS.ToGoTLSConfig("http_server")
7373
}
7474

7575
return nil, nil

internal/config/config.go

+12-34
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,12 @@ import (
1818
)
1919

2020
type Config struct {
21-
// Address to bind HTTP server to.
22-
Address string `mapstructure:"address" json:"address" envconfig:"address" toml:"address" yaml:"address"`
23-
// Port to bind HTTP server to.
24-
Port int `mapstructure:"port" json:"port" envconfig:"port" default:"8000" toml:"port" yaml:"port"`
25-
// InternalAddress to bind internal HTTP server to. Internal server is used to serve endpoints
26-
// which are normally should not be exposed to the outside world.
27-
InternalAddress string `mapstructure:"internal_address" json:"internal_address" envconfig:"internal_address" toml:"internal_address" yaml:"internal_address"`
28-
// InternalPort to bind internal HTTP server to.
29-
InternalPort string `mapstructure:"internal_port" json:"internal_port" envconfig:"internal_port" toml:"internal_port" yaml:"internal_port"`
30-
// PidFile is a path to write PID file with server's PID.
21+
// PidFile is a path to write a file with Centrifugo process PID.
3122
PidFile string `mapstructure:"pid_file" json:"pid_file" envconfig:"pid_file" toml:"pid_file" yaml:"pid_file"`
32-
// LogLevel is a log level for Centrifugo logger. Supported values: none, trace, debug, info, warn, error.
33-
LogLevel string `mapstructure:"log_level" json:"log_level" envconfig:"log_level" default:"info" toml:"log_level" yaml:"log_level"`
34-
// LogFile is a path to log file. If not set logs go to stdout.
35-
LogFile string `mapstructure:"log_file" json:"log_file" envconfig:"log_file" toml:"log_file" yaml:"log_file"`
36-
37-
// TLS configuration for HTTP server.
38-
TLS configtypes.TLSConfig `mapstructure:"tls" json:"tls" envconfig:"tls" toml:"tls" yaml:"tls"`
39-
// TLSAutocert for automatic TLS certificates from ACME provider (ex. Let's Encrypt).
40-
TLSAutocert configtypes.TLSAutocert `mapstructure:"tls_autocert" json:"tls_autocert" envconfig:"tls_autocert" toml:"tls_autocert" yaml:"tls_autocert"`
41-
// TLSExternal enables TLS only for external HTTP endpoints.
42-
TLSExternal bool `mapstructure:"tls_external" json:"tls_external" envconfig:"tls_external" toml:"tls_external" yaml:"tls_external"`
43-
// InternalTLS is a custom configuration for internal HTTP endpoints. If not set InternalTLS will be the same as TLS.
44-
InternalTLS configtypes.TLSConfig `mapstructure:"internal_tls" json:"internal_tls" envconfig:"internal_tls" toml:"internal_tls" yaml:"internal_tls"`
45-
23+
// HTTP is a configuration for Centrifugo HTTP server.
24+
HTTP configtypes.HTTPServer `mapstructure:"http_server" json:"http_server" envconfig:"http_server" toml:"http_server" yaml:"http_server"`
25+
// Log is a configuration for logging.
26+
Log configtypes.Log `mapstructure:"log" json:"log" envconfig:"log" toml:"log" yaml:"log"`
4627
// Engine is a configuration for Centrifugo engine. It's a handy combination of Broker and PresenceManager.
4728
// Currently only memory and redis engines are supported – both implement all the features. For more granular
4829
// control use Broker and PresenceManager options.
@@ -109,9 +90,6 @@ type Config struct {
10990
// Debug helps to enable Go profiling endpoints.
11091
Debug configtypes.Debug `mapstructure:"debug" json:"debug" envconfig:"debug" toml:"debug" yaml:"debug"`
11192

112-
// HTTP3 enables HTTP/3 support. EXPERIMENTAL.
113-
HTTP3 configtypes.HTTP3 `mapstructure:"http3" json:"http3" envconfig:"http3" toml:"http3" yaml:"http3"`
114-
11593
// OpenTelemetry is a configuration for OpenTelemetry tracing.
11694
OpenTelemetry configtypes.OpenTelemetry `mapstructure:"opentelemetry" json:"opentelemetry" envconfig:"opentelemetry" toml:"opentelemetry" yaml:"opentelemetry"`
11795
// Graphite is a configuration for export metrics to Graphite.
@@ -136,18 +114,18 @@ type Meta struct {
136114
}
137115

138116
func DefineFlags(rootCmd *cobra.Command) {
139-
rootCmd.Flags().StringP("address", "a", "", "interface address to listen on")
140-
rootCmd.Flags().StringP("port", "p", "8000", "port to bind HTTP server to")
141-
rootCmd.Flags().StringP("internal_address", "", "", "custom interface address to listen on for internal endpoints")
142-
rootCmd.Flags().StringP("internal_port", "", "", "custom port for internal endpoints")
117+
rootCmd.Flags().StringP("pid_file", "", "", "optional path to create PID file")
118+
rootCmd.Flags().StringP("http_server.address", "a", "", "interface address to listen on")
119+
rootCmd.Flags().StringP("http_server.port", "p", "8000", "port to bind HTTP server to")
120+
rootCmd.Flags().StringP("http_server.internal_address", "", "", "custom interface address to listen on for internal endpoints")
121+
rootCmd.Flags().StringP("http_server.internal_port", "", "", "custom port for internal endpoints")
143122
rootCmd.Flags().StringP("engine.type", "", "memory", "broker to use: ex. redis")
144123
rootCmd.Flags().BoolP("broker.enabled", "", false, "enable broker")
145124
rootCmd.Flags().StringP("broker.type", "", "memory", "broker to use: ex. redis")
146125
rootCmd.Flags().BoolP("presence_manager.enabled", "", false, "enable presence manager")
147126
rootCmd.Flags().StringP("presence_manager.type", "", "memory", "presence manager to use: ex. redis")
148-
rootCmd.Flags().StringP("log_level", "", "info", "set the log level: trace, debug, info, error, fatal or none")
149-
rootCmd.Flags().StringP("log_file", "", "", "optional log file - if not specified logs go to STDOUT")
150-
rootCmd.Flags().StringP("pid_file", "", "", "optional path to create PID file")
127+
rootCmd.Flags().StringP("log.level", "", "info", "set the log level: trace, debug, info, error, fatal or none")
128+
rootCmd.Flags().StringP("log.file", "", "", "optional log file - if not specified logs go to STDOUT")
151129
rootCmd.Flags().BoolP("debug.enabled", "", false, "enable debug endpoints")
152130
rootCmd.Flags().BoolP("admin.enabled", "", false, "enable admin web interface")
153131
rootCmd.Flags().BoolP("admin.external", "", false, "expose admin web interface on external port")

internal/config/testdata/config.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
2-
"tls": {
3-
"enabled": true
2+
"http_server": {
3+
"tls": {
4+
"enabled": true
5+
}
46
},
57
"engine": {
68
"type": "redis",

internal/config/testdata/config.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
[tls]
2-
enabled = true
1+
[http_server]
2+
3+
[http_server.tls]
4+
enabled = true
35

46
[engine]
57
type = "redis"
68

79
[engine.redis]
10+
address = "redis:6379"
811
presence_ttl = "30s"
912

1013
[client]

internal/config/testdata/config.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
---
2-
tls:
3-
enabled: true
2+
http_server:
3+
tls:
4+
enabled: true
45
engine:
56
type: redis
67
redis:
8+
address: redis:6379
79
presence_ttl: 30s
810
client:
911
allowed_origins:

internal/configtypes/types.go

+31
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,37 @@ import (
88
"net"
99
)
1010

11+
// HTTPServer configuration.
12+
type HTTPServer struct {
13+
// Address to bind HTTP server to.
14+
Address string `mapstructure:"address" json:"address" envconfig:"address" toml:"address" yaml:"address"`
15+
// Port to bind HTTP server to.
16+
Port int `mapstructure:"port" json:"port" envconfig:"port" default:"8000" toml:"port" yaml:"port"`
17+
// InternalAddress to bind internal HTTP server to. Internal server is used to serve endpoints
18+
// which are normally should not be exposed to the outside world.
19+
InternalAddress string `mapstructure:"internal_address" json:"internal_address" envconfig:"internal_address" toml:"internal_address" yaml:"internal_address"`
20+
// InternalPort to bind internal HTTP server to.
21+
InternalPort string `mapstructure:"internal_port" json:"internal_port" envconfig:"internal_port" toml:"internal_port" yaml:"internal_port"`
22+
// TLS configuration for HTTP server.
23+
TLS TLSConfig `mapstructure:"tls" json:"tls" envconfig:"tls" toml:"tls" yaml:"tls"`
24+
// TLSAutocert for automatic TLS certificates from ACME provider (ex. Let's Encrypt).
25+
TLSAutocert TLSAutocert `mapstructure:"tls_autocert" json:"tls_autocert" envconfig:"tls_autocert" toml:"tls_autocert" yaml:"tls_autocert"`
26+
// TLSExternal enables TLS only for external HTTP endpoints.
27+
TLSExternal bool `mapstructure:"tls_external" json:"tls_external" envconfig:"tls_external" toml:"tls_external" yaml:"tls_external"`
28+
// InternalTLS is a custom configuration for internal HTTP endpoints. If not set InternalTLS will be the same as TLS.
29+
InternalTLS TLSConfig `mapstructure:"internal_tls" json:"internal_tls" envconfig:"internal_tls" toml:"internal_tls" yaml:"internal_tls"`
30+
// HTTP3 allows enabling HTTP/3 support. EXPERIMENTAL!
31+
HTTP3 HTTP3 `mapstructure:"http3" json:"http3" envconfig:"http3" toml:"http3" yaml:"http3"`
32+
}
33+
34+
// Log configuration.
35+
type Log struct {
36+
// Level is a log level for Centrifugo logger. Supported values: none, trace, debug, info, warn, error.
37+
Level string `mapstructure:"level" default:"info" json:"level" envconfig:"level" toml:"level" yaml:"level"`
38+
// File is a path to log file. If not set logs go to stdout.
39+
File string `mapstructure:"file" json:"file" envconfig:"file" toml:"file" yaml:"file"`
40+
}
41+
1142
// Token common configuration.
1243
type Token struct {
1344
HMACSecretKey string `mapstructure:"hmac_secret_key" json:"hmac_secret_key" envconfig:"hmac_secret_key" yaml:"hmac_secret_key" toml:"hmac_secret_key"`

internal/logging/logging.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ func Setup(cfg config.Config) (centrifuge.LogHandler, func()) {
125125
var writers []io.Writer
126126

127127
var file *os.File
128-
if cfg.LogFile != "" {
128+
if cfg.Log.File != "" {
129129
var err error
130-
file, err = os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
130+
file, err = os.OpenFile(cfg.Log.File, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
131131
if err != nil {
132132
log.Fatal().Err(err).Msg("error opening log file")
133133
}
@@ -141,7 +141,7 @@ func Setup(cfg config.Config) (centrifuge.LogHandler, func()) {
141141
}
142142
}
143143

144-
logLevel, ok := logLevelMatches[strings.ToUpper(cfg.LogLevel)]
144+
logLevel, ok := logLevelMatches[strings.ToUpper(cfg.Log.Level)]
145145
if !ok {
146146
logLevel = zerolog.InfoLevel
147147
}

internal/middleware/log.go

-7
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,3 @@ func (lrw *statusResponseWriter) Connection() http3.Connection {
7171
func (lrw *statusResponseWriter) HTTPStream() http3.Stream {
7272
return lrw.ResponseWriter.(http3.HTTPStreamer).HTTPStream()
7373
}
74-
75-
// CloseNotify implements http.CloseNotifier.
76-
//
77-
//goland:noinspection GoDeprecation
78-
func (lrw *statusResponseWriter) CloseNotify() <-chan bool {
79-
return lrw.ResponseWriter.(http.CloseNotifier).CloseNotify()
80-
}

0 commit comments

Comments
 (0)