//go:build plus package db import ( "encoding/json" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeCommon/pkg/langs/codes" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs" "github.com/iwind/TeaGo/actions" "strings" ) const clickhouseConfigCode = "clickhouseConfig" type ClickHouseAction struct { actionutils.ParentAction } func (this *ClickHouseAction) Init() { this.Nav("db", "db", "clickhouse") } func (this *ClickHouseAction) RunGet(params struct{}) { this.Data["mainTab"] = "clickhouse" resp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: clickhouseConfigCode}) if err != nil { this.ErrorPage(err) return } cfg := &systemconfigs.ClickHouseSetting{Port: 8123, Database: "default", Scheme: "http"} if len(resp.ValueJSON) > 0 { _ = json.Unmarshal(resp.ValueJSON, cfg) } if cfg.Port <= 0 { cfg.Port = 8123 } if cfg.Database == "" { cfg.Database = "default" } if strings.TrimSpace(cfg.Scheme) == "" { cfg.Scheme = "http" } this.Data["config"] = map[string]interface{}{ "host": cfg.Host, "port": cfg.Port, "user": cfg.User, "password": cfg.Password, "database": cfg.Database, "scheme": cfg.Scheme, "tlsSkipVerify": cfg.TLSSkipVerify, "tlsServerName": cfg.TLSServerName, } this.Show() } func (this *ClickHouseAction) RunPost(params struct { Host string Port int User string Password string Database string Scheme string TLSSkipVerify bool TLSServerName string Must *actions.Must }) { defer this.CreateLogInfo(codes.DBNode_LogUpdateDBNode, 0) if params.Port <= 0 { params.Port = 8123 } if params.Database == "" { params.Database = "default" } if params.Scheme != "https" { params.Scheme = "http" } password := params.Password if password == "" { resp, _ := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: clickhouseConfigCode}) if len(resp.ValueJSON) > 0 { var old systemconfigs.ClickHouseSetting if json.Unmarshal(resp.ValueJSON, &old) == nil { password = old.Password } } } cfg := &systemconfigs.ClickHouseSetting{ Host: params.Host, Port: params.Port, User: params.User, Password: password, Database: params.Database, Scheme: params.Scheme, TLSSkipVerify: params.TLSSkipVerify, TLSServerName: strings.TrimSpace(params.TLSServerName), } valueJSON, err := json.Marshal(cfg) if err != nil { this.ErrorPage(err) return } _, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{ Code: clickhouseConfigCode, ValueJSON: valueJSON, }) if err != nil { this.ErrorPage(err) return } this.Success() }