This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (108 loc) · 2.71 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
package main
import (
"net/http"
"os"
"os/signal"
"syscall"
"time"
common "github.com/neicnordic/sda-common/database"
log "github.com/sirupsen/logrus"
)
// Export Conf so we can access it in the other modules
var Conf *Config
func main() {
sigc := make(chan os.Signal, 5)
signal.Notify(sigc, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
// Create a function to handle panic and exit gracefully
defer func() {
if err := recover(); err != nil {
log.Fatal("Could not recover, exiting")
}
}()
c, err := NewConfig()
if err != nil {
log.Error(err)
sigc <- syscall.SIGINT
panic(err)
}
Conf = c
tlsBroker, err := TLSConfigBroker(Conf)
if err != nil {
log.Error(err)
sigc <- syscall.SIGINT
panic(err)
}
tlsProxy, err := TLSConfigProxy(Conf)
if err != nil {
log.Error(err)
sigc <- syscall.SIGINT
panic(err)
}
sdaDB, err := common.NewSDAdb(Conf.DB)
if err != nil {
log.Error(err)
sigc <- syscall.SIGINT
panic(err)
}
if sdaDB.Version < 4 {
log.Error("database schema v4 is required")
sigc <- syscall.SIGINT
panic(err)
}
log.Debugf("Connected to sda-db (v%v)", sdaDB.Version)
err = checkS3Bucket(Conf.S3)
if err != nil {
log.Error(err)
sigc <- syscall.SIGINT
panic(err)
}
messenger, err := NewAMQPMessenger(Conf.Broker, tlsBroker)
if err != nil {
log.Error(err)
sigc <- syscall.SIGINT
panic(err)
}
log.Debug("messenger acquired ", messenger)
go func() {
<-sigc
sdaDB.Close()
messenger.channel.Close()
messenger.connection.Close()
os.Exit(1)
}()
var pubkeys map[string][]byte
auth := NewValidateFromToken(pubkeys)
auth.pubkeys = make(map[string][]byte)
// Load keys for JWT verification
if Conf.Server.jwtpubkeyurl != "" {
if err := auth.getjwtpubkey(Conf.Server.jwtpubkeyurl); err != nil {
log.Panicf("Error while getting key %s: %v", Conf.Server.jwtpubkeyurl, err)
}
}
if Conf.Server.jwtpubkeypath != "" {
if err := auth.getjwtkey(Conf.Server.jwtpubkeypath); err != nil {
log.Panicf("Error while getting key %s: %v", Conf.Server.jwtpubkeypath, err)
}
}
proxy := NewProxy(Conf.S3, auth, messenger, sdaDB, tlsProxy)
log.Debug("got the proxy ", proxy)
http.Handle("/", proxy)
hc := NewHealthCheck(8001, Conf.S3, Conf.Broker, tlsProxy)
go hc.RunHealthChecks()
server := &http.Server{
Addr: ":8000",
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 30 * time.Second,
}
if Conf.Server.cert != "" && Conf.Server.key != "" {
if err := server.ListenAndServeTLS(Conf.Server.cert, Conf.Server.key); err != nil {
panic(err)
}
} else {
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}
}