diff --git a/routes/config/config_endpoint.go b/routes/config/config_endpoint.go new file mode 100644 index 0000000..7186fdb --- /dev/null +++ b/routes/config/config_endpoint.go @@ -0,0 +1,87 @@ +/** Healthz package, endpoints. +* +* @author Steffen Vogel +* @copyright 2014-2021, 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 . +*********************************************************************************/ +package config + +import ( + "git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration" + "github.com/gin-gonic/gin" +) + +func RegisterConfigEndpoint(r *gin.RouterGroup) { + + r.GET("", getConfig) +} + +type ConfigAuthenticationExternal struct { + Enabled bool `json:"enabled"` + ProviderName string `json:"provider_name"` + AuthorizeURL string `json:"authorize_url"` +} + +type ConfigAuthentication struct { + External ConfigAuthenticationExternal `json:"external"` + LoginURL string `json:"login_url"` + LogoutURL string `json:"logout_url"` +} + +type ConfigContact struct { + Name string `json:"name"` + Mail string `json:"mail"` +} + +type Config struct { + Title string `json:"title"` + SubTitle string `json:"sub_title"` + Mode string `json:"mode"` + BaseHost string `json:"base_host"` + BasePath string `json:"base_path"` + Contact ConfigContact `json:"contact"` + Authentication ConfigAuthentication `json:"authentication"` +} + +// getHealth godoc +// @Summary Get config VILLASweb to be used by frontend +// @ID config +// @Produce json +// @Tags config +// @Success 200 {object} config.Config "The configuration" +// @Router /config [get] +func getConfig(c *gin.Context) { + + cfg := configuration.GlobalConfig + + resp := &Config{} + + resp.Mode, _ = cfg.String("mode") + resp.BaseHost, _ = cfg.String("base.host") + resp.BasePath, _ = cfg.String("base.path") + resp.Authentication.LogoutURL, _ = cfg.String("auth.logout-url") + resp.Authentication.External.Enabled, _ = cfg.Bool("auth.external") + resp.Authentication.External.AuthorizeURL, _ = cfg.String("auth.external-authorize-url") + resp.Authentication.External.ProviderName, _ = cfg.String("auth.external-provider-name") + resp.Title, _ = cfg.String("title") + resp.SubTitle, _ = cfg.String("sub-title") + resp.Contact.Name, _ = cfg.String("contact.name") + resp.Contact.Mail, _ = cfg.String("contact.mail") + + c.JSON(200, resp) +} diff --git a/routes/config/config_test.go b/routes/config/config_test.go new file mode 100644 index 0000000..580d2cf --- /dev/null +++ b/routes/config/config_test.go @@ -0,0 +1,47 @@ +/** Healthz package, testing. +* +* @author Steffen Vogel +* @copyright 2014-2021, 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 . +*********************************************************************************/ +package config + +import ( + "net/http" + "testing" + + "git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" +) + +var router *gin.Engine + +func TestConfig(t *testing.T) { + err := configuration.InitConfig() + assert.NoError(t, err) + + router = gin.Default() + + RegisterConfigEndpoint(router.Group("/config")) + + code, resp, err := helper.TestEndpoint(router, "", "config", http.MethodGet, nil) + assert.NoError(t, err) + assert.Equalf(t, 200, code, "Response body: \n%v\n", resp) +} diff --git a/routes/register.go b/routes/register.go index 4499691..1981e86 100644 --- a/routes/register.go +++ b/routes/register.go @@ -37,6 +37,7 @@ import ( "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" + config_route "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/config" "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" @@ -80,10 +81,11 @@ func RegisterEndpoints(router *gin.Engine, api *gin.RouterGroup) { healthz.RegisterHealthzEndpoint(api.Group("/healthz")) metrics.RegisterMetricsEndpoint(api.Group("/metrics")) openapi.RegisterOpenAPIEndpoint(api.Group("/openapi")) - // All endpoints (except for /healthz and /metrics) require authentication except when someone wants to - // login (POST /authenticate) + config_route.RegisterConfigEndpoint(api.Group("/config")) user.RegisterAuthenticate(api.Group("/authenticate")) + // The following endpoints require authentication + api.Use(user.Authentication()) scenario.RegisterScenarioEndpoints(api.Group("/scenarios"))