17
17
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18
18
*/
19
19
20
- package main
20
+ package program
21
21
22
22
import (
23
23
"context"
@@ -40,6 +40,12 @@ import (
40
40
"golang.org/x/time/rate"
41
41
)
42
42
43
+ type Extension interface {
44
+ ParametersOfInterest () []string
45
+ Start (parameters map [string ]interface {}, w * windermere.Windermere )
46
+ Stop ()
47
+ }
48
+
43
49
// Windermere can be run in interactive mode as a regular executable,
44
50
// or as a service. The code below is used when using the program as
45
51
// a service.
@@ -59,10 +65,12 @@ type serviceInterface struct {
59
65
// When running as a service, the process will send to this channel to signal
60
66
// when proper shutdown is complete.
61
67
done chan bool
68
+
69
+ extension Extension
62
70
}
63
71
64
72
func (si serviceInterface ) Start (s service.Service ) error {
65
- go run (si .signals , si .done )
73
+ go run (si .signals , si .done , si . extension )
66
74
return nil
67
75
}
68
76
@@ -179,11 +187,21 @@ func parseClients(value interface{}) (map[string]string, error) {
179
187
return res , nil
180
188
}
181
189
190
+ func getViperParameters (parameters []string ) map [string ]interface {} {
191
+ result := make (map [string ]interface {})
192
+ for _ , p := range parameters {
193
+ if viper .IsSet (p ) {
194
+ result [p ] = viper .Get (p )
195
+ }
196
+ }
197
+ return result
198
+ }
199
+
182
200
// This is like the programs core main function. The actual main()
183
201
// will take care of parsing arguments and behaves a bit differently
184
202
// depending on whether we're running interactively, as a service,
185
203
// or if we're installing/uninstalling a service.
186
- func run (signals chan os.Signal , done chan bool ) {
204
+ func run (signals chan os.Signal , done chan bool , extension Extension ) {
187
205
certFile := viper .GetString (CNFCert )
188
206
keyFile := viper .GetString (CNFKey )
189
207
@@ -341,6 +359,11 @@ func run(signals chan os.Signal, done chan bool) {
341
359
}()
342
360
}
343
361
362
+ if extension != nil {
363
+ extensionParameters := getViperParameters (extension .ParametersOfInterest ())
364
+ extension .Start (extensionParameters , wind )
365
+ }
366
+
344
367
waitForShutdownSignal (signals )
345
368
346
369
log .Printf ("Shutting down, waiting for active requests to finish..." )
@@ -359,6 +382,10 @@ func run(signals chan os.Signal, done chan bool) {
359
382
}
360
383
}
361
384
385
+ if extension != nil {
386
+ extension .Stop ()
387
+ }
388
+
362
389
err = wind .Shutdown ()
363
390
if err != nil {
364
391
log .Printf ("Failed to gracefully shutdown Windermere: %v" , err )
@@ -376,7 +403,7 @@ func run(signals chan os.Signal, done chan bool) {
376
403
}
377
404
}
378
405
379
- func main ( ) {
406
+ func Main ( ext Extension ) {
380
407
// Configuration defaults
381
408
defaults := map [string ]interface {}{
382
409
CNFMDURL : "https://fed.skolfederation.se/prod/md/kontosynk.jws" ,
@@ -462,7 +489,7 @@ func main() {
462
489
UserName : * serviceUser ,
463
490
Option : opts ,
464
491
}
465
- si := & serviceInterface {signals : sigs , done : make (chan bool )}
492
+ si := & serviceInterface {signals : sigs , done : make (chan bool ), extension : ext }
466
493
s , err := service .New (si , serviceConfig )
467
494
if err != nil {
468
495
log .Fatalf ("Cannot create the service: %s" , err .Error ())
@@ -485,7 +512,7 @@ func main() {
485
512
} else {
486
513
// Don't use the service, just run directly so Ctrl-C works
487
514
signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM )
488
- run (sigs , nil )
515
+ run (sigs , nil , ext )
489
516
}
490
517
} else {
491
518
err = s .Run ()
0 commit comments