servcvgen/handlers_config.go

82 lines
3.2 KiB
Go

package main
import (
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
utils "github.com/jesusperez/datautils"
"gopkg.in/yaml.v2"
)
func get_config_handle(c *gin.Context, rtenv *RouteEnv) {
idusr := rtenv.AuthMiddleware.IdentityHandler(c)
user, okusr := idusr.(*User)
if !okusr || len(user.UserName) == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"})
return
}
role := rtenv.Cfg.AdminRole
if _,res := user_has_role(user,c,rtenv,role); !res {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"})
return
}
path := fmt.Sprintf("%s_new.yaml",strings.Replace(rtenv.RunFlags.cfgPath,".yaml","",-1))
if utils.ExistsPath(path) {
config,err := loadConfig(path)
if err != nil {
logRoute(c,rtenv,"config",fmt.Sprintf("get /config error %v",err),fmt.Sprintf("get config_new %s (%s)",user.UserName,role))
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load config_new"})
return
}
logRoute(c,rtenv,"config","get /config",fmt.Sprintf("get config_new %s (%s)",user.UserName,role))
c.JSON(http.StatusOK, config)
} else {
logRoute(c,rtenv,"config","get /config",fmt.Sprintf("get config %s (%s)",user.UserName,role))
c.JSON(http.StatusOK, rtenv.Cfg)
}
}
func post_config_handle(c *gin.Context, rtenv *RouteEnv) {
var dataconfig ConfigPostData
role := rtenv.Cfg.AdminRole
err := c.BindJSON(&dataconfig)
if err != nil {
fmt.Printf("err: %+v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save config"})
return
}
if len(dataconfig.Id) == 0 || len(dataconfig.Config.Routes) == 0 {
c.JSON(http.StatusNotAcceptable, gin.H{"config": "error config"})
return
}
// roles,_ := enforcer.GetRolesForUser(user)
hasRole,_ := rtenv.Enforcer.HasRoleForUser(dataconfig.Id, role)
fmt.Printf("%s (%s) %+v\n",dataconfig.Id, role, hasRole)
if !hasRole {
logRoute(c,rtenv,"post_config",fmt.Sprintf("post %s: has no role",dataconfig.Id), fmt.Sprintf("post %s: %s",dataconfig.Id,dataconfig.Val))
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"})
return
}
data, err := yaml.Marshal(dataconfig.Config)
if err != nil {
logRoute(c,rtenv,"post_config",fmt.Sprintf("post %s: error %v ",dataconfig.Id,err), fmt.Sprintf("post %s: %s",dataconfig.Id,dataconfig.Val))
fmt.Printf("Error Parse dataconfig %s (%s): %v\n", dataconfig.Id, dataconfig.Val, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save config"})
return
}
path := fmt.Sprintf("%s.yaml",dataconfig.Val)
if path == rtenv.RunFlags.cfgPath {
path = fmt.Sprintf("%s_new.yaml",dataconfig.Val)
}
if res := utils.WriteData(string(data),path) ; res != nil {
logRoute(c,rtenv,"post_config",fmt.Sprintf("post %s: write error %v ",dataconfig.Id,err), fmt.Sprintf("post %s: %s",dataconfig.Id,dataconfig.Val))
fmt.Printf("Error write config from %s to %s: %v\n", dataconfig.Id, path, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save config"})
} else {
fmt.Printf("Config from %s writed to: %s\n", dataconfig.Id, path)
logRoute(c,rtenv,"post_config",fmt.Sprintf("post %s: %s",dataconfig.Id,dataconfig.Val), fmt.Sprintf("post %s: %s",dataconfig.Id,dataconfig.Val))
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
}