From 4ac3618f7ef0e79e0ffcb885943956409975d8bd Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Sun, 15 Jun 2025 22:17:49 +0800 Subject: [PATCH] refactor: abstractions --- internal/pkg/core/core.go | 9 +++++++++ internal/pkg/core/notifier.go | 27 +++++++++++++++++++++++++++ internal/pkg/core/ssl_deployer.go | 27 +++++++++++++++++++++++++++ internal/pkg/core/ssl_manager.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 internal/pkg/core/core.go create mode 100644 internal/pkg/core/notifier.go create mode 100644 internal/pkg/core/ssl_deployer.go create mode 100644 internal/pkg/core/ssl_manager.go diff --git a/internal/pkg/core/core.go b/internal/pkg/core/core.go new file mode 100644 index 00000000..e83a1900 --- /dev/null +++ b/internal/pkg/core/core.go @@ -0,0 +1,9 @@ +package core + +import ( + "log/slog" +) + +type WithLogger interface { + SetLogger(logger *slog.Logger) +} diff --git a/internal/pkg/core/notifier.go b/internal/pkg/core/notifier.go new file mode 100644 index 00000000..4e1dbe62 --- /dev/null +++ b/internal/pkg/core/notifier.go @@ -0,0 +1,27 @@ +package core + +import ( + "context" +) + +// 表示定义消息通知器的抽象类型接口。 +type Notifier interface { + WithLogger + + // 发送通知。 + // + // 入参: + // - ctx:上下文。 + // - subject:通知主题。 + // - message:通知内容。 + // + // 出参: + // - res:发送结果。 + // - err: 错误。 + Notify(ctx context.Context, subject string, message string) (_res *NotifyResult, _err error) +} + +// 表示通知发送结果的数据结构。 +type NotifyResult struct { + ExtendedData map[string]any `json:"extendedData,omitempty"` +} diff --git a/internal/pkg/core/ssl_deployer.go b/internal/pkg/core/ssl_deployer.go new file mode 100644 index 00000000..7f6d79ea --- /dev/null +++ b/internal/pkg/core/ssl_deployer.go @@ -0,0 +1,27 @@ +package core + +import ( + "context" +) + +// 表示定义 SSL 证书部署器的抽象类型接口。 +type SSLDeployer interface { + WithLogger + + // 部署证书。 + // + // 入参: + // - ctx:上下文。 + // - certPEM:证书 PEM 内容。 + // - privkeyPEM:私钥 PEM 内容。 + // + // 出参: + // - res:部署结果。 + // - err: 错误。 + Deploy(ctx context.Context, certPEM string, privkeyPEM string) (_res *SSLDeployResult, _err error) +} + +// 表示 SSL 证书部署结果的数据结构。 +type SSLDeployResult struct { + ExtendedData map[string]any `json:"extendedData,omitempty"` +} diff --git a/internal/pkg/core/ssl_manager.go b/internal/pkg/core/ssl_manager.go new file mode 100644 index 00000000..837b62c7 --- /dev/null +++ b/internal/pkg/core/ssl_manager.go @@ -0,0 +1,30 @@ +package core + +import ( + "context" +) + +// 表示定义 SSL 证书管理器的抽象类型接口。 +// 云服务商通常会提供 SSL 证书管理服务,可供用户集中管理证书。 +type SSLManager interface { + WithLogger + + // 上传证书。 + // + // 入参: + // - ctx:上下文。 + // - certPEM:证书 PEM 内容。 + // - privkeyPEM:私钥 PEM 内容。 + // + // 出参: + // - res:上传结果。 + // - err: 错误。 + Upload(ctx context.Context, certPEM string, privkeyPEM string) (_res *SSLUploadResult, _err error) +} + +// 表示证书上传结果的数据结构,包含上传后的证书 ID、名称和其他数据。 +type SSLUploadResult struct { + CertId string `json:"certId"` + CertName string `json:"certName,omitempty"` + ExtendedData map[string]any `json:"extendedData,omitempty"` +}