diff --git a/internal/rpc/rpc_client.go b/internal/rpc/rpc_client.go index bc013fc0..a6bb3b5f 100644 --- a/internal/rpc/rpc_client.go +++ b/internal/rpc/rpc_client.go @@ -33,6 +33,7 @@ type RPCClient struct { httpHeaderClients []pb.HTTPHeaderServiceClient httpPageClients []pb.HTTPPageServiceClient httpAccessLogPolicyClients []pb.HTTPAccessLogPolicyServiceClient + httpCachePolicyClients []pb.HTTPCachePolicyServiceClient } func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) { @@ -55,6 +56,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) { httpHeaderClients := []pb.HTTPHeaderServiceClient{} httpPageClients := []pb.HTTPPageServiceClient{} httpAccessLogPolicyClients := []pb.HTTPAccessLogPolicyServiceClient{} + httpCachePolicyClients := []pb.HTTPCachePolicyServiceClient{} conns := []*grpc.ClientConn{} for _, endpoint := range apiConfig.RPC.Endpoints { @@ -85,6 +87,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) { httpHeaderClients = append(httpHeaderClients, pb.NewHTTPHeaderServiceClient(conn)) httpPageClients = append(httpPageClients, pb.NewHTTPPageServiceClient(conn)) httpAccessLogPolicyClients = append(httpAccessLogPolicyClients, pb.NewHTTPAccessLogPolicyServiceClient(conn)) + httpCachePolicyClients = append(httpCachePolicyClients, pb.NewHTTPCachePolicyServiceClient(conn)) } return &RPCClient{ @@ -104,6 +107,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) { httpHeaderClients: httpHeaderClients, httpPageClients: httpPageClients, httpAccessLogPolicyClients: httpAccessLogPolicyClients, + httpCachePolicyClients: httpCachePolicyClients, }, nil } @@ -212,6 +216,13 @@ func (this *RPCClient) HTTPAccessLogPolicyRPC() pb.HTTPAccessLogPolicyServiceCli return nil } +func (this *RPCClient) HTTPCachePolicyRPC() pb.HTTPCachePolicyServiceClient { + if len(this.httpCachePolicyClients) > 0 { + return this.httpCachePolicyClients[rands.Int(0, len(this.httpCachePolicyClients)-1)] + } + return nil +} + func (this *RPCClient) Context(adminId int64) context.Context { ctx := context.Background() m := maps.Map{ diff --git a/internal/web/actions/default/servers/server/settings/accessLog/index.go b/internal/web/actions/default/servers/server/settings/accessLog/index.go index 008d3b99..67705c8e 100644 --- a/internal/web/actions/default/servers/server/settings/accessLog/index.go +++ b/internal/web/actions/default/servers/server/settings/accessLog/index.go @@ -34,7 +34,7 @@ func (this *IndexAction) RunGet(params struct { return } this.Data["webId"] = webConfig.Id - this.Data["accessLogConfig"] = webConfig.AccessLog + this.Data["accessLogConfig"] = webConfig.AccessLogRef // 可选的缓存策略 policiesResp, err := this.RPC().HTTPAccessLogPolicyRPC().FindAllEnabledHTTPAccessLogPolicies(this.AdminContext(), &pb.FindAllEnabledHTTPAccessLogPoliciesRequest{}) 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 10968e88..d4c3d529 100644 --- a/internal/web/actions/default/servers/server/settings/cache/index.go +++ b/internal/web/actions/default/servers/server/settings/cache/index.go @@ -1,7 +1,12 @@ 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" + "github.com/iwind/TeaGo/maps" ) type IndexAction struct { @@ -16,7 +21,56 @@ func (this *IndexAction) Init() { func (this *IndexAction) RunGet(params struct { ServerId int64 }) { - // TODO + webConfigResp, err := this.RPC().ServerRPC().FindAndInitServerWebConfig(this.AdminContext(), &pb.FindAndInitServerWebRequest{ServerId: params.ServerId}) + if err != nil { + this.ErrorPage(err) + return + } + webConfig := &serverconfigs.HTTPWebConfig{} + err = json.Unmarshal(webConfigResp.Config, webConfig) + if err != nil { + this.ErrorPage(err) + return + } + + this.Data["webId"] = webConfig.Id + this.Data["cacheConfig"] = webConfig.CacheRef + + // 所有缓存策略 + cachePoliciesResp, err := this.RPC().HTTPCachePolicyRPC().FindAllEnabledHTTPCachePolicies(this.AdminContext(), &pb.FindAllEnabledHTTPCachePoliciesRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + policyMaps := []maps.Map{} + for _, policy := range cachePoliciesResp.CachePolicies { + policyMaps = append(policyMaps, maps.Map{ + "id": policy.Id, + "name": policy.Name, + "isOn": policy.IsOn, + }) + } + this.Data["policies"] = policyMaps this.Show() } + +func (this *IndexAction) RunPost(params struct { + WebId int64 + CacheJSON []byte + + Must *actions.Must +}) { + // TODO 校验配置 + + _, err := this.RPC().HTTPWebRPC().UpdateHTTPCache(this.AdminContext(), &pb.UpdateHTTPCacheRequest{ + WebId: params.WebId, + CacheJSON: params.CacheJSON, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/server/settings/cache/init.go b/internal/web/actions/default/servers/server/settings/cache/init.go index 840e3046..0fc44b11 100644 --- a/internal/web/actions/default/servers/server/settings/cache/init.go +++ b/internal/web/actions/default/servers/server/settings/cache/init.go @@ -12,7 +12,7 @@ func init() { Helper(helpers.NewUserMustAuth()). Helper(serverutils.NewServerHelper()). Prefix("/servers/server/settings/cache"). - Get("", new(IndexAction)). + GetPost("", new(IndexAction)). EndAll() }) } diff --git a/internal/web/actions/default/servers/server/settings/gzip/index.go b/internal/web/actions/default/servers/server/settings/gzip/index.go index 1d0d151e..cbba4458 100644 --- a/internal/web/actions/default/servers/server/settings/gzip/index.go +++ b/internal/web/actions/default/servers/server/settings/gzip/index.go @@ -6,6 +6,7 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/iwind/TeaGo/actions" + "github.com/iwind/TeaGo/types" ) type IndexAction struct { @@ -32,16 +33,12 @@ func (this *IndexAction) RunGet(params struct { return } - webResp, err := this.RPC().HTTPWebRPC().FindEnabledHTTPWeb(this.AdminContext(), &pb.FindEnabledHTTPWebRequest{WebId: webConfig.Id}) - if err != nil { - this.ErrorPage(err) - return - } - web := webResp.Web - this.Data["webId"] = webConfig.Id - gzipId := web.GzipId + gzipId := int64(0) + if webConfig.GzipRef != nil { + gzipId = webConfig.GzipRef.GzipId + } gzipConfig := &serverconfigs.HTTPGzipConfig{ Id: 0, IsOn: true, @@ -95,11 +92,10 @@ func (this *IndexAction) RunPost(params struct { } } - if params.GzipId > 0 { _, err := this.RPC().HTTPGzipRPC().UpdateHTTPGzip(this.AdminContext(), &pb.UpdateHTTPGzipRequest{ GzipId: params.GzipId, - Level: int32(params.Level), + Level: types.Int32(params.Level), MinLength: minLength, MaxLength: maxLength, }) @@ -109,9 +105,9 @@ func (this *IndexAction) RunPost(params struct { } } else { resp, err := this.RPC().HTTPGzipRPC().CreateHTTPGzip(this.AdminContext(), &pb.CreateHTTPGzipRequest{ - Level: 0, - MinLength: nil, - MaxLength: nil, + Level: types.Int32(params.Level), + MinLength: minLength, + MaxLength: maxLength, }) if err != nil { this.ErrorPage(err) @@ -119,9 +115,19 @@ func (this *IndexAction) RunPost(params struct { } gzipId := resp.GzipId - _, err = this.RPC().HTTPWebRPC().UpdateHTTPWebGzip(this.AdminContext(), &pb.UpdateHTTPWebGzipRequest{ - WebId: params.WebId, + gzipRef := &serverconfigs.HTTPGzipRef{ + IsOn: true, GzipId: gzipId, + } + gzipRefJSON, err := json.Marshal(gzipRef) + if err != nil { + this.ErrorPage(err) + return + } + + _, err = this.RPC().HTTPWebRPC().UpdateHTTPWebGzip(this.AdminContext(), &pb.UpdateHTTPWebGzipRequest{ + WebId: params.WebId, + GzipJSON: gzipRefJSON, }) if err != nil { this.ErrorPage(err) diff --git a/internal/web/actions/default/servers/server/settings/stat/index.go b/internal/web/actions/default/servers/server/settings/stat/index.go index 8f245bac..90de0f63 100644 --- a/internal/web/actions/default/servers/server/settings/stat/index.go +++ b/internal/web/actions/default/servers/server/settings/stat/index.go @@ -33,7 +33,7 @@ func (this *IndexAction) RunGet(params struct { } this.Data["webId"] = webConfig.Id - this.Data["statConfig"] = webConfig.Stat + this.Data["statConfig"] = webConfig.StatRef this.Show() } diff --git a/web/public/js/components/server/http-cache-config-box.js b/web/public/js/components/server/http-cache-config-box.js new file mode 100644 index 00000000..6ab5b9c4 --- /dev/null +++ b/web/public/js/components/server/http-cache-config-box.js @@ -0,0 +1,46 @@ +Vue.component("http-cache-config-box", { + props: ["v-cache-config", "v-cache-policies"], + data: function () { + let cacheConfig = this.vCacheConfig + if (cacheConfig == null) { + cacheConfig = { + isOn: false, + cachePolicyId: 0 + } + } + return { + cacheConfig: cacheConfig + } + }, + template: `
| 是否开启缓存 | +
+
+
+
+
+ |
+
| 选择缓存策略 | +
+ 暂时没有可选的缓存策略
+
+
+
+ |
+