mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	优化代码/启用的日志策略排在最前面
This commit is contained in:
		@@ -1,13 +1,9 @@
 | 
			
		||||
package authority
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
	timeutil "github.com/iwind/TeaGo/utils/time"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type AuthorityKeyDAO dbs.DAO
 | 
			
		||||
@@ -33,63 +29,3 @@ func init() {
 | 
			
		||||
		_, _ = SharedAuthorityKeyDAO.IsPlus(nil)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateKey 设置Key
 | 
			
		||||
func (this *AuthorityKeyDAO) UpdateKey(tx *dbs.Tx, value string, dayFrom string, dayTo string, hostname string, macAddresses []string, company string) error {
 | 
			
		||||
	one, err := this.Query(tx).
 | 
			
		||||
		AscPk().
 | 
			
		||||
		Find()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	var op = NewAuthorityKeyOperator()
 | 
			
		||||
	if one != nil {
 | 
			
		||||
		op.Id = one.(*AuthorityKey).Id
 | 
			
		||||
	}
 | 
			
		||||
	op.Value = value
 | 
			
		||||
	op.DayFrom = dayFrom
 | 
			
		||||
	op.DayTo = dayTo
 | 
			
		||||
	op.Hostname = hostname
 | 
			
		||||
 | 
			
		||||
	if len(macAddresses) == 0 {
 | 
			
		||||
		macAddresses = []string{}
 | 
			
		||||
	}
 | 
			
		||||
	macAddressesJSON, err := json.Marshal(macAddresses)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.MacAddresses = macAddressesJSON
 | 
			
		||||
	op.Company = company
 | 
			
		||||
	op.UpdatedAt = time.Now().Unix()
 | 
			
		||||
 | 
			
		||||
	return this.Save(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReadKey 读取Key
 | 
			
		||||
func (this *AuthorityKeyDAO) ReadKey(tx *dbs.Tx) (key *AuthorityKey, err error) {
 | 
			
		||||
	one, err := this.Query(tx).
 | 
			
		||||
		AscPk().
 | 
			
		||||
		Find()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if one == nil {
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
	key = one.(*AuthorityKey)
 | 
			
		||||
 | 
			
		||||
	// 顺便更新相关变量
 | 
			
		||||
	if key.DayTo >= timeutil.Format("Y-m-d") {
 | 
			
		||||
		teaconst.IsPlus = true
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ResetKey 重置Key
 | 
			
		||||
func (this *AuthorityKeyDAO) ResetKey(tx *dbs.Tx) error {
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Delete()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,23 +0,0 @@
 | 
			
		||||
package authority
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	_ "github.com/iwind/TeaGo/bootstrap"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestAuthorityKeyDAO_UpdateValue(t *testing.T) {
 | 
			
		||||
	err := NewAuthorityKeyDAO().UpdateKey(nil, "12345678", "", "", "", []string{}, "")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	t.Log("ok")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestAuthorityKeyDAO_ReadValue(t *testing.T) {
 | 
			
		||||
	value, err := NewAuthorityKeyDAO().ReadKey(nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	t.Log(value)
 | 
			
		||||
}
 | 
			
		||||
@@ -87,6 +87,7 @@ func (this *HTTPAccessLogPolicyDAO) CountAllEnabledPolicies(tx *dbs.Tx) (int64,
 | 
			
		||||
func (this *HTTPAccessLogPolicyDAO) ListEnabledPolicies(tx *dbs.Tx, offset int64, size int64) (result []*HTTPAccessLogPolicy, err error) {
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
		State(HTTPAccessLogPolicyStateEnabled).
 | 
			
		||||
		Desc("isOn").
 | 
			
		||||
		DescPk().
 | 
			
		||||
		Offset(offset).
 | 
			
		||||
		Limit(size).
 | 
			
		||||
 
 | 
			
		||||
@@ -12,8 +12,8 @@ type HTTPAccessLogPolicyService struct {
 | 
			
		||||
	BaseService
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountAllEnabledHTTPAccessLogPolicies 计算访问日志策略数量
 | 
			
		||||
func (this *HTTPAccessLogPolicyService) CountAllEnabledHTTPAccessLogPolicies(ctx context.Context, req *pb.CountAllEnabledHTTPAccessLogPoliciesRequest) (*pb.RPCCountResponse, error) {
 | 
			
		||||
// CountAllHTTPAccessLogPolicies 计算访问日志策略数量
 | 
			
		||||
func (this *HTTPAccessLogPolicyService) CountAllHTTPAccessLogPolicies(ctx context.Context, req *pb.CountAllHTTPAccessLogPoliciesRequest) (*pb.RPCCountResponse, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -27,8 +27,8 @@ func (this *HTTPAccessLogPolicyService) CountAllEnabledHTTPAccessLogPolicies(ctx
 | 
			
		||||
	return this.SuccessCount(count)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListEnabledHTTPAccessLogPolicies 列出单页访问日志策略
 | 
			
		||||
func (this *HTTPAccessLogPolicyService) ListEnabledHTTPAccessLogPolicies(ctx context.Context, req *pb.ListEnabledHTTPAccessLogPoliciesRequest) (*pb.ListEnabledHTTPAccessLogPoliciesResponse, error) {
 | 
			
		||||
// ListHTTPAccessLogPolicies 列出单页访问日志策略
 | 
			
		||||
func (this *HTTPAccessLogPolicyService) ListHTTPAccessLogPolicies(ctx context.Context, req *pb.ListHTTPAccessLogPoliciesRequest) (*pb.ListHTTPAccessLogPoliciesResponse, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -52,7 +52,7 @@ func (this *HTTPAccessLogPolicyService) ListEnabledHTTPAccessLogPolicies(ctx con
 | 
			
		||||
			FirewallOnly: policy.FirewallOnly == 1,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.ListEnabledHTTPAccessLogPoliciesResponse{HttpAccessLogPolicies: pbPolicies}, nil
 | 
			
		||||
	return &pb.ListHTTPAccessLogPoliciesResponse{HttpAccessLogPolicies: pbPolicies}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateHTTPAccessLogPolicy 创建访问日志策略
 | 
			
		||||
@@ -105,8 +105,8 @@ func (this *HTTPAccessLogPolicyService) UpdateHTTPAccessLogPolicy(ctx context.Co
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledHTTPAccessLogPolicy 查找单个访问日志策略
 | 
			
		||||
func (this *HTTPAccessLogPolicyService) FindEnabledHTTPAccessLogPolicy(ctx context.Context, req *pb.FindEnabledHTTPAccessLogPolicyRequest) (*pb.FindEnabledHTTPAccessLogPolicyResponse, error) {
 | 
			
		||||
// FindHTTPAccessLogPolicy 查找单个访问日志策略
 | 
			
		||||
func (this *HTTPAccessLogPolicyService) FindHTTPAccessLogPolicy(ctx context.Context, req *pb.FindHTTPAccessLogPolicyRequest) (*pb.FindHTTPAccessLogPolicyResponse, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -118,9 +118,9 @@ func (this *HTTPAccessLogPolicyService) FindEnabledHTTPAccessLogPolicy(ctx conte
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if policy == nil {
 | 
			
		||||
		return &pb.FindEnabledHTTPAccessLogPolicyResponse{HttpAccessLogPolicy: nil}, nil
 | 
			
		||||
		return &pb.FindHTTPAccessLogPolicyResponse{HttpAccessLogPolicy: nil}, nil
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.FindEnabledHTTPAccessLogPolicyResponse{HttpAccessLogPolicy: &pb.HTTPAccessLogPolicy{
 | 
			
		||||
	return &pb.FindHTTPAccessLogPolicyResponse{HttpAccessLogPolicy: &pb.HTTPAccessLogPolicy{
 | 
			
		||||
		Id:           int64(policy.Id),
 | 
			
		||||
		Name:         policy.Name,
 | 
			
		||||
		IsOn:         policy.IsOn,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user