-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
86 lines (67 loc) · 2.24 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
package main
import (
"net/http"
"time"
"github.com/ARGOeu/argo-api-authn/version"
"github.com/gorilla/handlers"
"flag"
"crypto/tls"
"strconv"
"github.com/ARGOeu/argo-api-authn/auth"
"github.com/ARGOeu/argo-api-authn/config"
"github.com/ARGOeu/argo-api-authn/routing"
"github.com/ARGOeu/argo-api-authn/stores"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableColors: true})
// display binary version information during start up
version.LogInfo()
}
func main() {
// Retrieve configuration file location through cmd argument
var cfgPath = flag.String("config", "/etc/argo-api-authn/conf.d/argo-api-authn-config.json",
"Path for the required configuration file.")
flag.Parse()
// initialize the config
var cfg = config.WithDefaults()
if err := cfg.ConfigSetUp(*cfgPath); err != nil {
log.Error(err.Error())
panic(err.Error())
}
//configure datastore
store := &stores.MongoStoreWithOfficialDriver{
Server: cfg.MongoHost,
Database: cfg.MongoDB,
}
store.SetUp()
defer store.Close()
// configure the TLS config for the server
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
ClientAuth: cfg.ClientAuthPolicy(),
ClientCAs: auth.LoadCAs(cfg.CertificateAuthorities),
}
api := routing.NewRouting(routing.ApiRoutes, store, cfg)
xReqWithConType := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-AuthModel"})
allowVerbs := handlers.AllowedMethods([]string{"OPTIONS", "POST", "GET", "PUT", "DELETE", "HEAD"})
server := &http.Server{
Addr: ":" + strconv.Itoa(cfg.ServicePort),
Handler: handlers.CORS(xReqWithConType, allowVerbs)(api.Router),
TLSConfig: tlsConfig,
ReadTimeout: time.Duration(cfg.ServerReadTimeout) * time.Second,
ReadHeaderTimeout: time.Duration(cfg.ServerHeaderReadTimeout) * time.Second,
WriteTimeout: time.Duration(cfg.ServerWriteTimeout) * time.Second,
IdleTimeout: time.Duration(cfg.ServerIdleTimeout) * time.Second,
}
//Start the server
err := server.ListenAndServeTLS(cfg.Certificate, cfg.CertificateKey)
if err != nil {
log.WithFields(
log.Fields{
"type": "service_log",
"details": err.Error(),
},
).Fatal("Service failed to start")
}
}