Remove BPA operator
[icn.git] / cmd / bpa-restapi-agent / main.go
1 // main.go
2 package main
3
4 import (
5   "context"
6   "log"
7   "math/rand"
8   "net/http"
9   "os"
10   "os/signal"
11   "time"
12
13
14   "github.com/gorilla/handlers"
15
16   "bpa-restapi-agent/api"
17   utils "bpa-restapi-agent/internal"
18   "bpa-restapi-agent/internal/auth"
19   "bpa-restapi-agent/internal/config"
20 )
21
22 func main() {
23   // check initial config
24   err := utils.CheckInitialSettings()
25   if err != nil{
26     log.Fatal(err)
27   }
28
29   rand.Seed(time.Now().UnixNano())
30
31   httpRouter := api.NewRouter(nil, nil, nil)
32   // Return http.handler and log requests to Stdout
33   loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter)
34   log.Println("Starting Integrated Cloud Native API")
35
36   // Create custom http server
37   httpServer := &http.Server{
38     Handler: loggedRouter,
39     Addr:    ":" + config.GetConfiguration().ServicePort,
40   }
41   connectionsClose := make(chan struct{})
42   go func() {
43     c := make(chan os.Signal, 1) // create c channel to receive notifications
44     signal.Notify(c, os.Interrupt) // register c channel to run concurrently
45     <-c
46     httpServer.Shutdown(context.Background())
47     close(connectionsClose)
48   }()
49
50   // Start server
51   tlsConfig, err := auth.GetTLSConfig("ca.cert", "server.cert", "server.key")
52   if err != nil {
53     log.Println("Error Getting TLS Configuration. Starting without TLS...")
54     log.Fatal(httpServer.ListenAndServe())
55   } else {
56     httpServer.TLSConfig = tlsConfig
57     // empty strings because tlsconfig already has this information
58     err = httpServer.ListenAndServeTLS("", "")
59   }
60 }