Skip to content

Commit 876e72c

Browse files
committed
Re-usable and extendable main package.
Fixes #39
1 parent 2b9f118 commit 876e72c

9 files changed

+69
-12
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Releases
22

3+
## v1.2.1 (2024-09-19)
4+
- Re-usable and extendable main package (#39)
5+
36
## v1.2.0 (2024-09-16)
47
#### Features
58
- Windermere can run as a Windows service (#18)

Diff for: cmd/windermere/start.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of Windermere (EGIL SCIM Server).
3+
*
4+
* Copyright (C) 2019-2024 Föreningen Sambruk
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package main
20+
21+
import (
22+
"github.com/Sambruk/windermere/program"
23+
)
24+
25+
func main() {
26+
program.Main(nil)
27+
}

Diff for: cmd/windermere/accesslog.go renamed to program/accesslog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
package main
20+
package program
2121

2222
import (
2323
"log"

Diff for: cmd/windermere/apikeyauth.go renamed to program/apikeyauth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package program
22

33
import (
44
"context"

Diff for: cmd/windermere/limiter.go renamed to program/limiter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package program
22

33
import (
44
"net/http"

Diff for: cmd/windermere/main.go renamed to program/main.go

+33-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
package main
20+
package program
2121

2222
import (
2323
"context"
@@ -40,6 +40,12 @@ import (
4040
"golang.org/x/time/rate"
4141
)
4242

43+
type Extension interface {
44+
ParametersOfInterest() []string
45+
Start(parameters map[string]interface{}, w *windermere.Windermere)
46+
Stop()
47+
}
48+
4349
// Windermere can be run in interactive mode as a regular executable,
4450
// or as a service. The code below is used when using the program as
4551
// a service.
@@ -59,10 +65,12 @@ type serviceInterface struct {
5965
// When running as a service, the process will send to this channel to signal
6066
// when proper shutdown is complete.
6167
done chan bool
68+
69+
extension Extension
6270
}
6371

6472
func (si serviceInterface) Start(s service.Service) error {
65-
go run(si.signals, si.done)
73+
go run(si.signals, si.done, si.extension)
6674
return nil
6775
}
6876

@@ -179,11 +187,21 @@ func parseClients(value interface{}) (map[string]string, error) {
179187
return res, nil
180188
}
181189

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+
182200
// This is like the programs core main function. The actual main()
183201
// will take care of parsing arguments and behaves a bit differently
184202
// depending on whether we're running interactively, as a service,
185203
// 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) {
187205
certFile := viper.GetString(CNFCert)
188206
keyFile := viper.GetString(CNFKey)
189207

@@ -341,6 +359,11 @@ func run(signals chan os.Signal, done chan bool) {
341359
}()
342360
}
343361

362+
if extension != nil {
363+
extensionParameters := getViperParameters(extension.ParametersOfInterest())
364+
extension.Start(extensionParameters, wind)
365+
}
366+
344367
waitForShutdownSignal(signals)
345368

346369
log.Printf("Shutting down, waiting for active requests to finish...")
@@ -359,6 +382,10 @@ func run(signals chan os.Signal, done chan bool) {
359382
}
360383
}
361384

385+
if extension != nil {
386+
extension.Stop()
387+
}
388+
362389
err = wind.Shutdown()
363390
if err != nil {
364391
log.Printf("Failed to gracefully shutdown Windermere: %v", err)
@@ -376,7 +403,7 @@ func run(signals chan os.Signal, done chan bool) {
376403
}
377404
}
378405

379-
func main() {
406+
func Main(ext Extension) {
380407
// Configuration defaults
381408
defaults := map[string]interface{}{
382409
CNFMDURL: "https://fed.skolfederation.se/prod/md/kontosynk.jws",
@@ -462,7 +489,7 @@ func main() {
462489
UserName: *serviceUser,
463490
Option: opts,
464491
}
465-
si := &serviceInterface{signals: sigs, done: make(chan bool)}
492+
si := &serviceInterface{signals: sigs, done: make(chan bool), extension: ext}
466493
s, err := service.New(si, serviceConfig)
467494
if err != nil {
468495
log.Fatalf("Cannot create the service: %s", err.Error())
@@ -485,7 +512,7 @@ func main() {
485512
} else {
486513
// Don't use the service, just run directly so Ctrl-C works
487514
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
488-
run(sigs, nil)
515+
run(sigs, nil, ext)
489516
}
490517
} else {
491518
err = s.Run()

Diff for: cmd/windermere/metadata.go renamed to program/metadata.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
package main
20+
package program
2121

2222
import (
2323
"crypto/x509"

Diff for: cmd/windermere/panicreporttimeouthandler.go renamed to program/panicreporttimeouthandler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package program
22

33
import (
44
"log"

Diff for: cmd/windermere/parseclients_test.go renamed to program/parseclients_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package program
22

33
import (
44
"strings"

0 commit comments

Comments
 (0)