mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 23:20:26 +08:00
实现访问日志配置
This commit is contained in:
@@ -65,6 +65,7 @@ func (this *APINode) listenRPC() error {
|
||||
pb.RegisterHTTPHeaderPolicyServiceServer(rpcServer, &services.HTTPHeaderPolicyService{})
|
||||
pb.RegisterHTTPHeaderServiceServer(rpcServer, &services.HTTPHeaderService{})
|
||||
pb.RegisterHTTPPageServiceServer(rpcServer, &services.HTTPPageService{})
|
||||
pb.RegisterHTTPAccessLogPolicyServiceServer(rpcServer, &services.HTTPAccessLogPolicyService{})
|
||||
err = rpcServer.Serve(listener)
|
||||
if err != nil {
|
||||
return errors.New("[API]start rpc failed: " + err.Error())
|
||||
|
||||
118
internal/db/models/http_access_log_policy_dao.go
Normal file
118
internal/db/models/http_access_log_policy_dao.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
const (
|
||||
HTTPAccessLogPolicyStateEnabled = 1 // 已启用
|
||||
HTTPAccessLogPolicyStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type HTTPAccessLogPolicyDAO dbs.DAO
|
||||
|
||||
func NewHTTPAccessLogPolicyDAO() *HTTPAccessLogPolicyDAO {
|
||||
return dbs.NewDAO(&HTTPAccessLogPolicyDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeHTTPAccessLogPolicies",
|
||||
Model: new(HTTPAccessLogPolicy),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*HTTPAccessLogPolicyDAO)
|
||||
}
|
||||
|
||||
var SharedHTTPAccessLogPolicyDAO = NewHTTPAccessLogPolicyDAO()
|
||||
|
||||
// 启用条目
|
||||
func (this *HTTPAccessLogPolicyDAO) EnableHTTPAccessLogPolicy(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", HTTPAccessLogPolicyStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *HTTPAccessLogPolicyDAO) DisableHTTPAccessLogPolicy(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", HTTPAccessLogPolicyStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *HTTPAccessLogPolicyDAO) FindEnabledHTTPAccessLogPolicy(id int64) (*HTTPAccessLogPolicy, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", HTTPAccessLogPolicyStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*HTTPAccessLogPolicy), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *HTTPAccessLogPolicyDAO) FindHTTPAccessLogPolicyName(id int64) (string, error) {
|
||||
return this.Query().
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 查找所有可用策略信息
|
||||
func (this *HTTPAccessLogPolicyDAO) FindAllEnabledAccessLogPolicies() (result []*HTTPAccessLogPolicy, err error) {
|
||||
_, err = this.Query().
|
||||
State(HTTPAccessLogPolicyStateEnabled).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 组合配置
|
||||
func (this *HTTPAccessLogPolicyDAO) ComposeAccessLogPolicyConfig(policyId int64) (*serverconfigs.HTTPAccessLogStoragePolicy, error) {
|
||||
policy, err := this.FindEnabledHTTPAccessLogPolicy(policyId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if policy == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
config := &serverconfigs.HTTPAccessLogStoragePolicy{}
|
||||
config.Id = int64(policy.Id)
|
||||
config.IsOn = policy.IsOn == 1
|
||||
config.Name = policy.Name
|
||||
config.Type = policy.Type
|
||||
|
||||
// 选项
|
||||
if IsNotNull(policy.Options) {
|
||||
m := map[string]interface{}{}
|
||||
err = json.Unmarshal([]byte(policy.Options), &m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Options = m
|
||||
}
|
||||
|
||||
// 条件
|
||||
if IsNotNull(policy.Conds) {
|
||||
// TODO 需要用更全面的条件管理器来代替RequestCond
|
||||
conds := []*shared.RequestCond{}
|
||||
err = json.Unmarshal([]byte(policy.Conds), &conds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Conds = conds
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
5
internal/db/models/http_access_log_policy_dao_test.go
Normal file
5
internal/db/models/http_access_log_policy_dao_test.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
34
internal/db/models/http_access_log_policy_model.go
Normal file
34
internal/db/models/http_access_log_policy_model.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package models
|
||||
|
||||
// 访问日志策略
|
||||
type HTTPAccessLogPolicy struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
TemplateId uint32 `field:"templateId"` // 模版ID
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
State uint8 `field:"state"` // 状态
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Name string `field:"name"` // 名称
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
Type string `field:"type"` // 存储类型
|
||||
Options string `field:"options"` // 存储选项
|
||||
Conds string `field:"conds"` // 请求条件
|
||||
}
|
||||
|
||||
type HTTPAccessLogPolicyOperator struct {
|
||||
Id interface{} // ID
|
||||
TemplateId interface{} // 模版ID
|
||||
AdminId interface{} // 管理员ID
|
||||
UserId interface{} // 用户ID
|
||||
State interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
Name interface{} // 名称
|
||||
IsOn interface{} // 是否启用
|
||||
Type interface{} // 存储类型
|
||||
Options interface{} // 存储选项
|
||||
Conds interface{} // 请求条件
|
||||
}
|
||||
|
||||
func NewHTTPAccessLogPolicyOperator() *HTTPAccessLogPolicyOperator {
|
||||
return &HTTPAccessLogPolicyOperator{}
|
||||
}
|
||||
1
internal/db/models/http_access_log_policy_model_ext.go
Normal file
1
internal/db/models/http_access_log_policy_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
@@ -136,6 +136,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
|
||||
}
|
||||
}
|
||||
|
||||
// 访问日志
|
||||
if IsNotNull(web.AccessLog) {
|
||||
accessLogConfig := &serverconfigs.HTTPAccessLogConfig{}
|
||||
err = json.Unmarshal([]byte(web.AccessLog), accessLogConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.AccessLog = accessLogConfig
|
||||
}
|
||||
|
||||
// TODO 更多配置
|
||||
|
||||
return config, nil
|
||||
@@ -265,6 +275,22 @@ func (this *HTTPWebDAO) UpdateWebShutdown(webId int64, shutdownJSON []byte) erro
|
||||
return this.NotifyUpdating(webId)
|
||||
}
|
||||
|
||||
// 更改访问日志策略
|
||||
func (this *HTTPWebDAO) UpdateWebAccessLogConfig(webId int64, accessLogJSON []byte) error {
|
||||
if webId <= 0 {
|
||||
return errors.New("invalid webId")
|
||||
}
|
||||
op := NewHTTPWebOperator()
|
||||
op.Id = webId
|
||||
op.AccessLog = JSONBytes(accessLogJSON)
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdating(webId)
|
||||
}
|
||||
|
||||
// 通知更新
|
||||
func (this *HTTPWebDAO) NotifyUpdating(webId int64) error {
|
||||
err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId)
|
||||
|
||||
@@ -16,13 +16,13 @@ type HTTPWeb struct {
|
||||
Pages string `field:"pages"` // 特殊页面
|
||||
FirewallId uint32 `field:"firewallId"` // WAF ID
|
||||
CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID
|
||||
AccessLogPolicyId uint32 `field:"accessLogPolicyId"` // 访问日志策略ID
|
||||
RedirectToHttps string `field:"redirectToHttps"` // 跳转到HTTPS设置
|
||||
Indexes string `field:"indexes"` // 首页文件列表
|
||||
MaxRequestBodySize string `field:"maxRequestBodySize"` // 最大允许的请求内容尺寸
|
||||
StatPolicyId uint32 `field:"statPolicyId"` // 统计策略ID
|
||||
RequestHeaderPolicyId uint32 `field:"requestHeaderPolicyId"` // Request Header策略ID
|
||||
ResponseHeaderPolicyId uint32 `field:"responseHeaderPolicyId"` // Response Header策略
|
||||
AccessLog string `field:"accessLog"` // 访问日志配置
|
||||
}
|
||||
|
||||
type HTTPWebOperator struct {
|
||||
@@ -40,13 +40,13 @@ type HTTPWebOperator struct {
|
||||
Pages interface{} // 特殊页面
|
||||
FirewallId interface{} // WAF ID
|
||||
CachePolicyId interface{} // 缓存策略ID
|
||||
AccessLogPolicyId interface{} // 访问日志策略ID
|
||||
RedirectToHttps interface{} // 跳转到HTTPS设置
|
||||
Indexes interface{} // 首页文件列表
|
||||
MaxRequestBodySize interface{} // 最大允许的请求内容尺寸
|
||||
StatPolicyId interface{} // 统计策略ID
|
||||
RequestHeaderPolicyId interface{} // Request Header策略ID
|
||||
ResponseHeaderPolicyId interface{} // Response Header策略
|
||||
AccessLog interface{} // 访问日志配置
|
||||
}
|
||||
|
||||
func NewHTTPWebOperator() *HTTPWebOperator {
|
||||
|
||||
39
internal/rpc/services/service_http_access_log_policy.go
Normal file
39
internal/rpc/services/service_http_access_log_policy.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
type HTTPAccessLogPolicyService struct {
|
||||
}
|
||||
|
||||
// 获取所有可用策略
|
||||
func (this *HTTPAccessLogPolicyService) FindAllEnabledHTTPAccessLogPolicies(ctx context.Context, req *pb.FindAllEnabledHTTPAccessLogPoliciesRequest) (*pb.FindAllEnabledHTTPAccessLogPoliciesResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
policies, err := models.SharedHTTPAccessLogPolicyDAO.FindAllEnabledAccessLogPolicies()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []*pb.HTTPAccessLogPolicy{}
|
||||
for _, policy := range policies {
|
||||
result = append(result, &pb.HTTPAccessLogPolicy{
|
||||
Id: int64(policy.Id),
|
||||
Name: policy.Name,
|
||||
IsOn: policy.IsOn == 1,
|
||||
Type: policy.Name,
|
||||
OptionsJSON: []byte(policy.Options),
|
||||
CondsJSON: []byte(policy.Conds),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindAllEnabledHTTPAccessLogPoliciesResponse{AccessLogPolicies: result}, nil
|
||||
}
|
||||
@@ -162,3 +162,18 @@ func (this *HTTPWebService) UpdateHTTPWebPages(ctx context.Context, req *pb.Upda
|
||||
}
|
||||
return rpcutils.RPCUpdateSuccess()
|
||||
}
|
||||
|
||||
// 更改访问日志配置
|
||||
func (this *HTTPWebService) UpdateHTTPAccessLog(ctx context.Context, req *pb.UpdateHTTPAccessLogRequest) (*pb.RPCUpdateSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedHTTPWebDAO.UpdateWebAccessLogConfig(req.WebId, req.AccessLogJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rpcutils.RPCUpdateSuccess()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user