-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
102 lines (81 loc) · 1.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"syscall"
"github.com/itzg/go-flagsfiller"
"github.com/itzg/saml-auth-proxy/server"
"github.com/itzg/zapconfigs"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
var (
version = "dev"
commit = "HEAD"
)
func main() {
var serverConfig server.Config
filler := flagsfiller.New(flagsfiller.WithEnv("SamlProxy"))
err := filler.Fill(flag.CommandLine, &serverConfig)
if err != nil {
log.Fatal(err)
}
flag.Parse()
if serverConfig.Version {
fmt.Printf("%s %s (%s)\n", os.Args[0], version, commit)
os.Exit(0)
}
var logger *zap.Logger
if serverConfig.Debug {
logger = zapconfigs.NewDebugLogger()
} else {
logger = zapconfigs.NewDefaultLogger()
}
defer logger.Sync()
checkRequired(serverConfig.BaseUrl, "base-url")
checkRequired(serverConfig.BackendUrl, "backend-url")
checkRequired(serverConfig.IdpMetadataUrl, "idp-metadata-url")
ctx, cancel := context.WithCancel(context.Background())
go func() {
c := make(chan os.Signal, 1) // we need to reserve to buffer size 1, so the notifier are not blocked
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
cancel()
}()
var bindType, bind = httpBinding(serverConfig.Bind)
listener, err := net.Listen(bindType, bind)
if err != nil {
log.Fatal(err)
}
g, gCtx := errgroup.WithContext(ctx)
g.Go(func() error {
return server.Start(ctx, listener, logger, &serverConfig)
})
g.Go(func() error {
<-gCtx.Done()
return listener.Close()
})
if err := g.Wait(); err != nil {
fmt.Printf("exit reason: %s \n", err)
}
}
func checkRequired(value string, name string) {
if value == "" {
_, _ = fmt.Fprintf(os.Stderr, "%s is required\n", name)
flag.Usage()
os.Exit(2)
}
}
func httpBinding(bind string) (string, string) {
if strings.HasPrefix(bind, "unix:") {
return "unix", strings.TrimLeft(bind, "unix:")
} else {
return "tcp", bind
}
}