mirror of https://github.com/usual2970/certimate
				
				
				
			refactor: clean code
							parent
							
								
									8af5235e4d
								
							
						
					
					
						commit
						2a68372713
					
				| 
						 | 
				
			
			@ -28,9 +28,9 @@ type applyConfig struct {
 | 
			
		|||
	DisableFollowCNAME bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ApplyResult struct {
 | 
			
		||||
	PrivateKey        string
 | 
			
		||||
type ApplyCertResult struct {
 | 
			
		||||
	Certificate       string
 | 
			
		||||
	PrivateKey        string
 | 
			
		||||
	IssuerCertificate string
 | 
			
		||||
	ACMECertUrl       string
 | 
			
		||||
	ACMECertStableUrl string
 | 
			
		||||
| 
						 | 
				
			
			@ -38,13 +38,15 @@ type ApplyResult struct {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
type applicant interface {
 | 
			
		||||
	Apply() (*ApplyResult, error)
 | 
			
		||||
	Apply() (*ApplyCertResult, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewWithApplyNode(node *domain.WorkflowNode) (applicant, error) {
 | 
			
		||||
	// 获取授权配置
 | 
			
		||||
	accessRepo := repository.NewAccessRepository()
 | 
			
		||||
	if node.Type != domain.WorkflowNodeTypeApply {
 | 
			
		||||
		return nil, fmt.Errorf("node type is not apply")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	accessRepo := repository.NewAccessRepository()
 | 
			
		||||
	access, err := accessRepo.GetById(context.Background(), node.GetConfigString("providerAccessId"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("access record not found: %w", err)
 | 
			
		||||
| 
						 | 
				
			
			@ -71,7 +73,7 @@ func NewWithApplyNode(node *domain.WorkflowNode) (applicant, error) {
 | 
			
		|||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func apply(challengeProvider challenge.Provider, applyConfig *applyConfig) (*ApplyResult, error) {
 | 
			
		||||
func apply(challengeProvider challenge.Provider, applyConfig *applyConfig) (*ApplyCertResult, error) {
 | 
			
		||||
	record, _ := app.GetApp().Dao().FindFirstRecordByFilter("settings", "name='sslProvider'")
 | 
			
		||||
 | 
			
		||||
	sslProvider := &acmeSSLProviderConfig{
 | 
			
		||||
| 
						 | 
				
			
			@ -131,7 +133,7 @@ func apply(challengeProvider challenge.Provider, applyConfig *applyConfig) (*App
 | 
			
		|||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &ApplyResult{
 | 
			
		||||
	return &ApplyCertResult{
 | 
			
		||||
		PrivateKey:        string(certificates.PrivateKey),
 | 
			
		||||
		Certificate:       string(certificates.Certificate),
 | 
			
		||||
		IssuerCertificate: string(certificates.IssuerCertificate),
 | 
			
		||||
| 
						 | 
				
			
			@ -166,6 +168,6 @@ type proxyApplicant struct {
 | 
			
		|||
	applyConfig *applyConfig
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *proxyApplicant) Apply() (*ApplyResult, error) {
 | 
			
		||||
func (d *proxyApplicant) Apply() (*ApplyCertResult, error) {
 | 
			
		||||
	return apply(d.applicant, d.applyConfig)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,47 +2,55 @@ package deployer
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"github.com/usual2970/certimate/internal/applicant"
 | 
			
		||||
	"github.com/usual2970/certimate/internal/domain"
 | 
			
		||||
	"github.com/usual2970/certimate/internal/pkg/core/deployer"
 | 
			
		||||
	"github.com/usual2970/certimate/internal/pkg/core/logger"
 | 
			
		||||
	"github.com/usual2970/certimate/internal/repository"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DeployerOption struct {
 | 
			
		||||
	NodeId       string                `json:"nodeId"`
 | 
			
		||||
	Domains      string                `json:"domains"`
 | 
			
		||||
	AccessConfig string                `json:"accessConfig"`
 | 
			
		||||
	AccessRecord *domain.Access        `json:"-"`
 | 
			
		||||
	DeployConfig domain.DeployConfig   `json:"deployConfig"`
 | 
			
		||||
	Certificate  applicant.ApplyResult `json:"certificate"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Deployer interface {
 | 
			
		||||
	Deploy(ctx context.Context) error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewWithProviderAndOption(provider string, option *DeployerOption) (Deployer, error) {
 | 
			
		||||
	deployer, logger, err := createDeployer(domain.DeployProviderType(provider), option.AccessRecord.Config, option.DeployConfig.NodeConfig)
 | 
			
		||||
func NewWithDeployNode(node *domain.WorkflowNode, certdata struct {
 | 
			
		||||
	Certificate string
 | 
			
		||||
	PrivateKey  string
 | 
			
		||||
},
 | 
			
		||||
) (Deployer, error) {
 | 
			
		||||
	if node.Type != domain.WorkflowNodeTypeApply {
 | 
			
		||||
		return nil, fmt.Errorf("node type is not deploy")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	accessRepo := repository.NewAccessRepository()
 | 
			
		||||
	access, err := accessRepo.GetById(context.Background(), node.GetConfigString("providerAccessId"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("access record not found: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	deployer, logger, err := createDeployer(domain.DeployProviderType(node.GetConfigString("provider")), access.Config, node.Config)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &proxyDeployer{
 | 
			
		||||
		option:   option,
 | 
			
		||||
		logger:   logger,
 | 
			
		||||
		deployer: deployer,
 | 
			
		||||
		logger:            logger,
 | 
			
		||||
		deployer:          deployer,
 | 
			
		||||
		deployCertificate: certdata.Certificate,
 | 
			
		||||
		deployPrivateKey:  certdata.PrivateKey,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO: 暂时使用代理模式以兼容之前版本代码,后续重新实现此处逻辑
 | 
			
		||||
type proxyDeployer struct {
 | 
			
		||||
	option   *DeployerOption
 | 
			
		||||
	logger   logger.Logger
 | 
			
		||||
	deployer deployer.Deployer
 | 
			
		||||
	logger            logger.Logger
 | 
			
		||||
	deployer          deployer.Deployer
 | 
			
		||||
	deployCertificate string
 | 
			
		||||
	deployPrivateKey  string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *proxyDeployer) Deploy(ctx context.Context) error {
 | 
			
		||||
	_, err := d.deployer.Deploy(ctx, d.option.Certificate.Certificate, d.option.Certificate.PrivateKey)
 | 
			
		||||
	_, err := d.deployer.Deploy(ctx, d.deployCertificate, d.deployPrivateKey)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +0,0 @@
 | 
			
		|||
package domain
 | 
			
		||||
 | 
			
		||||
// Deprecated: TODO: 即将废弃
 | 
			
		||||
type DeployConfig struct {
 | 
			
		||||
	NodeId           string         `json:"nodeId"`
 | 
			
		||||
	NodeConfig       map[string]any `json:"nodeConfig"`
 | 
			
		||||
	Provider         string         `json:"provider"`
 | 
			
		||||
	ProviderAccessId string         `json:"providerAccessId"`
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -5,7 +5,6 @@ import (
 | 
			
		|||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/usual2970/certimate/internal/applicant"
 | 
			
		||||
	"github.com/usual2970/certimate/internal/deployer"
 | 
			
		||||
	"github.com/usual2970/certimate/internal/domain"
 | 
			
		||||
	"github.com/usual2970/certimate/internal/repository"
 | 
			
		||||
| 
						 | 
				
			
			@ -58,34 +57,10 @@ func (d *deployNode) Run(ctx context.Context) error {
 | 
			
		|||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	accessRepo := repository.NewAccessRepository()
 | 
			
		||||
	access, err := accessRepo.GetById(context.Background(), d.node.GetConfigString("providerAccessId"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		d.AddOutput(ctx, d.node.Name, "获取授权配置失败", err.Error())
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	option := &deployer.DeployerOption{
 | 
			
		||||
		NodeId:       d.node.Id,
 | 
			
		||||
		Domains:      cert.SubjectAltNames,
 | 
			
		||||
		AccessConfig: access.Config,
 | 
			
		||||
		AccessRecord: access,
 | 
			
		||||
		Certificate: applicant.ApplyResult{
 | 
			
		||||
			ACMECertUrl:       cert.ACMECertUrl,
 | 
			
		||||
			ACMECertStableUrl: cert.ACMECertStableUrl,
 | 
			
		||||
			PrivateKey:        cert.PrivateKey,
 | 
			
		||||
			Certificate:       cert.Certificate,
 | 
			
		||||
			IssuerCertificate: cert.IssuerCertificate,
 | 
			
		||||
		},
 | 
			
		||||
		DeployConfig: domain.DeployConfig{
 | 
			
		||||
			NodeId:           d.node.Id,
 | 
			
		||||
			NodeConfig:       d.node.Config,
 | 
			
		||||
			Provider:         d.node.GetConfigString("provider"),
 | 
			
		||||
			ProviderAccessId: access.Id,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	deploy, err := deployer.NewWithProviderAndOption(d.node.GetConfigString("provider"), option)
 | 
			
		||||
	deploy, err := deployer.NewWithDeployNode(d.node, struct {
 | 
			
		||||
		Certificate string
 | 
			
		||||
		PrivateKey  string
 | 
			
		||||
	}{Certificate: cert.Certificate, PrivateKey: cert.PrivateKey})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		d.AddOutput(ctx, d.node.Name, "获取部署对象失败", err.Error())
 | 
			
		||||
		return err
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -154,6 +154,7 @@ const DeployNodeForm = ({ node }: DeployFormProps) => {
 | 
			
		|||
  const handleProviderSelect = (value: string) => {
 | 
			
		||||
    if (fieldProvider === value) return;
 | 
			
		||||
 | 
			
		||||
    // TODO: 暂时不支持切换部署目标,需后端调整,否则之前若存在部署结果输出就不会再部署
 | 
			
		||||
    // 切换部署目标时重置表单,避免其他部署目标的配置字段影响当前部署目标
 | 
			
		||||
    if (node.config?.provider === value) {
 | 
			
		||||
      formInst.resetFields();
 | 
			
		||||
| 
						 | 
				
			
			@ -179,7 +180,13 @@ const DeployNodeForm = ({ node }: DeployFormProps) => {
 | 
			
		|||
    <Form {...formProps} form={formInst} disabled={formPending} layout="vertical">
 | 
			
		||||
      <Show when={!!fieldProvider} fallback={<DeployProviderPicker onSelect={handleProviderPick} />}>
 | 
			
		||||
        <Form.Item name="provider" label={t("workflow_node.deploy.form.provider.label")} rules={[formRule]}>
 | 
			
		||||
          <DeployProviderSelect allowClear placeholder={t("workflow_node.deploy.form.provider.placeholder")} showSearch onSelect={handleProviderSelect} />
 | 
			
		||||
          <DeployProviderSelect
 | 
			
		||||
            allowClear
 | 
			
		||||
            disabled
 | 
			
		||||
            placeholder={t("workflow_node.deploy.form.provider.placeholder")}
 | 
			
		||||
            showSearch
 | 
			
		||||
            onSelect={handleProviderSelect}
 | 
			
		||||
          />
 | 
			
		||||
        </Form.Item>
 | 
			
		||||
 | 
			
		||||
        <Form.Item className="mb-0">
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue