2 // ========================LICENSE_START=================================
5 // Copyright (C) 2023: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
30 log "github.com/sirupsen/logrus"
32 "github.com/labstack/echo/v4"
33 "oransc.org/nonrtric/capifinvoker/handler"
36 type TemplateRegistry struct {
37 templates map[string]*template.Template
40 func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
41 tmpl, ok := t.templates[name]
43 err := errors.New("Template not found -> " + name)
46 return tmpl.ExecuteTemplate(w, "base", data)
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")
61 if loglevel, err := log.ParseLevel(*logLevelStr); err == nil {
62 log.SetLevel(loglevel)
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"))
72 e.Renderer = &TemplateRegistry{
77 e.GET("/", handler.HomeHandler)
78 e.POST("/", handler.HomeHandler)
80 e.GET("/discovery", handler.DiscoverAPIs(capifCoreUrl))
82 e.GET("/onboardinvoker", handler.OnboardingInvokerHandler)
83 e.POST("/onboardinvoker", handler.OnboardInvoker(capifCoreUrl))
85 e.GET("/securitymethod", handler.SecurityMethodHandler)
86 e.POST("/securitymethod", handler.ObtainSecurityMethod(capifCoreUrl))
88 e.GET("/gettoken", handler.GetTokenHandler)
89 e.POST("/gettoken", handler.ObtainToken(capifCoreUrl))
91 // Start the web server
92 e.Logger.Fatal(e.Start(fmt.Sprintf("0.0.0.0:%d", *port)))