mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-09 05:59:50 +00:00
refactor: clean code
This commit is contained in:
parent
551b06b4e8
commit
94579d65c4
@ -193,19 +193,3 @@ func getMailNotifier(conf map[string]any) (notifyPackage.Notifier, error) {
|
||||
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
func getString(conf map[string]any, key string) string {
|
||||
if _, ok := conf[key]; !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return conf[key].(string)
|
||||
}
|
||||
|
||||
func getBool(conf map[string]any, key string) bool {
|
||||
if _, ok := conf[key]; !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return conf[key].(bool)
|
||||
}
|
||||
|
17
internal/notify/utils.go
Normal file
17
internal/notify/utils.go
Normal file
@ -0,0 +1,17 @@
|
||||
package notify
|
||||
|
||||
func getString(conf map[string]any, key string) string {
|
||||
if _, ok := conf[key]; !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return conf[key].(string)
|
||||
}
|
||||
|
||||
func getBool(conf map[string]any, key string) bool {
|
||||
if _, ok := conf[key]; !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return conf[key].(bool)
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
notifyHttp "github.com/nikoksr/notify/service/http"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type ServerChanNotifierConfig struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type ServerChanNotifier struct {
|
||||
config *ServerChanNotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*ServerChanNotifier)(nil)
|
||||
|
||||
func New(config *ServerChanNotifierConfig) (*ServerChanNotifier, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("config is nil")
|
||||
}
|
||||
|
||||
return &ServerChanNotifier{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *ServerChanNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
srv := notifyHttp.New()
|
||||
|
||||
srv.AddReceivers(¬ifyHttp.Webhook{
|
||||
URL: n.config.Url,
|
||||
Header: http.Header{},
|
||||
ContentType: "application/json",
|
||||
Method: http.MethodPost,
|
||||
BuildPayload: func(subject, message string) (payload any) {
|
||||
return map[string]string{
|
||||
"text": subject,
|
||||
"desp": message,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
err = srv.Send(ctx, subject, message)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ¬ifier.NotifyResult{}, nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user