diff --git a/internal/errors/error.go b/internal/errors/error.go new file mode 100644 index 00000000..e44a22f6 --- /dev/null +++ b/internal/errors/error.go @@ -0,0 +1,56 @@ +package errors + +import ( + "errors" + "path/filepath" + "runtime" + "strconv" +) + +type errorObj struct { + err error + file string + line int + funcName string +} + +func (this *errorObj) Error() string { + s := this.err.Error() + "\n " + this.file + if len(this.funcName) > 0 { + s += ":" + this.funcName + "()" + } + s += ":" + strconv.Itoa(this.line) + return s +} + +// 新错误 +func New(errText string) error { + ptr, file, line, ok := runtime.Caller(1) + funcName := "" + if ok { + frame, _ := runtime.CallersFrames([]uintptr{ptr}).Next() + funcName = filepath.Base(frame.Function) + } + return &errorObj{ + err: errors.New(errText), + file: file, + line: line, + funcName: funcName, + } +} + +// 包装已有错误 +func Wrap(err error) error { + ptr, file, line, ok := runtime.Caller(1) + funcName := "" + if ok { + frame, _ := runtime.CallersFrames([]uintptr{ptr}).Next() + funcName = filepath.Base(frame.Function) + } + return &errorObj{ + err: err, + file: file, + line: line, + funcName: funcName, + } +} diff --git a/internal/errors/error_test.go b/internal/errors/error_test.go new file mode 100644 index 00000000..b288d27c --- /dev/null +++ b/internal/errors/error_test.go @@ -0,0 +1,22 @@ +package errors + +import ( + "errors" + "testing" +) + +func TestNew(t *testing.T) { + t.Log(New("hello")) + t.Log(Wrap(errors.New("hello"))) + t.Log(testError1()) + t.Log(Wrap(testError1())) + t.Log(Wrap(testError2())) +} + +func testError1() error { + return New("test error1") +} + +func testError2() error { + return Wrap(testError1()) +} diff --git a/internal/rpc/rpc_client.go b/internal/rpc/rpc_client.go index d3384ac2..468b9122 100644 --- a/internal/rpc/rpc_client.go +++ b/internal/rpc/rpc_client.go @@ -134,6 +134,10 @@ func (this *RPCClient) SSLPolicyRPC() pb.SSLPolicyServiceClient { return pb.NewSSLPolicyServiceClient(this.pickConn()) } +func (this *RPCClient) SysSettingRPC() pb.SysSettingServiceClient { + return pb.NewSysSettingServiceClient(this.pickConn()) +} + // 构造上下文 func (this *RPCClient) Context(adminId int64) context.Context { ctx := context.Background() diff --git a/internal/web/actions/default/clusters/clusterutils/cluster_helper.go b/internal/web/actions/default/clusters/clusterutils/cluster_helper.go index a1520313..a73fb104 100644 --- a/internal/web/actions/default/clusters/clusterutils/cluster_helper.go +++ b/internal/web/actions/default/clusters/clusterutils/cluster_helper.go @@ -2,8 +2,8 @@ package clusters import ( "github.com/TeaOSLab/EdgeAdmin/internal/rpc" - "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/maps" @@ -48,9 +48,13 @@ func (this *ClusterHelper) BeforeAction(action *actions.ActionObject) { } tabbar := actionutils.NewTabbar() - tabbar.Add("当前集群:"+cluster.Name, "", "/clusters", "left long alternate arrow", false) + tabbar.Add("集群列表", "", "/clusters", "", false) tabbar.Add("节点", "", "/clusters/cluster?clusterId="+clusterIdString, "server", selectedTabbar == "node") tabbar.Add("设置", "", "/clusters/cluster/settings?clusterId="+clusterIdString, "setting", selectedTabbar == "setting") + { + m := tabbar.Add("当前集群:"+cluster.Name, "", "/clusters/cluster?clusterId="+clusterIdString, "", false) + m["right"] = true + } actionutils.SetTabbar(action, tabbar) // 左侧菜单 diff --git a/internal/web/actions/default/servers/components/cache/clean.go b/internal/web/actions/default/servers/components/cache/clean.go new file mode 100644 index 00000000..7ced0c35 --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/clean.go @@ -0,0 +1,15 @@ +package cache + +import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + +type CleanAction struct { + actionutils.ParentAction +} + +func (this *CleanAction) Init() { + this.Nav("", "", "") +} + +func (this *CleanAction) RunGet(params struct{}) { + this.Show() +} diff --git a/internal/web/actions/default/servers/components/cache/createPopup.go b/internal/web/actions/default/servers/components/cache/createPopup.go new file mode 100644 index 00000000..e26e10f5 --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/createPopup.go @@ -0,0 +1,82 @@ +package cache + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/iwind/TeaGo/actions" +) + +type CreatePopupAction struct { + actionutils.ParentAction +} + +func (this *CreatePopupAction) Init() { + this.Nav("", "", "") +} + +func (this *CreatePopupAction) RunGet(params struct{}) { + this.Data["types"] = serverconfigs.AllCachePolicyTypes + this.Show() +} + +func (this *CreatePopupAction) RunPost(params struct { + Name string + Type string + + // file + FileDir string + + CapacityJSON []byte + MaxSizeJSON []byte + MaxKeys int64 + + Description string + IsOn bool + + Must *actions.Must +}) { + params.Must. + Field("name", params.Name). + Require("请输入策略名称") + + // 校验选项 + var options interface{} + switch params.Type { + case serverconfigs.CachePolicyTypeFile: + params.Must. + Field("fileDir", params.FileDir). + Require("请输入缓存目录") + options = &serverconfigs.HTTPFileCacheConfig{ + Dir: params.FileDir, + } + case serverconfigs.CachePolicyTypeMemory: + options = &serverconfigs.HTTPMemoryCacheConfig{ + } + default: + this.Fail("请选择正确的缓存类型") + } + + optionsJSON, err := json.Marshal(options) + if err != nil { + this.ErrorPage(err) + return + } + _, err = this.RPC().HTTPCachePolicyRPC().CreateHTTPCachePolicy(this.AdminContext(), &pb.CreateHTTPCachePolicyRequest{ + IsOn: params.IsOn, + Name: params.Name, + Description: params.Description, + CapacityJSON: params.CapacityJSON, + MaxKeys: params.MaxKeys, + MaxSizeJSON: params.MaxSizeJSON, + Type: params.Type, + OptionsJSON: optionsJSON, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/cache/delete.go b/internal/web/actions/default/servers/components/cache/delete.go new file mode 100644 index 00000000..ed03b696 --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/delete.go @@ -0,0 +1,32 @@ +package cache + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" +) + +type DeleteAction struct { + actionutils.ParentAction +} + +func (this *DeleteAction) RunPost(params struct { + CachePolicyId int64 +}) { + // 检查是否被引用 + countResp, err := this.RPC().ServerRPC().CountAllEnabledServersWithCachePolicyId(this.AdminContext(), &pb.CountAllEnabledServersWithCachePolicyIdRequest{CachePolicyId: params.CachePolicyId}) + if err != nil { + this.ErrorPage(err) + return + } + if countResp.Count > 0 { + this.Fail("此缓存策略正在被别的服务引用,请修改后再删除。") + } + + _, err = this.RPC().HTTPCachePolicyRPC().DeleteHTTPCachePolicy(this.AdminContext(), &pb.DeleteHTTPCachePolicyRequest{CachePolicyId: params.CachePolicyId}) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/cache/index.go b/internal/web/actions/default/servers/components/cache/index.go index 5871f782..4fb286b4 100644 --- a/internal/web/actions/default/servers/components/cache/index.go +++ b/internal/web/actions/default/servers/components/cache/index.go @@ -1,7 +1,11 @@ package cache import ( + "encoding/json" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/iwind/TeaGo/maps" ) type IndexAction struct { @@ -13,6 +17,47 @@ func (this *IndexAction) Init() { } func (this *IndexAction) RunGet(params struct{}) { + countResp, err := this.RPC().HTTPCachePolicyRPC().CountAllEnabledHTTPCachePolicies(this.AdminContext(), &pb.CountAllEnabledHTTPCachePoliciesRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + count := countResp.Count + page := this.NewPage(count) + this.Data["page"] = page.AsHTML() + + listResp, err := this.RPC().HTTPCachePolicyRPC().ListEnabledHTTPCachePolicies(this.AdminContext(), &pb.ListEnabledHTTPCachePoliciesRequest{ + Offset: page.Offset, + Size: page.Size, + }) + if err != nil { + this.ErrorPage(err) + return + } + cachePoliciesJSON := listResp.CachePoliciesJSON + cachePolicies := []*serverconfigs.HTTPCachePolicy{} + err = json.Unmarshal(cachePoliciesJSON, &cachePolicies) + if err != nil { + this.ErrorPage(err) + return + } + this.Data["cachePolicies"] = cachePolicies + + infos := []maps.Map{} + for _, cachePolicy := range cachePolicies { + countServersResp, err := this.RPC().ServerRPC().CountAllEnabledServersWithCachePolicyId(this.AdminContext(), &pb.CountAllEnabledServersWithCachePolicyIdRequest{CachePolicyId: cachePolicy.Id}) + if err != nil { + this.ErrorPage(err) + return + } + countServers := countServersResp.Count + + infos = append(infos, maps.Map{ + "typeName": serverconfigs.FindCachePolicyTypeName(cachePolicy.Type), + "countServers": countServers, + }) + } + this.Data["infos"] = infos this.Show() } diff --git a/internal/web/actions/default/servers/components/cache/init.go b/internal/web/actions/default/servers/components/cache/init.go index b98e04f8..042ebf56 100644 --- a/internal/web/actions/default/servers/components/cache/init.go +++ b/internal/web/actions/default/servers/components/cache/init.go @@ -14,6 +14,15 @@ func init() { Helper(componentutils.NewComponentHelper()). Prefix("/servers/components/cache"). Get("", new(IndexAction)). + GetPost("/createPopup", new(CreatePopupAction)). + Get("/policy", new(PolicyAction)). + GetPost("/updatePopup", new(UpdatePopupAction)). + GetPost("/clean", new(CleanAction)). + GetPost("/preheat", new(PreheatAction)). + GetPost("/purge", new(PurgeAction)). + GetPost("/stat", new(StatAction)). + GetPost("/test", new(TestAction)). + Post("/delete", new(DeleteAction)). EndAll() }) } diff --git a/internal/web/actions/default/servers/components/cache/policy.go b/internal/web/actions/default/servers/components/cache/policy.go new file mode 100644 index 00000000..3dc4c3ca --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/policy.go @@ -0,0 +1,15 @@ +package cache + +import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + +type PolicyAction struct { + actionutils.ParentAction +} + +func (this *PolicyAction) Init() { + this.Nav("", "", "") +} + +func (this *PolicyAction) RunGet(params struct{}) { + this.Show() +} diff --git a/internal/web/actions/default/servers/components/cache/preheat.go b/internal/web/actions/default/servers/components/cache/preheat.go new file mode 100644 index 00000000..73123fab --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/preheat.go @@ -0,0 +1,15 @@ +package cache + +import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + +type PreheatAction struct { + actionutils.ParentAction +} + +func (this *PreheatAction) Init() { + this.Nav("", "", "") +} + +func (this *PreheatAction) RunGet(params struct{}) { + this.Show() +} diff --git a/internal/web/actions/default/servers/components/cache/purge.go b/internal/web/actions/default/servers/components/cache/purge.go new file mode 100644 index 00000000..fe2030a1 --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/purge.go @@ -0,0 +1,15 @@ +package cache + +import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + +type PurgeAction struct { + actionutils.ParentAction +} + +func (this *PurgeAction) Init() { + this.Nav("", "", "") +} + +func (this *PurgeAction) RunGet(params struct{}) { + this.Show() +} diff --git a/internal/web/actions/default/servers/components/cache/stat.go b/internal/web/actions/default/servers/components/cache/stat.go new file mode 100644 index 00000000..4dfacade --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/stat.go @@ -0,0 +1,15 @@ +package cache + +import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + +type StatAction struct { + actionutils.ParentAction +} + +func (this *StatAction) Init() { + this.Nav("", "", "") +} + +func (this *StatAction) RunGet(params struct{}) { + this.Show() +} diff --git a/internal/web/actions/default/servers/components/cache/test.go b/internal/web/actions/default/servers/components/cache/test.go new file mode 100644 index 00000000..aefc1a64 --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/test.go @@ -0,0 +1,15 @@ +package cache + +import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + +type TestAction struct { + actionutils.ParentAction +} + +func (this *TestAction) Init() { + this.Nav("", "", "") +} + +func (this *TestAction) RunGet(params struct{}) { + this.Show() +} diff --git a/internal/web/actions/default/servers/components/cache/updatePopup.go b/internal/web/actions/default/servers/components/cache/updatePopup.go new file mode 100644 index 00000000..1c7ee465 --- /dev/null +++ b/internal/web/actions/default/servers/components/cache/updatePopup.go @@ -0,0 +1,108 @@ +package cache + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/iwind/TeaGo/actions" +) + +type UpdatePopupAction struct { + actionutils.ParentAction +} + +func (this *UpdatePopupAction) Init() { + this.Nav("", "", "") +} + +func (this *UpdatePopupAction) RunGet(params struct { + CachePolicyId int64 +}) { + configResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: params.CachePolicyId}) + if err != nil { + this.ErrorPage(err) + return + } + configJSON := configResp.CachePolicyJSON + if len(configJSON) == 0 { + this.NotFound("cachePolicy", params.CachePolicyId) + return + } + + cachePolicy := &serverconfigs.HTTPCachePolicy{} + err = json.Unmarshal(configJSON, cachePolicy) + if err != nil { + this.ErrorPage(err) + return + } + this.Data["cachePolicy"] = cachePolicy + + // 其他选项 + this.Data["types"] = serverconfigs.AllCachePolicyTypes + + this.Show() +} + +func (this *UpdatePopupAction) RunPost(params struct { + CachePolicyId int64 + + Name string + Type string + + // file + FileDir string + + CapacityJSON []byte + MaxSizeJSON []byte + MaxKeys int64 + + Description string + IsOn bool + + Must *actions.Must +}) { + params.Must. + Field("name", params.Name). + Require("请输入策略名称") + + // 校验选项 + var options interface{} + switch params.Type { + case serverconfigs.CachePolicyTypeFile: + params.Must. + Field("fileDir", params.FileDir). + Require("请输入缓存目录") + options = &serverconfigs.HTTPFileCacheConfig{ + Dir: params.FileDir, + } + case serverconfigs.CachePolicyTypeMemory: + options = &serverconfigs.HTTPMemoryCacheConfig{ + } + default: + this.Fail("请选择正确的缓存类型") + } + + optionsJSON, err := json.Marshal(options) + if err != nil { + this.ErrorPage(err) + return + } + _, err = this.RPC().HTTPCachePolicyRPC().UpdateHTTPCachePolicy(this.AdminContext(), &pb.UpdateHTTPCachePolicyRequest{ + CachePolicyId: params.CachePolicyId, + IsOn: params.IsOn, + Name: params.Name, + Description: params.Description, + CapacityJSON: params.CapacityJSON, + MaxKeys: params.MaxKeys, + MaxSizeJSON: params.MaxSizeJSON, + Type: params.Type, + OptionsJSON: optionsJSON, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/componentutils/component_helper.go b/internal/web/actions/default/servers/components/componentutils/component_helper.go index 2fe7a902..483d0155 100644 --- a/internal/web/actions/default/servers/components/componentutils/component_helper.go +++ b/internal/web/actions/default/servers/components/componentutils/component_helper.go @@ -25,7 +25,7 @@ func (this *ComponentHelper) BeforeAction(action *actions.ActionObject) { selectedTabbar := action.Data.GetString("mainTab") tabbar := actionutils.NewTabbar() tabbar.Add("服务", "", "/servers", "", selectedTabbar == "server") - tabbar.Add("组件", "", "/servers/components", "", selectedTabbar == "component") + tabbar.Add("通用", "", "/servers/components", "", selectedTabbar == "component") actionutils.SetTabbar(action, tabbar) // 创建左侧菜单 @@ -39,11 +39,11 @@ func (this *ComponentHelper) createLeftMenus(secondMenuItem string) (items []map "url": "/servers/components", "isActive": secondMenuItem == "global", }) - items = append(items, maps.Map{ + /**items = append(items, maps.Map{ "name": "分组设置", "url": "/servers/components/group", "isActive": secondMenuItem == "group", - }) + })**/ items = append(items, maps.Map{ "name": "缓存策略", "url": "/servers/components/cache", diff --git a/internal/web/actions/default/servers/components/helper.go b/internal/web/actions/default/servers/components/helper.go index ba3852e1..d0c0f003 100644 --- a/internal/web/actions/default/servers/components/helper.go +++ b/internal/web/actions/default/servers/components/helper.go @@ -19,6 +19,6 @@ func (this *Helper) BeforeAction(action *actions.ActionObject) { tabbar := actionutils.NewTabbar() tabbar.Add("服务", "", "/servers", "", selectedTabbar == "server") - tabbar.Add("组件", "", "/servers/components", "", selectedTabbar == "component") + tabbar.Add("通用", "", "/servers/components", "", selectedTabbar == "component") actionutils.SetTabbar(action, tabbar) } diff --git a/internal/web/actions/default/servers/components/index.go b/internal/web/actions/default/servers/components/index.go index fc548342..ffcf9e45 100644 --- a/internal/web/actions/default/servers/components/index.go +++ b/internal/web/actions/default/servers/components/index.go @@ -1,7 +1,15 @@ package components import ( + "encoding/json" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/iwind/TeaGo/actions" +) + +const ( + SettingCodeGlobalConfig = "globalConfig" ) type IndexAction struct { @@ -14,6 +22,48 @@ func (this *IndexAction) Init() { } func (this *IndexAction) RunGet(params struct{}) { + valueJSONResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: SettingCodeGlobalConfig}) + if err != nil { + this.ErrorPage(err) + return + } + valueJSON := valueJSONResp.ValueJSON + globalConfig := &serverconfigs.GlobalConfig{} + if len(valueJSON) > 0 { + err = json.Unmarshal(valueJSON, globalConfig) + if err != nil { + this.ErrorPage(err) + return + } + } + this.Data["globalConfig"] = globalConfig this.Show() } + +func (this *IndexAction) RunPost(params struct { + GlobalConfigJSON []byte + Must *actions.Must +}) { + if len(params.GlobalConfigJSON) == 0 { + this.Fail("错误的配置信息,请刷新当前页面后重试") + } + + globalConfig := &serverconfigs.GlobalConfig{} + err := json.Unmarshal(params.GlobalConfigJSON, globalConfig) + if err != nil { + this.Fail("配置校验失败:" + err.Error()) + } + + // 修改配置 + _, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{ + Code: SettingCodeGlobalConfig, + ValueJSON: params.GlobalConfigJSON, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/init.go b/internal/web/actions/default/servers/components/init.go index b36dd24b..92201799 100644 --- a/internal/web/actions/default/servers/components/init.go +++ b/internal/web/actions/default/servers/components/init.go @@ -13,7 +13,7 @@ func init() { Helper(NewHelper()). Helper(componentutils.NewComponentHelper()). Prefix("/servers/components"). - Get("", new(IndexAction)). + GetPost("", new(IndexAction)). EndAll() }) } diff --git a/internal/web/actions/default/servers/components/ssl/certPopup.go b/internal/web/actions/default/servers/components/ssl/certPopup.go index 88f6fef0..3ecbc855 100644 --- a/internal/web/actions/default/servers/components/ssl/certPopup.go +++ b/internal/web/actions/default/servers/components/ssl/certPopup.go @@ -57,7 +57,7 @@ func (this *CertPopupAction) RunGet(params struct { } // 引入的服务 - serversResp, err := this.RPC().ServerRPC().FindAllServersWithSSLCertId(this.AdminContext(), &pb.FindAllServersWithSSLCertIdRequest{CertId: params.CertId}) + serversResp, err := this.RPC().ServerRPC().FindAllEnabledServersWithSSLCertId(this.AdminContext(), &pb.FindAllEnabledServersWithSSLCertIdRequest{CertId: params.CertId}) if err != nil { this.ErrorPage(err) return diff --git a/internal/web/actions/default/servers/components/ssl/delete.go b/internal/web/actions/default/servers/components/ssl/delete.go index e9e1d4ab..f2c36508 100644 --- a/internal/web/actions/default/servers/components/ssl/delete.go +++ b/internal/web/actions/default/servers/components/ssl/delete.go @@ -13,7 +13,7 @@ func (this *DeleteAction) RunPost(params struct { CertId int64 }) { // 是否正在被使用 - countResp, err := this.RPC().ServerRPC().CountServersWithSSLCertId(this.AdminContext(), &pb.CountServersWithSSLCertIdRequest{CertId: params.CertId}) + countResp, err := this.RPC().ServerRPC().CountAllEnabledServersWithSSLCertId(this.AdminContext(), &pb.CountAllEnabledServersWithSSLCertIdRequest{CertId: params.CertId}) if err != nil { this.ErrorPage(err) return diff --git a/internal/web/actions/default/servers/components/ssl/index.go b/internal/web/actions/default/servers/components/ssl/index.go index c6b69bbc..13a32c2a 100644 --- a/internal/web/actions/default/servers/components/ssl/index.go +++ b/internal/web/actions/default/servers/components/ssl/index.go @@ -141,13 +141,14 @@ func (this *IndexAction) RunGet(params struct { certMaps := []maps.Map{} nowTime := time.Now().Unix() for _, certConfig := range certConfigs { - countServersResp, err := this.RPC().ServerRPC().CountServersWithSSLCertId(this.AdminContext(), &pb.CountServersWithSSLCertIdRequest{CertId: certConfig.Id}) + countServersResp, err := this.RPC().ServerRPC().CountAllEnabledServersWithSSLCertId(this.AdminContext(), &pb.CountAllEnabledServersWithSSLCertIdRequest{CertId: certConfig.Id}) if err != nil { this.ErrorPage(err) return } certMaps = append(certMaps, maps.Map{ + "isOn": certConfig.IsOn, "beginDay": timeutil.FormatTime("Y-m-d", certConfig.TimeBeginAt), "endDay": timeutil.FormatTime("Y-m-d", certConfig.TimeEndAt), "isExpired": nowTime > certConfig.TimeEndAt, diff --git a/internal/web/actions/default/servers/components/ssl/selectPopup.go b/internal/web/actions/default/servers/components/ssl/selectPopup.go index bc6eb470..67268d08 100644 --- a/internal/web/actions/default/servers/components/ssl/selectPopup.go +++ b/internal/web/actions/default/servers/components/ssl/selectPopup.go @@ -48,7 +48,7 @@ func (this *SelectPopupAction) RunGet(params struct{}) { certMaps := []maps.Map{} nowTime := time.Now().Unix() for _, certConfig := range certConfigs { - countServersResp, err := this.RPC().ServerRPC().CountServersWithSSLCertId(this.AdminContext(), &pb.CountServersWithSSLCertIdRequest{CertId: certConfig.Id}) + countServersResp, err := this.RPC().ServerRPC().CountAllEnabledServersWithSSLCertId(this.AdminContext(), &pb.CountAllEnabledServersWithSSLCertIdRequest{CertId: certConfig.Id}) if err != nil { this.ErrorPage(err) return diff --git a/internal/web/actions/default/servers/components/ssl/updatePopup.go b/internal/web/actions/default/servers/components/ssl/updatePopup.go index 39a4246e..33288fd6 100644 --- a/internal/web/actions/default/servers/components/ssl/updatePopup.go +++ b/internal/web/actions/default/servers/components/ssl/updatePopup.go @@ -1,6 +1,12 @@ package ssl -import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs" + "github.com/iwind/TeaGo/actions" +) type UpdatePopupAction struct { actionutils.ParentAction @@ -10,6 +16,114 @@ func (this *UpdatePopupAction) Init() { this.Nav("", "", "") } -func (this *UpdatePopupAction) RunGet(params struct{}) { +func (this *UpdatePopupAction) RunGet(params struct { + CertId int64 +}) { + certConfigResp, err := this.RPC().SSLCertRPC().FindEnabledSSLCertConfig(this.AdminContext(), &pb.FindEnabledSSLCertConfigRequest{CertId: params.CertId}) + if err != nil { + this.ErrorPage(err) + return + } + certConfigJSON := certConfigResp.CertJSON + if len(certConfigJSON) == 0 { + this.NotFound("cert", params.CertId) + return + } + + certConfig := &sslconfigs.SSLCertConfig{} + err = json.Unmarshal(certConfigJSON, certConfig) + if err != nil { + this.ErrorPage(err) + return + } + this.Data["certConfig"] = certConfig + this.Show() } + +func (this *UpdatePopupAction) RunPost(params struct { + CertId int64 + + Name string + IsCA bool + Description string + IsOn bool + + CertFile *actions.File + KeyFile *actions.File + + Must *actions.Must +}) { + // 查询Cert + certConfigResp, err := this.RPC().SSLCertRPC().FindEnabledSSLCertConfig(this.AdminContext(), &pb.FindEnabledSSLCertConfigRequest{CertId: params.CertId}) + if err != nil { + this.ErrorPage(err) + return + } + certConfigJSON := certConfigResp.CertJSON + if len(certConfigJSON) == 0 { + this.NotFound("cert", params.CertId) + return + } + + certConfig := &sslconfigs.SSLCertConfig{} + err = json.Unmarshal(certConfigJSON, certConfig) + if err != nil { + this.ErrorPage(err) + return + } + + // 校验参数 + params.Must. + Field("name", params.Name). + Require("请输入证书说明") + + if params.CertFile != nil { + certConfig.CertData, err = params.CertFile.Read() + if err != nil { + this.Fail("读取证书文件内容错误,请重新上传") + } + } + + if !params.IsCA { + if params.KeyFile != nil { + certConfig.KeyData, err = params.KeyFile.Read() + if err != nil { + this.Fail("读取密钥文件内容错误,请重新上传") + } + } + } + + // 校验 + certConfig.IsCA = params.IsCA + err = certConfig.Init() + if err != nil { + if params.IsCA { + this.Fail("证书校验错误:" + err.Error()) + } else { + this.Fail("证书或密钥校验错误:" + err.Error()) + } + } + + // 保存 + _, err = this.RPC().SSLCertRPC().UpdateSSLCert(this.AdminContext(), &pb.UpdateSSLCertRequest{ + CertId: params.CertId, + IsOn: params.IsOn, + Name: params.Name, + Description: params.Description, + ServerName: "", + IsCA: params.IsCA, + CertData: certConfig.CertData, + KeyData: certConfig.KeyData, + TimeBeginAt: certConfig.TimeBeginAt, + TimeEndAt: certConfig.TimeEndAt, + DnsNames: certConfig.DNSNames, + CommonNames: certConfig.CommonNames, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/helper.go b/internal/web/actions/default/servers/helper.go index da761f6a..e9781c9a 100644 --- a/internal/web/actions/default/servers/helper.go +++ b/internal/web/actions/default/servers/helper.go @@ -19,6 +19,6 @@ func (this *Helper) BeforeAction(action *actions.ActionObject) { tabbar := actionutils.NewTabbar() tabbar.Add("服务", "", "/servers", "", selectedTabbar == "server") - tabbar.Add("组件", "", "/servers/components", "", selectedTabbar == "component") + tabbar.Add("通用", "", "/servers/components", "", selectedTabbar == "component") actionutils.SetTabbar(action, tabbar) } diff --git a/internal/web/actions/default/servers/server/settings/cache/index.go b/internal/web/actions/default/servers/server/settings/cache/index.go index ecd31ee0..454d74bf 100644 --- a/internal/web/actions/default/servers/server/settings/cache/index.go +++ b/internal/web/actions/default/servers/server/settings/cache/index.go @@ -27,7 +27,7 @@ func (this *IndexAction) RunGet(params struct { } this.Data["webId"] = webConfig.Id - this.Data["cacheConfig"] = webConfig.CacheRef + this.Data["cacheConfig"] = webConfig.CacheRefs // 所有缓存策略 cachePoliciesResp, err := this.RPC().HTTPCachePolicyRPC().FindAllEnabledHTTPCachePolicies(this.AdminContext(), &pb.FindAllEnabledHTTPCachePoliciesRequest{}) diff --git a/internal/web/actions/default/servers/server/settings/locations/cache/index.go b/internal/web/actions/default/servers/server/settings/locations/cache/index.go index b834f580..122d514a 100644 --- a/internal/web/actions/default/servers/server/settings/locations/cache/index.go +++ b/internal/web/actions/default/servers/server/settings/locations/cache/index.go @@ -25,7 +25,7 @@ func (this *IndexAction) RunGet(params struct { } this.Data["webId"] = webConfig.Id - this.Data["cacheConfig"] = webConfig.CacheRef + this.Data["cacheConfig"] = webConfig.CacheRefs // 所有缓存策略 cachePoliciesResp, err := this.RPC().HTTPCachePolicyRPC().FindAllEnabledHTTPCachePolicies(this.AdminContext(), &pb.FindAllEnabledHTTPCachePoliciesRequest{}) diff --git a/internal/web/actions/default/servers/server/settings/locations/locationutils/location_helper.go b/internal/web/actions/default/servers/server/settings/locations/locationutils/location_helper.go index 78788070..532c23ae 100644 --- a/internal/web/actions/default/servers/server/settings/locations/locationutils/location_helper.go +++ b/internal/web/actions/default/servers/server/settings/locations/locationutils/location_helper.go @@ -100,7 +100,7 @@ func (this *LocationHelper) createMenus(serverIdString string, locationIdString "name": "缓存", "url": "/servers/server/settings/locations/cache?serverId=" + serverIdString + "&locationId=" + locationIdString, "isActive": secondMenuItem == "cache", - "isOn": locationConfig != nil && locationConfig.Web != nil && locationConfig.Web.CacheRef != nil && locationConfig.Web.CacheRef.IsPrior, + "isOn": locationConfig != nil && locationConfig.Web != nil && len(locationConfig.Web.CacheRefs) > 0, }) menuItems = append(menuItems, maps.Map{ "name": "访问控制", diff --git a/internal/web/actions/default/servers/serverutils/server_helper.go b/internal/web/actions/default/servers/serverutils/server_helper.go index cbb363cc..596bef5a 100644 --- a/internal/web/actions/default/servers/serverutils/server_helper.go +++ b/internal/web/actions/default/servers/serverutils/server_helper.go @@ -78,12 +78,16 @@ func (this *ServerHelper) createLeftMenu(action *actions.ActionObject) { // TABBAR selectedTabbar, _ := action.Data["mainTab"] tabbar := actionutils.NewTabbar() - tabbar.Add("当前服务:"+server.Name, "", "/servers", "left long alternate arrow", false) + tabbar.Add("服务首页", "", "/servers", "", false) //tabbar.Add("看板", "", "/servers/server/board?serverId="+serverIdString, "dashboard", selectedTabbar == "board") tabbar.Add("日志", "", "/servers/server/log?serverId="+serverIdString, "history", selectedTabbar == "log") //tabbar.Add("统计", "", "/servers/server/stat?serverId="+serverIdString, "chart area", selectedTabbar == "stat") tabbar.Add("设置", "", "/servers/server/settings?serverId="+serverIdString, "setting", selectedTabbar == "setting") tabbar.Add("删除", "", "/servers/server/delete?serverId="+serverIdString, "trash", selectedTabbar == "delete") + { + m := tabbar.Add("当前服务:"+server.Name, "", "/servers/server?serverId="+serverIdString, "", false) + m["right"] = true + } actionutils.SetTabbar(action, tabbar) @@ -207,7 +211,7 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri "name": "缓存", "url": "/servers/server/settings/cache?serverId=" + serverIdString, "isActive": secondMenuItem == "cache", - "isOn": serverConfig.Web != nil && serverConfig.Web.CacheRef != nil && serverConfig.Web.CacheRef.IsOn, + "isOn": serverConfig.Web != nil && len(serverConfig.Web.CacheRefs) > 0, }) menuItems = append(menuItems, maps.Map{ "name": "访问控制", diff --git a/web/public/js/components/common/time-duration-box.js b/web/public/js/components/common/time-duration-box.js new file mode 100644 index 00000000..b35e29f3 --- /dev/null +++ b/web/public/js/components/common/time-duration-box.js @@ -0,0 +1,47 @@ +Vue.component("time-duration-box", { + props: ["v-name", "v-value", "v-count", "v-unit"], + data: function () { + let v = this.vValue + if (v == null) { + v = { + count: this.vCount, + unit: this.vUnit + } + } + if (typeof (v["count"]) != "number") { + v["count"] = -1 + } + return { + "size": v, + countString: (v.count >= 0) ? v.count.toString() : "" + } + }, + watch: { + "countString": function (newValue) { + let value = newValue.trim() + if (value.length == 0) { + this.size.count = -1 + return + } + let count = parseInt(value) + if (!isNaN(count)) { + this.size.count = count + } + } + }, + template: `
| 匹配条件 | +|
| 缓存有效期 | +
+ |
+
| 状态码列表 | +
+ 允许缓存的HTTP状态码列表。 + |
+
| 跳过的Cache-Control值 | +
+ 当响应的Cache-Control为这些值时不缓存响应内容,而且不区分大小写。 + |
+
| 跳过Set-Cookie | +
+
+
+
+
+ 选中后,当响应的Header中有Set-Cookie时不缓存响应内容。 + |
+
| 支持请求no-cache刷新 | +
+
+
+
+
+ 选中后,当请求的Header中含有Pragma: no-cache或Cache-Control: no-cache时,会跳过缓存直接读取源内容。 + |
+
0表示不限制,内容长度从文件尺寸或Content-Length中获取。
+| + + + {{cond.param}} {{cond.operator}} + {{typeName(cond)}}: + {{cond.value}} + + + {{group.connector}} + + | ++ + | +
| - - - {{cond.param}} {{cond.operator}} - {{typeName(cond)}}: - {{cond.value}} - - - {{group.connector}} - - | -- - | -
暂时还没有缓存策略。
+| 策略名称 | +策略类型 | +容量 | +引用服务 | +状态 | +操作 | +
|---|---|---|---|---|---|
| {{policy.name}} | +{{infos[index].typeName}} ({{policy.type}}) | ++ {{policy.capacity.count}}{{policy.capacity.unit.toUpperCase()}} + 不限 + | +{{infos[index].countServers}} | ++ 详情 修改 删除 + | +
内容中通常含有"-----BEGIN CERTIFICATE-----"类似的信息。
内容中通常含有"-----BEGIN CERTIFICATE-----"类似的信息。| 服务名称 | -- {{server.name}} - | -
| 部署的集群 | -- {{server.cluster.name}} - | -
| 服务类型 | -- {{serverType.name}} - | -
| 绑定端口 | -
- -
-
- {{port.portRange}}({{port.protocol}})
-
- |
-
| 绑定域名 | -
-
- {{serverName.type}} {{serverName.name}}
-
- |
-
| 源站地址 | -
-
-
-
- {{origin.addr.protocol}}://{{origin.addr.host}}:{{origin.addr.portRange}}
-
- |
-
| 描述 | -- - - {{server.description}} - | -