refactor: clean code

This commit is contained in:
Fu Diwei 2025-04-26 21:27:53 +08:00
parent 3be70c3696
commit 609a252ee0
11 changed files with 49 additions and 67 deletions

2
go.mod
View File

@ -187,7 +187,7 @@ require (
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/nrdcg/namesilo v0.2.1 // indirect
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
github.com/pkg/errors v0.9.1
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/cobra v1.9.1 // indirect

View File

@ -32,6 +32,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
}, nil
}

View File

@ -30,6 +30,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
}, nil
}

View File

@ -44,6 +44,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
}, nil
}

View File

@ -9,25 +9,21 @@ import (
"log/slog"
"net/http"
"github.com/pkg/errors"
"github.com/usual2970/certimate/internal/pkg/core/notifier"
)
type NotifierConfig struct {
// Gotify 服务地址
// 示例https://gotify.example.com
// Gotify 服务地址。
Url string `json:"url"`
// Gotify Token
// Gotify Token
Token string `json:"token"`
// Gotify 消息优先级
Priority int64 `json:"priority"`
// Gotify 消息优先级
Priority int64 `json:"priority,omitempty"`
}
type NotifierProvider struct {
config *NotifierConfig
logger *slog.Logger
// 未来将移除
config *NotifierConfig
logger *slog.Logger
httpClient *http.Client
}
@ -40,6 +36,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
httpClient: http.DefaultClient,
}, nil
}
@ -54,7 +51,6 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier {
}
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
// Gotify 原生实现, notify 库没有实现, 等待合并
reqBody := &struct {
Title string `json:"title"`
Message string `json:"message"`
@ -65,10 +61,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
Priority: n.config.Priority,
}
// Make request
body, err := json.Marshal(reqBody)
if err != nil {
return nil, errors.Wrap(err, "encode message body")
return nil, fmt.Errorf("gotify api error: failed to encode message body: %w", err)
}
req, err := http.NewRequestWithContext(
@ -78,27 +73,24 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
bytes.NewReader(body),
)
if err != nil {
return nil, errors.Wrap(err, "create new request")
return nil, fmt.Errorf("gotify api error: failed to create new request: %w", err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", n.config.Token))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
// Send request to gotify service
resp, err := n.httpClient.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "send request to gotify server")
return nil, fmt.Errorf("gotify api error: failed to send request: %w", err)
}
defer resp.Body.Close()
// Read response and verify success
result, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "read response")
return nil, fmt.Errorf("gotify api error: failed to read response: %w", err)
} else if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("gotify api error: unexpected status code: %d, resp: %s", resp.StatusCode, string(result))
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("gotify returned status code %d: %s", resp.StatusCode, string(result))
}
return &notifier.NotifyResult{}, nil
}

View File

@ -28,6 +28,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
}, nil
}

View File

@ -38,6 +38,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
}, nil
}

View File

@ -9,20 +9,19 @@ import (
"log/slog"
"net/http"
"github.com/pkg/errors"
"github.com/usual2970/certimate/internal/pkg/core/notifier"
)
type NotifierConfig struct {
Token string `json:"token"` // 应用 API Token
User string `json:"user"` // 用户/分组 Key
// Pushover API Token。
Token string `json:"token"`
// 用户或分组标识。
User string `json:"user"`
}
type NotifierProvider struct {
config *NotifierConfig
logger *slog.Logger
// 未来将移除
config *NotifierConfig
logger *slog.Logger
httpClient *http.Client
}
@ -35,6 +34,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
httpClient: http.DefaultClient,
}, nil
}
@ -48,10 +48,8 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier {
return n
}
// Notify 发送通知
// 参考文档https://pushover.net/api
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
// 请求体
// REF: https://pushover.net/api
reqBody := &struct {
Token string `json:"token"`
User string `json:"user"`
@ -64,10 +62,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
Message: message,
}
// Make request
body, err := json.Marshal(reqBody)
if err != nil {
return nil, errors.Wrap(err, "encode message body")
return nil, fmt.Errorf("pushover api error: failed to encode message body: %w", err)
}
req, err := http.NewRequestWithContext(
@ -77,25 +74,22 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
bytes.NewReader(body),
)
if err != nil {
return nil, errors.Wrap(err, "create new request")
return nil, fmt.Errorf("pushover api error: failed to create new request: %w", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
// Send request to pushover service
resp, err := n.httpClient.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "send request to pushover server")
return nil, fmt.Errorf("pushover api error: failed to send request: %w", err)
}
defer resp.Body.Close()
result, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "read response")
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("pushover returned status code %d: %s", resp.StatusCode, string(result))
return nil, fmt.Errorf("pushover api error: failed to read response: %w", err)
} else if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("pushover api error: unexpected status code: %d, resp: %s", resp.StatusCode, string(result))
}
return &notifier.NotifyResult{}, nil

View File

@ -9,20 +9,17 @@ import (
"log/slog"
"net/http"
"github.com/pkg/errors"
"github.com/usual2970/certimate/internal/pkg/core/notifier"
)
type NotifierConfig struct {
// PushPlus Token
// PushPlus Token
Token string `json:"token"`
}
type NotifierProvider struct {
config *NotifierConfig
logger *slog.Logger
// 未来将移除
config *NotifierConfig
logger *slog.Logger
httpClient *http.Client
}
@ -35,6 +32,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
httpClient: http.DefaultClient,
}, nil
}
@ -48,10 +46,8 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier {
return n
}
// Notify 发送通知
// 参考文档https://pushplus.plus/doc/guide/api.html
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
// 请求体
// REF: https://pushplus.plus/doc/guide/api.html
reqBody := &struct {
Token string `json:"token"`
Title string `json:"title"`
@ -62,10 +58,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
Content: message,
}
// Make request
body, err := json.Marshal(reqBody)
if err != nil {
return nil, errors.Wrap(err, "encode message body")
return nil, fmt.Errorf("pushplus api error: failed to encode message body: %w", err)
}
req, err := http.NewRequestWithContext(
@ -75,38 +70,32 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
bytes.NewReader(body),
)
if err != nil {
return nil, errors.Wrap(err, "create new request")
return nil, fmt.Errorf("pushplus api error: failed to create new request: %w", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
// Send request to pushplus service
resp, err := n.httpClient.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "send request to pushplus server")
return nil, fmt.Errorf("pushplus api error: failed to send request: %w", err)
}
defer resp.Body.Close()
result, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "read response")
return nil, fmt.Errorf("pushplus api error: failed to read response: %w", err)
} else if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("pushplus api error: unexpected status code: %d, resp: %s", resp.StatusCode, string(result))
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("pushplus returned status code %d: %s", resp.StatusCode, string(result))
}
// 解析响应
var errorResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
if err := json.Unmarshal(result, &errorResponse); err != nil {
return nil, errors.Wrap(err, "decode response")
}
if errorResponse.Code != 200 {
return nil, fmt.Errorf("pushplus returned error: %s", errorResponse.Msg)
return nil, fmt.Errorf("pushplus api error: failed to decode response: %w", err)
} else if errorResponse.Code != 200 {
return nil, fmt.Errorf("pushplus api error: unexpected response code: %d, msg: %s", errorResponse.Code, errorResponse.Msg)
}
return &notifier.NotifyResult{}, nil

View File

@ -29,6 +29,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
}, nil
}

View File

@ -30,6 +30,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
return &NotifierProvider{
config: config,
logger: slog.Default(),
}, nil
}