113 lines
3.3 KiB
Go
113 lines
3.3 KiB
Go
package httpdns
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
)
|
|
|
|
// HTTPDNSDomainService HTTPDNS域名服务
|
|
type HTTPDNSDomainService struct {
|
|
services.BaseService
|
|
pb.UnimplementedHTTPDNSDomainServiceServer
|
|
}
|
|
|
|
func (this *HTTPDNSDomainService) CreateHTTPDNSDomain(ctx context.Context, req *pb.CreateHTTPDNSDomainRequest) (*pb.CreateHTTPDNSDomainResponse, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if req.AppDbId <= 0 || len(req.Domain) == 0 {
|
|
return nil, errors.New("required 'appDbId' and 'domain'")
|
|
}
|
|
var domainId int64
|
|
err = this.RunTx(func(tx *dbs.Tx) error {
|
|
domainId, err = models.SharedHTTPDNSDomainDAO.CreateDomain(tx, req.AppDbId, req.Domain, req.IsOn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return notifyHTTPDNSAppTasksByAppDbId(tx, req.AppDbId, models.HTTPDNSNodeTaskTypeDomainChanged)
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.CreateHTTPDNSDomainResponse{DomainId: domainId}, nil
|
|
}
|
|
|
|
func (this *HTTPDNSDomainService) DeleteHTTPDNSDomain(ctx context.Context, req *pb.DeleteHTTPDNSDomainRequest) (*pb.RPCSuccess, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = this.RunTx(func(tx *dbs.Tx) error {
|
|
domain, err := models.SharedHTTPDNSDomainDAO.FindEnabledDomain(tx, req.DomainId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if domain == nil {
|
|
return nil
|
|
}
|
|
|
|
err = models.SharedHTTPDNSDomainDAO.DisableDomain(tx, req.DomainId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return notifyHTTPDNSAppTasksByAppDbId(tx, int64(domain.AppId), models.HTTPDNSNodeTaskTypeDomainChanged)
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return this.Success()
|
|
}
|
|
|
|
func (this *HTTPDNSDomainService) UpdateHTTPDNSDomainStatus(ctx context.Context, req *pb.UpdateHTTPDNSDomainStatusRequest) (*pb.RPCSuccess, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = this.RunTx(func(tx *dbs.Tx) error {
|
|
domain, err := models.SharedHTTPDNSDomainDAO.FindEnabledDomain(tx, req.DomainId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if domain == nil {
|
|
return nil
|
|
}
|
|
|
|
err = models.SharedHTTPDNSDomainDAO.UpdateDomainStatus(tx, req.DomainId, req.IsOn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return notifyHTTPDNSAppTasksByAppDbId(tx, int64(domain.AppId), models.HTTPDNSNodeTaskTypeDomainChanged)
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return this.Success()
|
|
}
|
|
|
|
func (this *HTTPDNSDomainService) ListHTTPDNSDomainsWithAppId(ctx context.Context, req *pb.ListHTTPDNSDomainsWithAppIdRequest) (*pb.ListHTTPDNSDomainsWithAppIdResponse, error) {
|
|
_, _, validateErr := this.ValidateAdminAndUser(ctx, true)
|
|
if validateErr != nil {
|
|
if _, nodeErr := this.ValidateHTTPDNSNode(ctx); nodeErr != nil {
|
|
return nil, validateErr
|
|
}
|
|
}
|
|
domains, err := models.SharedHTTPDNSDomainDAO.ListEnabledDomainsWithAppId(this.NullTx(), req.AppDbId, req.Keyword)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var pbDomains []*pb.HTTPDNSDomain
|
|
for _, domain := range domains {
|
|
ruleCount, err := models.SharedHTTPDNSCustomRuleDAO.CountEnabledRulesWithDomainId(this.NullTx(), int64(domain.Id))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pbDomains = append(pbDomains, toPBDomain(domain, ruleCount))
|
|
}
|
|
return &pb.ListHTTPDNSDomainsWithAppIdResponse{Domains: pbDomains}, nil
|
|
}
|