forked from qvest-digital/loginsrv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
74 lines (58 loc) · 1.5 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
package main
import (
_ "github.com/tarent/loginsrv/htpasswd"
_ "github.com/tarent/loginsrv/httpupstream"
_ "github.com/tarent/loginsrv/osiam"
"github.com/tarent/loginsrv/login"
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/tarent/loginsrv/logging"
)
const applicationName = "loginsrv"
func main() {
config := login.ReadConfig()
if err := logging.Set(config.LogLevel, config.TextLogging); err != nil {
exit(nil, err)
}
logging.AccessLogCookiesBlacklist = append(logging.AccessLogCookiesBlacklist, config.CookieName)
configToLog := *config
configToLog.JwtSecret = "..."
logging.LifecycleStart(applicationName, configToLog)
h, err := login.NewHandler(config)
if err != nil {
exit(nil, err)
}
handlerChain := logging.NewLogMiddleware(h)
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
port := config.Port
if port != "" {
port = fmt.Sprintf(":%s", port)
}
httpSrv := &http.Server{Addr: port, Handler: handlerChain}
go func() {
if err := httpSrv.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
logging.ServerClosed(applicationName)
} else {
exit(nil, err)
}
}
}()
logging.LifecycleStop(applicationName, <-stop, nil)
ctx, ctxCancel := context.WithTimeout(context.Background(), config.GracePeriod)
httpSrv.Shutdown(ctx)
ctxCancel()
}
var exit = func(signal os.Signal, err error) {
logging.LifecycleStop(applicationName, signal, err)
if err == nil {
os.Exit(0)
} else {
os.Exit(1)
}
}