Skip to content

Commit a264cc9

Browse files
committed
Merge pull request #4 from longsleep/tlsconfig
tls.Config fixes
2 parents d640b0a + b16496c commit a264cc9

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

runtime.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ type Runtime interface {
7272
// called are undefined.
7373
DefaultHTTPSHandler(http.Handler)
7474

75+
// TLSConfig returns the current tls.Config used with HTTPS servers
76+
// If no tls.Config is set, it is created using the options provided in
77+
// configuration. Modifications to the tls.Config the tls.Config are
78+
// propagated to existing HTTPS servers.
79+
//
80+
// Results of modifying the tls.Config after Start() has been called are
81+
// undefined.
82+
TLSConfig() (*tls.Config, error)
83+
84+
// SetTLSConfig applies a given tls.Config to the runtime. It
85+
// will be used with all HTTPS servers created after SetTLSConfig
86+
// was called.
87+
SetTLSConfig(*tls.Config)
88+
7589
// Start runs all registered servers and blocks until they terminate.
7690
Start() error
7791
}
@@ -91,11 +105,12 @@ type runtime struct {
91105
*conf.ConfigFile
92106
callbacks []callback
93107
servers []*httputils.Server
108+
tlsConfig *tls.Config
94109
runFunc RunFunc
95110
}
96111

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

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

139+
func (runtime *runtime) TLSConfig() (*tls.Config, error) {
140+
var err error
141+
if runtime.tlsConfig == nil {
142+
runtime.tlsConfig, err = runtime.loadTLSConfig("https")
143+
}
144+
return runtime.tlsConfig, err
145+
}
146+
147+
func (runtime *runtime) SetTLSConfig(tlsConfig *tls.Config) {
148+
runtime.tlsConfig = tlsConfig
149+
}
150+
124151
func (runtime *runtime) Start() error {
125152
if len(runtime.servers) == 0 {
126153
return errors.New("No servers were registered")
@@ -246,6 +273,16 @@ func (runtime *runtime) DefaultHTTPSHandler(handler http.Handler) {
246273
writetimeout = 10
247274
}
248275

276+
if runtime.tlsConfig == nil {
277+
runtime.tlsConfig, err = runtime.loadTLSConfig("https")
278+
if err != nil {
279+
runtime.OnStart(func(r Runtime) error {
280+
return err
281+
})
282+
return
283+
}
284+
}
285+
249286
// Loop through each listen address, seperated by space
250287
addresses := strings.Split(listen, " ")
251288
for _, addr := range addresses {
@@ -261,16 +298,16 @@ func (runtime *runtime) DefaultHTTPSHandler(handler http.Handler) {
261298
ReadTimeout: time.Duration(readtimeout) * time.Second,
262299
WriteTimeout: time.Duration(writetimeout) * time.Second,
263300
MaxHeaderBytes: 1 << 20,
301+
TLSConfig: runtime.tlsConfig,
264302
},
265303
Logger: runtime.Logger,
266304
}
267305
runtime.servers = append(runtime.servers, server)
268306

269307
func(a string) {
270-
runtime.OnStart(func(r Runtime) (err error) {
308+
runtime.OnStart(func(r Runtime) error {
271309
r.Printf("Starting HTTPS server on %s", a)
272-
server.TLSConfig, err = runtime.loadTLSConfig("https")
273-
return
310+
return nil
274311
})
275312
}(addr)
276313
}

0 commit comments

Comments
 (0)