@@ -72,6 +72,20 @@ type Runtime interface {
72
72
// called are undefined.
73
73
DefaultHTTPSHandler (http.Handler )
74
74
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
+
75
89
// Start runs all registered servers and blocks until they terminate.
76
90
Start () error
77
91
}
@@ -91,11 +105,12 @@ type runtime struct {
91
105
* conf.ConfigFile
92
106
callbacks []callback
93
107
servers []* httputils.Server
108
+ tlsConfig * tls.Config
94
109
runFunc RunFunc
95
110
}
96
111
97
112
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 }
99
114
}
100
115
101
116
func (runtime * runtime ) Callback (start startFunc , stop stopFunc ) {
@@ -121,6 +136,18 @@ func (runtime *runtime) Run() (err error) {
121
136
return
122
137
}
123
138
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
+
124
151
func (runtime * runtime ) Start () error {
125
152
if len (runtime .servers ) == 0 {
126
153
return errors .New ("No servers were registered" )
@@ -246,6 +273,16 @@ func (runtime *runtime) DefaultHTTPSHandler(handler http.Handler) {
246
273
writetimeout = 10
247
274
}
248
275
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
+
249
286
// Loop through each listen address, seperated by space
250
287
addresses := strings .Split (listen , " " )
251
288
for _ , addr := range addresses {
@@ -261,16 +298,16 @@ func (runtime *runtime) DefaultHTTPSHandler(handler http.Handler) {
261
298
ReadTimeout : time .Duration (readtimeout ) * time .Second ,
262
299
WriteTimeout : time .Duration (writetimeout ) * time .Second ,
263
300
MaxHeaderBytes : 1 << 20 ,
301
+ TLSConfig : runtime .tlsConfig ,
264
302
},
265
303
Logger : runtime .Logger ,
266
304
}
267
305
runtime .servers = append (runtime .servers , server )
268
306
269
307
func (a string ) {
270
- runtime .OnStart (func (r Runtime ) ( err error ) {
308
+ runtime .OnStart (func (r Runtime ) error {
271
309
r .Printf ("Starting HTTPS server on %s" , a )
272
- server .TLSConfig , err = runtime .loadTLSConfig ("https" )
273
- return
310
+ return nil
274
311
})
275
312
}(addr )
276
313
}
0 commit comments