| // - |
| // ========================LICENSE_START================================= |
| // O-RAN-SC |
| // %% |
| // Copyright (C) 2022: Nordix Foundation |
| // %% |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| // ========================LICENSE_END=================================== |
| // |
| |
| package main |
| |
| import ( |
| "flag" |
| "fmt" |
| "io/ioutil" |
| "net/http" |
| "os" |
| "path/filepath" |
| |
| "github.com/deepmap/oapi-codegen/pkg/middleware" |
| "github.com/labstack/echo/v4" |
| echomiddleware "github.com/labstack/echo/v4/middleware" |
| "github.com/pkg/errors" |
| log "github.com/sirupsen/logrus" |
| "gopkg.in/yaml.v2" |
| "helm.sh/helm/v3/pkg/cli" |
| "helm.sh/helm/v3/pkg/getter" |
| "helm.sh/helm/v3/pkg/repo" |
| "oransc.org/nonrtric/plt/capif/internal/discoverservice" |
| "oransc.org/nonrtric/plt/capif/internal/helmmanagement" |
| "oransc.org/nonrtric/plt/capif/internal/invokermanagement" |
| "oransc.org/nonrtric/plt/capif/internal/providermanagement" |
| "oransc.org/nonrtric/plt/capif/internal/publishservice" |
| "oransc.org/nonrtric/plt/capif/internal/readonly/discoverserviceapi" |
| "oransc.org/nonrtric/plt/capif/internal/readonly/invokermanagementapi" |
| "oransc.org/nonrtric/plt/capif/internal/readonly/providermanagementapi" |
| "oransc.org/nonrtric/plt/capif/internal/readonly/publishserviceapi" |
| ) |
| |
| var settings *cli.EnvSettings |
| var url string |
| var repoName string |
| |
| func main() { |
| var port = flag.Int("port", 8090, "Port for CAPIF Core Function HTTP server") |
| flag.StringVar(&url, "url", "http://chartmuseum:8080", "ChartMuseum url") |
| flag.StringVar(&repoName, "repoName", "local-dev", "Repository name") |
| flag.Parse() |
| settings = cli.New() |
| |
| // Add repo |
| fmt.Printf("Adding %s to Helm Repo\n", url) |
| _, err := addToRepo() |
| if err != nil { |
| log.Fatal(err.Error()) |
| } |
| |
| // This is how you set up a basic Echo router |
| e := echo.New() |
| // Log all requests |
| e.Use(echomiddleware.Logger()) |
| |
| // Register InvokerManagement |
| invokerManagerSwagger, err := invokermanagementapi.GetSwagger() |
| if err != nil { |
| log.Fatalf("Error loading InvokerManagement swagger spec\n: %s", err) |
| } |
| invokerManagerSwagger.Servers = nil |
| invokerManager := invokermanagement.NewInvokerManager() |
| g := e.Group("/api-invoker-management/v1") |
| g.Use(middleware.OapiRequestValidator(invokerManagerSwagger)) |
| invokermanagementapi.RegisterHandlersWithBaseURL(e, invokerManager, "/api-invoker-management/v1") |
| |
| // Register ProviderManagement |
| providerManagerSwagger, err := providermanagementapi.GetSwagger() |
| if err != nil { |
| log.Fatalf("Error loading ProviderManagement swagger spec\n: %s", err) |
| } |
| providerManagerSwagger.Servers = nil |
| providerManager := providermanagement.NewProviderManager(helmmanagement.NewHelmManager(settings)) |
| g = e.Group("/api-provider-management/v1") |
| g.Use(middleware.OapiRequestValidator(providerManagerSwagger)) |
| providermanagementapi.RegisterHandlersWithBaseURL(e, providerManager, "/api-provider-management/v1") |
| |
| // Register PublishService |
| publishServiceSwagger, err := publishserviceapi.GetSwagger() |
| if err != nil { |
| log.Fatalf("Error loading PublishService swagger spec\n: %s", err) |
| } |
| publishServiceSwagger.Servers = nil |
| publishService := publishservice.NewPublishService() |
| g = e.Group("/published-apis/v1") |
| g.Use(middleware.OapiRequestValidator(publishServiceSwagger)) |
| publishserviceapi.RegisterHandlersWithBaseURL(e, publishService, "/published-apis/v1") |
| |
| // Register DiscoverService |
| discoverServiceSwagger, err := discoverserviceapi.GetSwagger() |
| if err != nil { |
| log.Fatalf("Error loading DiscoverService swagger spec\n: %s", err) |
| } |
| discoverServiceSwagger.Servers = nil |
| discoverService := discoverservice.NewDiscoverService() |
| g = e.Group("/service-apis/v1") |
| g.Use(middleware.OapiRequestValidator(discoverServiceSwagger)) |
| discoverserviceapi.RegisterHandlersWithBaseURL(e, discoverService, "/service-apis/v1") |
| |
| go startWebServer(e, *port) |
| |
| log.Info("Server started and listening on port: ", *port) |
| |
| keepServerAlive() |
| } |
| |
| func addToRepo() (string, error) { |
| repoFile := settings.RepositoryConfig |
| |
| //Ensure the file directory exists as it is required for file locking |
| err := os.MkdirAll(filepath.Dir(repoFile), os.ModePerm) |
| if err != nil && !os.IsExist(err) { |
| return "", err |
| } |
| |
| b, err := ioutil.ReadFile(repoFile) |
| if err != nil && !os.IsNotExist(err) { |
| return "", err |
| } |
| |
| var f repo.File |
| if err := yaml.Unmarshal(b, &f); err != nil { |
| return "", err |
| } |
| |
| if f.Has(repoName) { |
| fmt.Printf("repository name (%s) already exists\n", repoName) |
| return "", nil |
| } |
| |
| c := repo.Entry{ |
| Name: repoName, |
| URL: url, |
| } |
| |
| r, err := repo.NewChartRepository(&c, getter.All(settings)) |
| if err != nil { |
| return "", err |
| } |
| |
| if _, err := r.DownloadIndexFile(); err != nil { |
| err := errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", url) |
| return "", err |
| } |
| |
| f.Update(&c) |
| |
| if err := f.WriteFile(repoFile, 0644); err != nil { |
| return "", err |
| } |
| fmt.Printf("%q has been added to your repositories\n", repoName) |
| return "", nil |
| } |
| |
| func startWebServer(e *echo.Echo, port int) { |
| e.GET("/", hello) |
| |
| e.Logger.Fatal(e.Start(fmt.Sprintf("0.0.0.0:%d", port))) |
| } |
| |
| func keepServerAlive() { |
| forever := make(chan int) |
| <-forever |
| } |
| |
| func hello(c echo.Context) error { |
| return c.String(http.StatusOK, "Hello, World!\n") |
| } |