Skip to content

Commit

Permalink
refactor config
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Jul 21, 2018
1 parent 5411cbc commit 800aae8
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type AppConfig struct {
Timeout string `yaml:"timeout" json:"timeout"`
Wait string `yaml:"wait" json:"wait"`
} `yaml:"gracefulShutdown" json:"gracefulShutdown"`
TLS struct {
TLS *struct {
CertFile string `yaml:"certFile" json:"certFile"`
KeyFile string `yaml:"keyFile" json:"keyFile"`
Profile string `yaml:"profile" json:"profile"`
Expand Down Expand Up @@ -83,46 +83,46 @@ func (app *App) Config(config AppConfig) *App {
app.Template().Config(cfg)
}

// load server config
if config.Server.Addr != "" {
app.Addr = config.Server.Addr
}
parseDuration(config.Server.ReadTimeout, &app.ReadTimeout)
parseDuration(config.Server.ReadHeaderTimeout, &app.ReadHeaderTimeout)
parseDuration(config.Server.WriteTimeout, &app.WriteTimeout)
parseDuration(config.Server.IdleTimeout, &app.IdleTimeout)

{
// tls
tls := config.Server.TLS
if tls.CertFile != "" {
app.certFile = tls.CertFile
}
if tls.KeyFile != "" {
app.keyFile = tls.KeyFile
// server config
server := config.Server

if server.Addr != "" {
app.Addr = server.Addr
}
if tls.Profile != "" {
app.tlsProfile = strings.ToLower(tls.Profile)
parseDuration(server.ReadTimeout, &app.ReadTimeout)
parseDuration(server.ReadHeaderTimeout, &app.ReadHeaderTimeout)
parseDuration(server.WriteTimeout, &app.WriteTimeout)
parseDuration(server.IdleTimeout, &app.IdleTimeout)

if tls := server.TLS; tls != nil {
// TODO: auto generate self-signed tls if cert file, key file empty
if tls.CertFile != "" {
app.certFile = tls.CertFile
}
if tls.KeyFile != "" {
app.keyFile = tls.KeyFile
}
if tls.Profile != "" {
app.tlsProfile = strings.ToLower(tls.Profile)
}
}
}

// load graceful config
if config.Server.GracefulShutdown != nil {
if app.gracefulShutdown == nil {
app.gracefulShutdown = &gracefulShutdown{}
if gs := server.GracefulShutdown; gs != nil {
if app.gracefulShutdown == nil {
app.gracefulShutdown = &gracefulShutdown{}
}

parseDuration(gs.Timeout, &app.gracefulShutdown.timeout)
parseDuration(gs.Wait, &app.gracefulShutdown.wait)
}
parseDuration(config.Server.GracefulShutdown.Timeout, &app.gracefulShutdown.timeout)
parseDuration(config.Server.GracefulShutdown.Wait, &app.gracefulShutdown.wait)
}

{
httpsRedirect := config.Server.HTTPSRedirect
if httpsRedirect != nil {
if httpsRedirect.Addr == "" {
httpsRedirect.Addr = ":80"
if rd := server.HTTPSRedirect; rd != nil {
if rd.Addr == "" {
rd.Addr = ":80"
}

go StartHTTPSRedirectServer(httpsRedirect.Addr)
go StartHTTPSRedirectServer(rd.Addr)
}
}

Expand Down

0 comments on commit 800aae8

Please sign in to comment.