NONRTRIC-946: Servicemanager - mock kong and capif as library
[nonrtric/plt/sme.git] / capifcore / cmd / main.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation. All rights reserved.
6 //   Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved.
7 //   %%
8 //   Licensed under the Apache License, Version 2.0 (the "License");
9 //   you may not use this file except in compliance with the License.
10 //   You may obtain a copy of the License at
11 //
12 //        http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //   Unless required by applicable law or agreed to in writing, software
15 //   distributed under the License is distributed on an "AS IS" BASIS,
16 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //   See the License for the specific language governing permissions and
18 //   limitations under the License.
19 //   ========================LICENSE_END===================================
20 //
21
22 package main
23
24 import (
25         "flag"
26         "fmt"
27         "net/http"
28
29         "github.com/labstack/echo/v4"
30         "helm.sh/helm/v3/pkg/cli"
31         log "github.com/sirupsen/logrus"
32
33         "oransc.org/nonrtric/capifcore/internal/helmmanagement"
34         config "oransc.org/nonrtric/capifcore/internal/config"
35         "oransc.org/nonrtric/capifcore/internal/keycloak"
36
37         "oransc.org/nonrtric/capifcore"
38 )
39
40 func main() {
41         var url string
42         var helmManager helmmanagement.HelmManager
43         var repoName string
44
45         var port = flag.Int("port", 8090, "Port for CAPIF Core Function HTTP server")
46         var secPort = flag.Int("secPort", 4433, "Port for CAPIF Core Function HTTPS server")
47         flag.StringVar(&url, "chartMuseumUrl", "", "ChartMuseum URL")
48         flag.StringVar(&repoName, "repoName", "capifcore", "Repository name")
49         var logLevelStr = flag.String("loglevel", "Info", "Log level")
50         var certPath = flag.String("certPath", "certs/cert.pem", "Path for server certificate")
51         var keyPath = flag.String("keyPath", "certs/key.pem", "Path for server private key")
52
53         flag.Parse()
54
55         if loglevel, err := log.ParseLevel(*logLevelStr); err == nil {
56                 log.SetLevel(loglevel)
57         }
58
59         // Add Helm repo
60         helmManager = helmmanagement.NewHelmManager(cli.New())
61         err := helmManager.SetUpRepo(repoName, url)
62         if err != nil {
63                 log.Warnf("No Helm repo added due to: %s", err.Error())
64         }
65
66         // Read configuration file
67         cfg, err := config.ReadKeycloakConfigFile("configs")
68         if err != nil {
69                 log.Fatalf("Error loading configuration file\n: %s", err)
70         }
71         km := keycloak.NewKeycloakManager(cfg, &http.Client{})
72
73         eWeb := echo.New()
74         capifcore.RegisterHandlers(eWeb, helmManager, km)
75         go startWebServer(eWeb, *port)
76
77         eHttpsWeb := echo.New()
78         capifcore.RegisterHandlers(eHttpsWeb, helmManager, km)
79         go startHttpsWebServer(eHttpsWeb, *secPort, *certPath, *keyPath)
80
81         log.Info("Server started and listening on port: ", *port)
82         keepServerAlive()
83 }
84
85 func startWebServer(e *echo.Echo, port int) {
86         e.Logger.Fatal(e.Start(fmt.Sprintf("0.0.0.0:%d", port)))
87 }
88
89 func startHttpsWebServer(e *echo.Echo, port int, certPath string, keyPath string) {
90         e.Logger.Fatal(e.StartTLS(fmt.Sprintf("0.0.0.0:%d", port), certPath, keyPath))
91 }
92
93 func keepServerAlive() {
94         forever := make(chan int)
95         <-forever
96 }