mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
241 lines
8.4 KiB
Go
241 lines
8.4 KiB
Go
/** Routes package, registration function
|
|
*
|
|
* @author Sonja Happ <sonja.happ@eonerc.rwth-aachen.de>
|
|
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
|
|
* @license GNU General Public License (version 3)
|
|
*
|
|
* VILLASweb-backend-go
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*********************************************************************************/
|
|
|
|
package routes
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
|
|
component_configuration "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/component-configuration"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/dashboard"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/file"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/healthz"
|
|
infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/metrics"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/result"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/signal"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/widget"
|
|
"github.com/gin-gonic/gin"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
"github.com/swaggo/gin-swagger/swaggerFiles"
|
|
"github.com/zpatrick/go-config"
|
|
)
|
|
|
|
// register all backend endpoints; to be called after DB is initialized
|
|
func RegisterEndpoints(router *gin.Engine, api *gin.RouterGroup) {
|
|
|
|
healthz.RegisterHealthzEndpoint(api.Group("/healthz"))
|
|
metrics.RegisterMetricsEndpoint(api.Group("/metrics"))
|
|
// All endpoints (except for /healthz and /metrics) require authentication except when someone wants to
|
|
// login (POST /authenticate)
|
|
user.RegisterAuthenticate(api.Group("/authenticate"))
|
|
|
|
api.Use(user.Authentication(true))
|
|
|
|
scenario.RegisterScenarioEndpoints(api.Group("/scenarios"))
|
|
component_configuration.RegisterComponentConfigurationEndpoints(api.Group("/configs"))
|
|
signal.RegisterSignalEndpoints(api.Group("/signals"))
|
|
dashboard.RegisterDashboardEndpoints(api.Group("/dashboards"))
|
|
widget.RegisterWidgetEndpoints(api.Group("/widgets"))
|
|
file.RegisterFileEndpoints(api.Group("/files"))
|
|
user.RegisterUserEndpoints(api.Group("/users"))
|
|
infrastructure_component.RegisterICEndpoints(api.Group("/ic"))
|
|
result.RegisterResultEndpoints(api.Group("/results"))
|
|
|
|
router.GET("swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
|
|
|
metrics.InitCounters()
|
|
|
|
}
|
|
|
|
// Uses API endpoints to add test data to the backend; All endpoints have to be registered before invoking this function.
|
|
func AddTestData(cfg *config.Config, router *gin.Engine) (*bytes.Buffer, error) {
|
|
|
|
basePath, err := cfg.String("base.path")
|
|
if err != nil {
|
|
fmt.Println("error: testdata could not be added to DB: no base path specified")
|
|
return nil, err
|
|
}
|
|
|
|
database.MigrateModels()
|
|
|
|
// add Admin user (defaults to User_0, xyz789)
|
|
err, adminpw := database.DBAddAdminUser(cfg)
|
|
|
|
var Admin = helper.Credentials{
|
|
Username: "admin",
|
|
Password: adminpw,
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// authenticate as admin
|
|
token, err := helper.AuthenticateForTest(router, basePath+"/authenticate", "POST", Admin)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// add users
|
|
for _, u := range helper.GlobalTestData.Users {
|
|
code, resp, err := helper.TestEndpoint(router, token, basePath+"/users", "POST", helper.KeyModels{"user": u})
|
|
if code != http.StatusOK {
|
|
return resp, fmt.Errorf("error adding user %v: %v", u.Username, err)
|
|
}
|
|
}
|
|
|
|
// add infrastructure components
|
|
amqphost, _ := cfg.String("amqp.host")
|
|
counterICs := 0
|
|
log.Println("ICS", helper.GlobalTestData.ICs)
|
|
for _, i := range helper.GlobalTestData.ICs {
|
|
|
|
if (i.ManagedExternally && amqphost != "") || !i.ManagedExternally {
|
|
code, resp, err := helper.TestEndpoint(router, token, basePath+"/ic", "POST", helper.KeyModels{"ic": i})
|
|
if code != http.StatusOK {
|
|
return resp, fmt.Errorf("error adding IC %v: %v", i.Name, err)
|
|
}
|
|
counterICs++
|
|
}
|
|
}
|
|
|
|
// add scenarios
|
|
for _, s := range helper.GlobalTestData.Scenarios {
|
|
code, resp, err := helper.TestEndpoint(router, token, basePath+"/scenarios", "POST", helper.KeyModels{"scenario": s})
|
|
if code != http.StatusOK {
|
|
return resp, fmt.Errorf("error adding Scenario %v: %v", s.Name, err)
|
|
}
|
|
|
|
// add all users to the scenario
|
|
for _, u := range helper.GlobalTestData.Users {
|
|
code, resp, err := helper.TestEndpoint(router, token, fmt.Sprintf("%v/scenarios/1/user?username="+u.Username, basePath), "PUT", nil)
|
|
if code != http.StatusOK {
|
|
return resp, fmt.Errorf("error adding user %v to scenario %v: %v", u.Username, s.Name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// If there is at least one scenario and one IC in the test data, add component configs
|
|
configCounter := 0
|
|
if len(helper.GlobalTestData.Scenarios) > 0 && counterICs > 0 {
|
|
|
|
for _, c := range helper.GlobalTestData.Configs {
|
|
c.ScenarioID = 1
|
|
c.ICID = 1
|
|
code, resp, err := helper.TestEndpoint(router, token, basePath+"/configs", "POST", helper.KeyModels{"config": c})
|
|
if code != http.StatusOK {
|
|
return resp, fmt.Errorf("error adding Config %v: %v", c.Name, err)
|
|
}
|
|
configCounter++
|
|
}
|
|
}
|
|
|
|
// If there is at least one scenario, add dashboards and 2 test files
|
|
dashboardCounter := 0
|
|
if len(helper.GlobalTestData.Scenarios) > 0 {
|
|
for _, d := range helper.GlobalTestData.Dashboards {
|
|
d.ScenarioID = 1
|
|
code, resp, err := helper.TestEndpoint(router, token, basePath+"/dashboards", "POST", helper.KeyModels{"dashboard": d})
|
|
if code != http.StatusOK {
|
|
return resp, fmt.Errorf("error adding Dashboard %v: %v", d.Name, err)
|
|
}
|
|
dashboardCounter++
|
|
}
|
|
|
|
// upload files
|
|
|
|
// upload readme file
|
|
bodyBuf := &bytes.Buffer{}
|
|
bodyWriter := multipart.NewWriter(bodyBuf)
|
|
fileWriter, _ := bodyWriter.CreateFormFile("file", "Readme.md")
|
|
fh, _ := os.Open("README.md")
|
|
defer fh.Close()
|
|
|
|
// io copy
|
|
_, err = io.Copy(fileWriter, fh)
|
|
contentType := bodyWriter.FormDataContentType()
|
|
bodyWriter.Close()
|
|
|
|
// Create the request and add file to scenario
|
|
w1 := httptest.NewRecorder()
|
|
req1, _ := http.NewRequest("POST", basePath+"/files?scenarioID=1", bodyBuf)
|
|
req1.Header.Set("Content-Type", contentType)
|
|
req1.Header.Add("Authorization", "Bearer "+token)
|
|
router.ServeHTTP(w1, req1)
|
|
|
|
// upload image file
|
|
bodyBuf = &bytes.Buffer{}
|
|
bodyWriter = multipart.NewWriter(bodyBuf)
|
|
fileWriter, _ = bodyWriter.CreateFormFile("file", "logo.png")
|
|
fh, _ = os.Open("doc/pictures/villas_web.png")
|
|
defer fh.Close()
|
|
|
|
// io copy
|
|
_, err = io.Copy(fileWriter, fh)
|
|
contentType = bodyWriter.FormDataContentType()
|
|
bodyWriter.Close()
|
|
|
|
// Create the request and add a second file to scenario
|
|
w2 := httptest.NewRecorder()
|
|
req2, _ := http.NewRequest("POST", basePath+"/files?scenarioID=1", bodyBuf)
|
|
req2.Header.Set("Content-Type", contentType)
|
|
req2.Header.Add("Authorization", "Bearer "+token)
|
|
router.ServeHTTP(w2, req2)
|
|
|
|
}
|
|
|
|
// If there is at least one dashboard, add widgets
|
|
if dashboardCounter > 0 {
|
|
for _, w := range helper.GlobalTestData.Widgets {
|
|
w.DashboardID = 1
|
|
code, resp, err := helper.TestEndpoint(router, token, basePath+"/widgets", "POST", helper.KeyModels{"widget": w})
|
|
if code != http.StatusOK {
|
|
return resp, fmt.Errorf("error adding Widget %v: %v", w.Name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// If there is at least one config, add signals
|
|
if configCounter > 0 {
|
|
for _, s := range helper.GlobalTestData.Signals {
|
|
s.ConfigID = 1
|
|
code, resp, err := helper.TestEndpoint(router, token, basePath+"/signals", "POST", helper.KeyModels{"signal": s})
|
|
if code != http.StatusOK {
|
|
return resp, fmt.Errorf("error adding Signal %v: %v", s.Name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, nil
|
|
}
|