NONRTRIC-998: Servicemanager - Add API Docs
[nonrtric/plt/sme.git] / invoker / main.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package main
22
23 import (
24         "errors"
25         "flag"
26         "fmt"
27         "html/template"
28         "io"
29
30         log "github.com/sirupsen/logrus"
31
32         "github.com/labstack/echo/v4"
33         "oransc.org/nonrtric/capifinvoker/handler"
34 )
35
36 type TemplateRegistry struct {
37         templates map[string]*template.Template
38 }
39
40 func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
41         tmpl, ok := t.templates[name]
42         if !ok {
43                 err := errors.New("Template not found -> " + name)
44                 return err
45         }
46         return tmpl.ExecuteTemplate(w, "base", data)
47 }
48
49 func main() {
50
51         // Echo instance
52         e := echo.New()
53         e.Static("/", "view")
54         var capifCoreUrl string
55         flag.StringVar(&capifCoreUrl, "capifCoreUrl", "http://localhost:8090", "Url for CAPIF core")
56         var logLevelStr = flag.String("loglevel", "Info", "Log level")
57         var port = flag.Int("port", 9090, "Port for CAPIF Provider")
58
59         flag.Parse()
60
61         if loglevel, err := log.ParseLevel(*logLevelStr); err == nil {
62                 log.SetLevel(loglevel)
63         }
64
65         templates := make(map[string]*template.Template)
66         templates["home.html"] = template.Must(template.ParseFiles("view/home.html", "view/base.html"))
67         templates["discovery.html"] = template.Must(template.ParseFiles("view/discovery.html", "view/base.html"))
68         templates["onboardinvoker.html"] = template.Must(template.ParseFiles("view/onboardinvoker.html", "view/base.html"))
69         templates["securitymethod.html"] = template.Must(template.ParseFiles("view/securitymethod.html", "view/base.html"))
70         templates["gettoken.html"] = template.Must(template.ParseFiles("view/gettoken.html", "view/base.html"))
71
72         e.Renderer = &TemplateRegistry{
73                 templates: templates,
74         }
75
76         // Route => handler
77         e.GET("/", handler.HomeHandler)
78         e.POST("/", handler.HomeHandler)
79
80         e.GET("/discovery", handler.DiscoverAPIs(capifCoreUrl))
81
82         e.GET("/onboardinvoker", handler.OnboardingInvokerHandler)
83         e.POST("/onboardinvoker", handler.OnboardInvoker(capifCoreUrl))
84
85         e.GET("/securitymethod", handler.SecurityMethodHandler)
86         e.POST("/securitymethod", handler.ObtainSecurityMethod(capifCoreUrl))
87
88         e.GET("/gettoken", handler.GetTokenHandler)
89         e.POST("/gettoken", handler.ObtainToken(capifCoreUrl))
90
91         // Start the web server
92         e.Logger.Fatal(e.Start(fmt.Sprintf("0.0.0.0:%d", *port)))
93 }