Skip to content

Commit

Permalink
Merge pull request #4 from longsleep/tlsconfig
Browse files Browse the repository at this point in the history
tls.Config fixes
  • Loading branch information
longsleep committed May 5, 2014
2 parents d640b0a + b16496c commit a264cc9
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ type Runtime interface {
// called are undefined.
DefaultHTTPSHandler(http.Handler)

// TLSConfig returns the current tls.Config used with HTTPS servers
// If no tls.Config is set, it is created using the options provided in
// configuration. Modifications to the tls.Config the tls.Config are
// propagated to existing HTTPS servers.
//
// Results of modifying the tls.Config after Start() has been called are
// undefined.
TLSConfig() (*tls.Config, error)

// SetTLSConfig applies a given tls.Config to the runtime. It
// will be used with all HTTPS servers created after SetTLSConfig
// was called.
SetTLSConfig(*tls.Config)

// Start runs all registered servers and blocks until they terminate.
Start() error
}
Expand All @@ -91,11 +105,12 @@ type runtime struct {
*conf.ConfigFile
callbacks []callback
servers []*httputils.Server
tlsConfig *tls.Config
runFunc RunFunc
}

func newRuntime(name, version string, logger *log.Logger, configFile *conf.ConfigFile, runFunc RunFunc) *runtime {
return &runtime{name, version, logger, configFile, make([]callback, 0), nil, runFunc}
return &runtime{name, version, logger, configFile, make([]callback, 0), nil, nil, runFunc}
}

func (runtime *runtime) Callback(start startFunc, stop stopFunc) {
Expand All @@ -121,6 +136,18 @@ func (runtime *runtime) Run() (err error) {
return
}

func (runtime *runtime) TLSConfig() (*tls.Config, error) {
var err error
if runtime.tlsConfig == nil {
runtime.tlsConfig, err = runtime.loadTLSConfig("https")
}
return runtime.tlsConfig, err
}

func (runtime *runtime) SetTLSConfig(tlsConfig *tls.Config) {
runtime.tlsConfig = tlsConfig
}

func (runtime *runtime) Start() error {
if len(runtime.servers) == 0 {
return errors.New("No servers were registered")
Expand Down Expand Up @@ -246,6 +273,16 @@ func (runtime *runtime) DefaultHTTPSHandler(handler http.Handler) {
writetimeout = 10
}

if runtime.tlsConfig == nil {
runtime.tlsConfig, err = runtime.loadTLSConfig("https")
if err != nil {
runtime.OnStart(func(r Runtime) error {
return err
})
return
}
}

// Loop through each listen address, seperated by space
addresses := strings.Split(listen, " ")
for _, addr := range addresses {
Expand All @@ -261,16 +298,16 @@ func (runtime *runtime) DefaultHTTPSHandler(handler http.Handler) {
ReadTimeout: time.Duration(readtimeout) * time.Second,
WriteTimeout: time.Duration(writetimeout) * time.Second,
MaxHeaderBytes: 1 << 20,
TLSConfig: runtime.tlsConfig,
},
Logger: runtime.Logger,
}
runtime.servers = append(runtime.servers, server)

func(a string) {
runtime.OnStart(func(r Runtime) (err error) {
runtime.OnStart(func(r Runtime) error {
r.Printf("Starting HTTPS server on %s", a)
server.TLSConfig, err = runtime.loadTLSConfig("https")
return
return nil
})
}(addr)
}
Expand Down

0 comments on commit a264cc9

Please sign in to comment.