Add the rest of the apis to the diagrams
[nonrtric/plt/sme.git] / capifcore / main.go
index 7a3f96f..ddba0dd 100644 (file)
@@ -25,8 +25,10 @@ import (
        "fmt"
        "net/http"
 
+       "github.com/getkin/kin-openapi/openapi3"
        "github.com/labstack/echo/v4"
        "helm.sh/helm/v3/pkg/cli"
+       "oransc.org/nonrtric/capifcore/internal/common29122"
        "oransc.org/nonrtric/capifcore/internal/discoverserviceapi"
        "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
        "oransc.org/nonrtric/capifcore/internal/providermanagementapi"
@@ -50,7 +52,7 @@ var repoName string
 
 func main() {
        var port = flag.Int("port", 8090, "Port for CAPIF Core Function HTTP server")
-       flag.StringVar(&url, "url", "http://localhost:8080", "ChartMuseum url")
+       flag.StringVar(&url, "chartMuseumUrl", "", "ChartMuseum URL")
        flag.StringVar(&repoName, "repoName", "capifcore", "Repository name")
        var logLevelStr = flag.String("loglevel", "Info", "Log level")
        flag.Parse()
@@ -60,11 +62,10 @@ func main() {
        }
 
        // Add repo
-       fmt.Printf("Adding %s to Helm Repo\n", url)
        helmManager = helmmanagement.NewHelmManager(cli.New())
        err := helmManager.SetUpRepo(repoName, url)
        if err != nil {
-               log.Fatal(err.Error())
+               log.Warnf("No Helm repo added due to: %s", err.Error())
        }
 
        go startWebServer(getEcho(), *port)
@@ -126,7 +127,7 @@ func getEcho() *echo.Echo {
        discoverserviceapi.RegisterHandlersWithBaseURL(e, discoverService, "/service-apis/v1")
 
        // Register Security
-       securitySwagger, err := publishserviceapi.GetSwagger()
+       securitySwagger, err := securityapi.GetSwagger()
        if err != nil {
                log.Fatalf("Error loading Security swagger spec\n: %s", err)
        }
@@ -138,6 +139,8 @@ func getEcho() *echo.Echo {
 
        e.GET("/", hello)
 
+       e.GET("/swagger/:apiName", getSwagger)
+
        return e
 }
 
@@ -153,3 +156,33 @@ func keepServerAlive() {
 func hello(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!\n")
 }
+
+func getSwagger(c echo.Context) error {
+       var swagger *openapi3.T
+       var err error
+       switch api := c.Param("apiName"); api {
+       case "provider":
+               swagger, err = providermanagementapi.GetSwagger()
+       case "publish":
+               swagger, err = publishserviceapi.GetSwagger()
+       case "invoker":
+               swagger, err = invokermanagementapi.GetSwagger()
+       case "discover":
+               swagger, err = discoverserviceapi.GetSwagger()
+       case "security":
+               swagger, err = securityapi.GetSwagger()
+       default:
+               return c.JSON(http.StatusBadRequest, getProblemDetails("Invalid API name "+api, http.StatusBadRequest))
+       }
+       if err != nil {
+               return c.JSON(http.StatusInternalServerError, getProblemDetails("Unable to get swagger for API", http.StatusInternalServerError))
+       }
+       return c.JSON(http.StatusOK, swagger)
+}
+
+func getProblemDetails(cause string, status int) common29122.ProblemDetails {
+       return common29122.ProblemDetails{
+               Cause:  &cause,
+               Status: &status,
+       }
+}