Move controller module into separate package
[multicloud/k8s.git] / src / orchestrator / cmd / main.go
1 /*
2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package main
15
16 import (
17         "context"
18         "log"
19         "math/rand"
20         "net/http"
21         "os"
22         "os/signal"
23         "time"
24
25         "github.com/gorilla/handlers"
26         "github.com/onap/multicloud-k8s/src/orchestrator/api"
27         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/auth"
28         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/config"
29         contextDb "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb"
30         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
31         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/rpc"
32         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/controller"
33 )
34
35 func main() {
36
37         rand.Seed(time.Now().UnixNano())
38
39         err := db.InitializeDatabaseConnection("mco")
40         if err != nil {
41                 log.Println("Unable to initialize database connection...")
42                 log.Println(err)
43                 log.Fatalln("Exiting...")
44         }
45         err = contextDb.InitializeContextDatabase()
46         if err != nil {
47                 log.Println("Unable to initialize database connection...")
48                 log.Println(err)
49                 log.Fatalln("Exiting...")
50         }
51
52         httpRouter := api.NewRouter(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
53         loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter)
54         log.Println("Starting Kubernetes Multicloud API")
55
56         httpServer := &http.Server{
57                 Handler: loggedRouter,
58                 Addr:    ":" + config.GetConfiguration().ServicePort,
59         }
60
61         controller.NewControllerClient().InitControllers()
62
63         connectionsClose := make(chan struct{})
64         go func() {
65                 c := make(chan os.Signal, 1)
66                 signal.Notify(c, os.Interrupt)
67                 <-c
68                 httpServer.Shutdown(context.Background())
69                 rpc.CloseAllRpcConn()
70                 close(connectionsClose)
71         }()
72
73         tlsConfig, err := auth.GetTLSConfig("ca.cert", "server.cert", "server.key")
74         if err != nil {
75                 log.Println("Error Getting TLS Configuration. Starting without TLS...")
76                 log.Fatal(httpServer.ListenAndServe())
77         } else {
78                 httpServer.TLSConfig = tlsConfig
79                 // empty strings because tlsconfig already has this information
80                 err = httpServer.ListenAndServeTLS("", "")
81         }
82 }