mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	DNS添加域名的时候自动同步数据
This commit is contained in:
		@@ -1 +1,16 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 获取API参数
 | 
			
		||||
func (this *DNSProvider) DecodeAPIParams() (maps.Map, error) {
 | 
			
		||||
	if len(this.ApiParams) == 0 || this.ApiParams == "null" {
 | 
			
		||||
		return maps.Map{}, nil
 | 
			
		||||
	}
 | 
			
		||||
	result := maps.Map{}
 | 
			
		||||
	err := json.Unmarshal([]byte(this.ApiParams), &result)
 | 
			
		||||
	return result, err
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -25,10 +25,64 @@ func (this *DNSDomainService) CreateDNSDomain(ctx context.Context, req *pb.Creat
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 查询Provider
 | 
			
		||||
	provider, err := models.SharedDNSProviderDAO.FindEnabledDNSProvider(req.DnsProviderId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if provider == nil {
 | 
			
		||||
		return nil, errors.New("can not find provider")
 | 
			
		||||
	}
 | 
			
		||||
	apiParams, err := provider.DecodeAPIParams()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	domainId, err := models.SharedDNSDomainDAO.CreateDomain(req.DnsProviderId, req.Name)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 更新数据,且不提示错误
 | 
			
		||||
	go func() {
 | 
			
		||||
		domainName := req.Name
 | 
			
		||||
 | 
			
		||||
		providerInterface := dnsclients.FindProvider(provider.Type)
 | 
			
		||||
		if providerInterface == nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		err = providerInterface.Auth(apiParams)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			// 这里我们刻意不提示错误
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		routes, err := providerInterface.GetRoutes(domainName)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		routesJSON, err := json.Marshal(routes)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		err = models.SharedDNSDomainDAO.UpdateDomainRoutes(domainId, routesJSON)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		records, err := providerInterface.GetRecords(domainName)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		recordsJSON, err := json.Marshal(records)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		err = models.SharedDNSDomainDAO.UpdateDomainRecords(domainId, recordsJSON)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	return &pb.CreateDNSDomainResponse{DnsDomainId: domainId}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user