mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-20 00:25:02 +01:00
This is a large-scale refactoring across the codebase that replaces the custom `gperr.Error` type with Go's standard `error` interface. The changes include: - Replacing `gperr.Error` return types with `error` in function signatures - Using `errors.New()` and `fmt.Errorf()` instead of `gperr.New()` and `gperr.Errorf()` - Using `%w` format verb for error wrapping instead of `.With()` method - Replacing `gperr.Subject()` calls with `gperr.PrependSubject()` - Converting error logging from `gperr.Log*()` functions to zerolog's `.Err().Msg()` pattern - Update NewLogger to handle multiline error message - Updating `goutils` submodule to latest commit This refactoring aligns with Go idioms and removes the dependency on custom error handling abstractions in favor of standard library patterns.
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package notif
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/rs/zerolog"
|
|
gperr "github.com/yusing/goutils/errs"
|
|
)
|
|
|
|
// See https://docs.ntfy.sh/publish
|
|
type Ntfy struct {
|
|
ProviderBase
|
|
Topic string `json:"topic"`
|
|
}
|
|
|
|
// Validate implements the utils.CustomValidator interface.
|
|
func (n *Ntfy) Validate() error {
|
|
var errs gperr.Builder
|
|
if err := n.ProviderBase.Validate(); err != nil {
|
|
errs.Add(err)
|
|
}
|
|
if n.URL == "" {
|
|
errs.Adds("url is required")
|
|
}
|
|
if n.Topic == "" {
|
|
errs.Adds("topic is required")
|
|
}
|
|
if n.Topic != "" && n.Topic[0] == '/' {
|
|
errs.Adds("topic should not start with a slash")
|
|
}
|
|
return errs.Error()
|
|
}
|
|
|
|
// GetURL implements Provider.
|
|
func (n *Ntfy) GetURL() string {
|
|
if n.URL[len(n.URL)-1] == '/' {
|
|
return n.URL + n.Topic
|
|
}
|
|
return n.URL + "/" + n.Topic
|
|
}
|
|
|
|
// GetMIMEType implements Provider.
|
|
func (n *Ntfy) GetMIMEType() string {
|
|
return ""
|
|
}
|
|
|
|
// GetToken implements Provider.
|
|
func (n *Ntfy) GetToken() string {
|
|
return n.Token
|
|
}
|
|
|
|
// MarshalMessage implements Provider.
|
|
func (n *Ntfy) MarshalMessage(logMsg *LogMessage) ([]byte, error) {
|
|
return logMsg.Body.Format(n.Format)
|
|
}
|
|
|
|
// SetHeaders implements Provider.
|
|
func (n *Ntfy) SetHeaders(logMsg *LogMessage, headers http.Header) {
|
|
headers.Set("Title", logMsg.Title)
|
|
|
|
switch logMsg.Level {
|
|
// warning (or other unspecified) uses default priority
|
|
case zerolog.FatalLevel:
|
|
headers.Set("Priority", "urgent")
|
|
case zerolog.ErrorLevel:
|
|
headers.Set("Priority", "high")
|
|
case zerolog.InfoLevel:
|
|
headers.Set("Priority", "low")
|
|
case zerolog.DebugLevel:
|
|
headers.Set("Priority", "min")
|
|
}
|
|
|
|
if n.Format == LogFormatMarkdown {
|
|
headers.Set("Markdown", "yes")
|
|
}
|
|
}
|