-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
105 lines (89 loc) · 2.95 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/madappgang/identifo/v2/config"
"github.com/madappgang/identifo/v2/localization"
"github.com/madappgang/identifo/v2/model"
)
func main() {
// load default translations
localization.LoadDefaultCatalog()
// load config
configFlag := flag.String("config", "", "The location of a server configuration file (local file, s3 or etcd)")
flag.Parse()
done := make(chan bool)
restart := make(chan bool)
defer close(done)
defer close(restart)
srv, httpSrv, err := initServer(*configFlag, restart)
if err != nil {
log.Fatalf("Unable to start Identifo with error: %v ", err)
}
go startHTTPServer(httpSrv)
log.Printf("Started the server on port: %s", srv.Settings().GetPort())
log.Printf("You can open admin panel: %s/adminpanel or http://localhost:%s/adminpanel", srv.Settings().General.Host, srv.Settings().GetPort())
watcher, err := config.NewConfigWatcher(srv.Settings().Config)
if err != nil {
log.Fatalf("Unable to start Identifo with error: %v ", err)
}
watcher.Watch()
osch := make(chan os.Signal, 1)
signal.Notify(osch, syscall.SIGINT, syscall.SIGTERM)
restartServer := func() {
ctx, _ := context.WithTimeout(context.Background(), time.Minute*3)
httpSrv.Shutdown(ctx)
srv.Close() // TODO: implement graceful server shutdown
srv, httpSrv, err = initServer(*configFlag, restart)
if err != nil {
log.Fatalf("Unable to start Identifo with error: %v ", err)
}
go startHTTPServer(httpSrv)
log.Printf("Started the server on port: %s", srv.Settings().GetPort())
log.Printf("You can open admin panel: %s/adminpanel or http://localhost:%s/adminpanel", srv.Settings().General.Host, srv.Settings().GetPort())
log.Println("Server successfully restarted with new settings ...")
}
for {
select {
case <-watcher.WatchChan():
log.Println("Config file has been changed, restarting ...")
restartServer()
case <-restart:
log.Println("Restart signal have been received, restarting ...")
restartServer()
case err := <-watcher.ErrorChan():
log.Printf("Getting error from config watcher: %v", err)
case <-osch:
log.Println("Received termination signal, shutting down the server ⤵️...")
ctx, _ := context.WithTimeout(context.Background(), time.Minute*3)
httpSrv.Shutdown(ctx)
srv.Close()
log.Println("The server is down, good bye 👋👋👋.")
return
}
}
}
func initServer(flag string, restartChan chan<- bool) (model.Server, *http.Server, error) {
srv, err := config.NewServerFromFlag(flag, restartChan)
if err != nil {
return nil, nil, fmt.Errorf("Unable to start Identifo with error: %v ", err)
}
httpSrv := &http.Server{
Addr: srv.Settings().GetPort(),
Handler: srv.Router(),
}
return srv, httpSrv, nil
}
func startHTTPServer(server *http.Server) {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("ListenAndServe() error: %v", err)
}
log.Print("Server stopped")
}