-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
169 lines (153 loc) · 3.77 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package adapter
import (
"context"
"encoding/hex"
"errors"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/gorilla/securecookie"
"github.com/shogo82148/server-starter/listener"
"github.com/sirupsen/logrus"
)
// these variable is set by goreleaser
var version = "main" // .Version
var commit = "HEAD" // .ShortCommit
// Main starts the go-nginx-oauth2-adapter server.
func Main(args []string) int {
flagSet := flag.NewFlagSet(args[0], flag.ContinueOnError)
var configFile string
var configtest bool
var showVersion bool
var showHelp bool
var genKey bool
flagSet.StringVar(&configFile, "c", "", "configuration file")
flagSet.StringVar(&configFile, "config", "", "configuration file")
flagSet.BoolVar(&configtest, "t", false, "test configuration and exit")
flagSet.BoolVar(&configtest, "configtest", false, "test configuration and exit")
flagSet.BoolVar(&showVersion, "v", false, "show version information")
flagSet.BoolVar(&showVersion, "version", false, "show version information")
flagSet.BoolVar(&showHelp, "h", false, "show help")
flagSet.BoolVar(&showHelp, "help", false, "show help")
flagSet.BoolVar(&genKey, "g", false, "shorthand of genkey")
flagSet.BoolVar(&genKey, "genkey", false, "generate random key for cookie")
err := flagSet.Parse(args[1:])
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
}).Error("error while parsing flags")
return 2
}
if showVersion {
fmt.Printf(
"go-nginx-oauth2-adapter version %s(rev %s) %s/%s (built by %s)\n",
version, commit, runtime.GOOS, runtime.GOARCH, runtime.Version(),
)
return 0
}
if showHelp {
flagSet.Usage()
return 0
}
if genKey {
fmt.Println(hex.EncodeToString(securecookie.GenerateRandomKey(64)))
fmt.Println(hex.EncodeToString(securecookie.GenerateRandomKey(32)))
return 0
}
c, err := parseConfig(configFile)
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
}).Error("error while parsing configure")
return 1
}
c.ConfigTest = configtest
s, err := NewServer(*c)
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
}).Error("init server error")
return 1
}
if configtest {
return 0
}
l, err := getListener(c)
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
}).Error("listen error")
return 1
}
server := &http.Server{
Handler: LoggingHandler(s),
}
go func() {
err := server.Serve(l)
if err == http.ErrServerClosed {
return
}
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
}).Fatal("serve error")
}
}()
waitSignal()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = server.Shutdown(ctx)
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
}).Error("shutdown server error")
}
return 0
}
func parseConfig(configFile string) (*Config, error) {
c := NewConfig()
if err := c.LoadEnv(); err != nil {
return nil, err
}
if configFile != "" {
if err := c.LoadYaml(configFile); err != nil {
return nil, err
}
}
return c, nil
}
func waitSignal() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM)
for {
s := <-signalChan
if s == syscall.SIGTERM {
logrus.Info("received SIGTERM")
return
}
}
}
func getListener(c *Config) (net.Listener, error) {
ports, err := listener.Ports()
if err != nil {
if errors.Is(err, listener.ErrNoListeningTarget) {
// Fallback if not running under Server::Starter
return net.Listen("tcp", c.Address)
}
return nil, err
}
listeners, err := ports.ListenAll(context.Background())
if err != nil {
return nil, err
}
if len(listeners) == 0 {
return nil, errors.New("running under Server::Starter but no port found")
}
return listeners[0], nil
}