blob: b7661ea0216b772f0e49a0cad13d5ebdf3bf75c9 [file] [log] [blame]
ychaconb8732552023-04-26 09:11:45 +02001// -
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
21package main
22
23import (
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/capifprov/handler"
34)
35
36type TemplateRegistry struct {
37 templates map[string]*template.Template
38}
39
40func (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
49func 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["registration.html"] = template.Must(template.ParseFiles("view/registration.html", "view/base.html"))
68 templates["publishapi.html"] = template.Must(template.ParseFiles("view/publishapi.html", "view/base.html"))
69 templates["getapi.html"] = template.Must(template.ParseFiles("view/getapi.html", "view/base.html"))
70
71 e.Renderer = &TemplateRegistry{
72 templates: templates,
73 }
74
75 // Route => handler
76 e.GET("/", handler.HomeHandler)
77 e.POST("/", handler.HomeHandler)
78
79 e.GET("/registration", handler.RegistrationHandler)
80 e.POST("/registration", handler.RegistrationFormHandler(capifCoreUrl))
81
82 e.GET("/publishapi", handler.PublishapiHandler)
83 e.POST("/publishapi", handler.PublishApiFormHandler(capifCoreUrl))
84
85 e.GET("/getapi", handler.GetApiRequest(capifCoreUrl))
86
87 // Start the web server
88 e.Logger.Fatal(e.Start(fmt.Sprintf("0.0.0.0:%d", *port)))
89}