add new config api endpoint

This commit is contained in:
Steffen Vogel 2021-02-04 19:09:44 +01:00
parent bdcff1cc8b
commit 828bf1c5bc
3 changed files with 138 additions and 2 deletions

View file

@ -0,0 +1,87 @@
/** Healthz package, endpoints.
*
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
* @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 <http://www.gnu.org/licenses/>.
*********************************************************************************/
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)
}

View file

@ -0,0 +1,47 @@
/** Healthz package, testing.
*
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
* @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 <http://www.gnu.org/licenses/>.
*********************************************************************************/
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)
}

View file

@ -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"))