mirror of
https://github.com/yusing/godoxy.git
synced 2026-01-11 21:10:30 +01:00
fix(notif): format not being applied correctly
This commit is contained in:
@@ -10,10 +10,10 @@ import (
|
||||
)
|
||||
|
||||
type ProviderBase struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
URL string `json:"url" validate:"url"`
|
||||
Token string `json:"token"`
|
||||
Format *LogFormat `json:"format"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
URL string `json:"url" validate:"url"`
|
||||
Token string `json:"token"`
|
||||
Format LogFormat `json:"format"`
|
||||
}
|
||||
|
||||
type rawError []byte
|
||||
@@ -30,9 +30,19 @@ var (
|
||||
|
||||
// Validate implements the utils.CustomValidator interface.
|
||||
func (base *ProviderBase) Validate() gperr.Error {
|
||||
if base.Format == nil || base.Format.string == "" {
|
||||
switch base.Format {
|
||||
case "":
|
||||
base.Format = LogFormatMarkdown
|
||||
case LogFormatPlain, LogFormatMarkdown:
|
||||
default:
|
||||
return gperr.Multiline().
|
||||
Addf("invalid log format %s, supported formats:", base.Format).
|
||||
AddLines(
|
||||
LogFormatPlain,
|
||||
LogFormatMarkdown,
|
||||
)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(base.URL, "http://") && !strings.HasPrefix(base.URL, "https://") {
|
||||
return ErrURLMissingScheme
|
||||
}
|
||||
|
||||
@@ -13,11 +13,9 @@ type (
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
LogFormat struct {
|
||||
string
|
||||
}
|
||||
LogBody interface {
|
||||
Format(format *LogFormat) ([]byte, error)
|
||||
LogFormat string
|
||||
LogBody interface {
|
||||
Format(format LogFormat) ([]byte, error)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -30,10 +28,10 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
LogFormatMarkdown = &LogFormat{"markdown"}
|
||||
LogFormatPlain = &LogFormat{"plain"}
|
||||
LogFormatRawJSON = &LogFormat{"json"} // internal use only
|
||||
const (
|
||||
LogFormatMarkdown LogFormat = "markdown"
|
||||
LogFormatPlain LogFormat = "plain"
|
||||
LogFormatRawJSON LogFormat = "json" // internal use only
|
||||
)
|
||||
|
||||
func MakeLogFields(fields ...LogField) LogBody {
|
||||
@@ -44,28 +42,11 @@ func ErrorBody(err error) LogBody {
|
||||
return errorBody{Error: err}
|
||||
}
|
||||
|
||||
func (f *LogFormat) Parse(format string) error {
|
||||
switch format {
|
||||
case "":
|
||||
f.string = LogFormatMarkdown.string
|
||||
case LogFormatPlain.string, LogFormatMarkdown.string:
|
||||
f.string = format
|
||||
default:
|
||||
return gperr.Multiline().
|
||||
Addf("invalid log format %s, supported formats:", format).
|
||||
AddLines(
|
||||
LogFormatPlain,
|
||||
LogFormatMarkdown,
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FieldsBody) Add(name, value string) {
|
||||
*f = append(*f, LogField{Name: name, Value: value})
|
||||
}
|
||||
|
||||
func (f FieldsBody) Format(format *LogFormat) ([]byte, error) {
|
||||
func (f FieldsBody) Format(format LogFormat) ([]byte, error) {
|
||||
switch format {
|
||||
case LogFormatMarkdown:
|
||||
var msg bytes.Buffer
|
||||
@@ -92,7 +73,7 @@ func (f FieldsBody) Format(format *LogFormat) ([]byte, error) {
|
||||
return f.Format(LogFormatMarkdown)
|
||||
}
|
||||
|
||||
func (l ListBody) Format(format *LogFormat) ([]byte, error) {
|
||||
func (l ListBody) Format(format LogFormat) ([]byte, error) {
|
||||
switch format {
|
||||
case LogFormatPlain:
|
||||
return []byte(strings.Join(l, "\n")), nil
|
||||
@@ -110,7 +91,7 @@ func (l ListBody) Format(format *LogFormat) ([]byte, error) {
|
||||
return l.Format(LogFormatMarkdown)
|
||||
}
|
||||
|
||||
func (m MessageBody) Format(format *LogFormat) ([]byte, error) {
|
||||
func (m MessageBody) Format(format LogFormat) ([]byte, error) {
|
||||
switch format {
|
||||
case LogFormatPlain, LogFormatMarkdown:
|
||||
return []byte(m), nil
|
||||
@@ -120,7 +101,7 @@ func (m MessageBody) Format(format *LogFormat) ([]byte, error) {
|
||||
return m.Format(LogFormatMarkdown)
|
||||
}
|
||||
|
||||
func (e errorBody) Format(format *LogFormat) ([]byte, error) {
|
||||
func (e errorBody) Format(format LogFormat) ([]byte, error) {
|
||||
switch format {
|
||||
case LogFormatRawJSON:
|
||||
return sonic.Marshal(e.Error)
|
||||
|
||||
@@ -64,6 +64,9 @@ func (msg *LogMessage) notify(ctx context.Context, provider Provider) error {
|
||||
}
|
||||
provider.SetHeaders(msg, req.Header)
|
||||
|
||||
fmt.Println(req.Header)
|
||||
fmt.Println(string(body))
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user