mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 15:00:27 +08:00
节点可以单独设置所使用的API节点地址
This commit is contained in:
@@ -961,6 +961,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
|
||||
Level: types.Int32(node.Level),
|
||||
GroupId: int64(node.GroupId),
|
||||
EnableIPLists: node.EnableIPLists,
|
||||
APINodeAddrs: node.DecodeAPINodeAddrs(),
|
||||
}
|
||||
|
||||
// API节点IP
|
||||
@@ -2038,6 +2039,46 @@ func (this *NodeDAO) UpdateNodeDDoSProtection(tx *dbs.Tx, nodeId int64, ddosProt
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindNodeAPIConfig 查找API相关配置信息
|
||||
func (this *NodeDAO) FindNodeAPIConfig(tx *dbs.Tx, nodeId int64) (*Node, error) {
|
||||
if nodeId <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
one, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Result("apiNodeAddrs").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
return one.(*Node), nil
|
||||
}
|
||||
|
||||
// UpdateNodeAPIConfig 修改API相关配置信息
|
||||
func (this *NodeDAO) UpdateNodeAPIConfig(tx *dbs.Tx, nodeId int64, apiNodeAddrs []*serverconfigs.NetworkAddressConfig) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
|
||||
if apiNodeAddrs == nil {
|
||||
apiNodeAddrs = []*serverconfigs.NetworkAddressConfig{}
|
||||
}
|
||||
apiNodeAddrsJSON, err := json.Marshal(apiNodeAddrs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var op = NewNodeOperator()
|
||||
op.Id = nodeId
|
||||
op.ApiNodeAddrs = apiNodeAddrsJSON
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdate(tx, nodeId)
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知节点相关更新
|
||||
func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||
// 这里只需要通知单个集群即可,因为节点是公用的,更新一个就相当于更新了所有
|
||||
|
||||
@@ -42,6 +42,7 @@ type Node struct {
|
||||
CacheDiskSubDirs dbs.JSON `field:"cacheDiskSubDirs"` // 其他缓存目录
|
||||
DnsResolver dbs.JSON `field:"dnsResolver"` // DNS解析器
|
||||
EnableIPLists bool `field:"enableIPLists"` // 启用IP名单
|
||||
ApiNodeAddrs dbs.JSON `field:"apiNodeAddrs"` // API节点地址
|
||||
}
|
||||
|
||||
type NodeOperator struct {
|
||||
@@ -83,6 +84,7 @@ type NodeOperator struct {
|
||||
CacheDiskSubDirs any // 其他缓存目录
|
||||
DnsResolver any // DNS解析器
|
||||
EnableIPLists any // 启用IP名单
|
||||
ApiNodeAddrs any // API节点地址
|
||||
}
|
||||
|
||||
func NewNodeOperator() *NodeOperator {
|
||||
|
||||
@@ -137,6 +137,7 @@ func (this *Node) HasDDoSProtection() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// DecodeMaxCacheDiskCapacity 解析硬盘容量
|
||||
func (this *Node) DecodeMaxCacheDiskCapacity() *shared.SizeCapacity {
|
||||
if this.MaxCacheDiskCapacity.IsNull() {
|
||||
return nil
|
||||
@@ -147,6 +148,7 @@ func (this *Node) DecodeMaxCacheDiskCapacity() *shared.SizeCapacity {
|
||||
return capacity
|
||||
}
|
||||
|
||||
// DecodeMaxCacheMemoryCapacity 解析内存容量
|
||||
func (this *Node) DecodeMaxCacheMemoryCapacity() *shared.SizeCapacity {
|
||||
if this.MaxCacheMemoryCapacity.IsNull() {
|
||||
return nil
|
||||
@@ -171,6 +173,7 @@ func (this *Node) DecodeDNSResolver() *nodeconfigs.DNSResolverConfig {
|
||||
return resolverConfig
|
||||
}
|
||||
|
||||
// DecodeLnAddrs 解析Ln地址
|
||||
func (this *Node) DecodeLnAddrs() []string {
|
||||
if IsNull(this.LnAddrs) {
|
||||
return nil
|
||||
@@ -184,6 +187,7 @@ func (this *Node) DecodeLnAddrs() []string {
|
||||
return result
|
||||
}
|
||||
|
||||
// DecodeCacheDiskSubDirs 解析缓存目录
|
||||
func (this *Node) DecodeCacheDiskSubDirs() []*serverconfigs.CacheDir {
|
||||
if IsNull(this.CacheDiskSubDirs) {
|
||||
return nil
|
||||
@@ -196,3 +200,17 @@ func (this *Node) DecodeCacheDiskSubDirs() []*serverconfigs.CacheDir {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// DecodeAPINodeAddrs 解析API节点地址
|
||||
func (this *Node) DecodeAPINodeAddrs() []*serverconfigs.NetworkAddressConfig {
|
||||
var result = []*serverconfigs.NetworkAddressConfig{}
|
||||
if IsNull(this.ApiNodeAddrs) {
|
||||
return result
|
||||
}
|
||||
|
||||
err := json.Unmarshal(this.ApiNodeAddrs, &result)
|
||||
if err != nil {
|
||||
remotelogs.Error("Node.DecodeAPINodeAddrs", err.Error())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ import (
|
||||
|
||||
// 注册服务
|
||||
func (this *APINode) registerServices(server *grpc.Server) {
|
||||
{
|
||||
var instance = this.serviceInstance(&services.PingService{}).(*services.PingService)
|
||||
pb.RegisterPingServiceServer(server, instance)
|
||||
this.rest(instance)
|
||||
}
|
||||
{
|
||||
var instance = this.serviceInstance(&services.APITokenService{}).(*services.APITokenService)
|
||||
pb.RegisterAPITokenServiceServer(server, instance)
|
||||
|
||||
@@ -2041,6 +2041,14 @@ func (this *NodeService) FindEnabledNodeConfigInfo(ctx context.Context, req *pb.
|
||||
if dnsResolverConfig != nil {
|
||||
result.HasSystemSettings = dnsResolverConfig.Type != nodeconfigs.DNSResolverTypeDefault
|
||||
}
|
||||
|
||||
if !result.HasSystemSettings {
|
||||
// api node addresses
|
||||
var apiNodeAddrs = node.DecodeAPINodeAddrs()
|
||||
if len(apiNodeAddrs) > 0 {
|
||||
result.HasSystemSettings = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ddos protection
|
||||
@@ -2142,3 +2150,50 @@ func (this *NodeService) UpdateNodeRegionInfo(ctx context.Context, req *pb.Updat
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindNodeAPIConfig 查找单个节点的API相关配置
|
||||
func (this *NodeService) FindNodeAPIConfig(ctx context.Context, req *pb.FindNodeAPIConfigRequest) (*pb.FindNodeAPIConfigResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
node, err := models.SharedNodeDAO.FindNodeAPIConfig(tx, req.NodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if node == nil {
|
||||
return &pb.FindNodeAPIConfigResponse{
|
||||
ApiNodeAddrsJSON: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &pb.FindNodeAPIConfigResponse{
|
||||
ApiNodeAddrsJSON: node.ApiNodeAddrs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateNodeAPIConfig 修改某个节点的API相关配置
|
||||
func (this *NodeService) UpdateNodeAPIConfig(ctx context.Context, req *pb.UpdateNodeAPIConfigRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
var apiNodeAddrs = []*serverconfigs.NetworkAddressConfig{}
|
||||
if len(req.ApiNodeAddrsJSON) > 0 {
|
||||
err = json.Unmarshal(req.ApiNodeAddrsJSON, &apiNodeAddrs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
err = models.SharedNodeDAO.UpdateNodeAPIConfig(tx, req.NodeId, apiNodeAddrs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
23
internal/rpc/services/service_ping.go
Normal file
23
internal/rpc/services/service_ping.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// PingService Ping服务
|
||||
// 用来测试连接是否可用
|
||||
type PingService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// Ping 发起Ping
|
||||
func (this *PingService) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error) {
|
||||
_, _, err := this.ValidateNodeId(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.PingResponse{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user