~gjabell/mfn

8feee3ab1d3e5088983792519859a414b068ee28 — Galen Abell 1 year, 2 months ago 60b7af8
Refactor for multiple configs, centralize test
4 files changed, 117 insertions(+), 46 deletions(-)

M config.go
M main.go
M notify.go
M webhook.go
M config.go => config.go +85 -17
@@ 7,14 7,83 @@ import (
	"github.com/BurntSushi/toml"
)

type Config struct {
	Server             string             `toml:"server"`
type EmailConfig struct {
	SubjectTemplate    *template.Template `toml:"-"`
	SubjectTemplateStr string             `toml:"subject_template"`
	BodyTemplate       *template.Template `toml:"-"`
	BodyTemplateStr    string             `toml:"body_template"`
	EmailTo            string             `toml:"to"`
	EmailFrom          string             `toml:"from"`
	Username           string             `toml:"username"`
	Password           string             `toml:"password"`
	DbPath             string             `toml:"db_path"`
	WebhookTemplateStr string             `toml:"webhook_template"`
	WebhookTemplate    *template.Template `toml:"-"`
	WebhookURL         string             `toml:"webhook_url"`
	Server             string             `toml:"server"`
}

func (c *EmailConfig) validate() error {
	for k, v := range map[string]string{
		"subject template": c.SubjectTemplateStr,
		"body template":    c.BodyTemplateStr,
		"\"to\" email":     c.EmailTo,
		"\"from\" email":   c.EmailFrom,
		"username":         c.Username,
		"password":         c.Password,
		"server":           c.Server,
	} {
		if v == "" {
			return fmt.Errorf("you must provide a valid %s", k)
		}
	}

	// parse the templates
	subjTmpl, err := template.New("subject_template").Parse(c.SubjectTemplateStr)
	if err != nil {
		return err
	}
	c.SubjectTemplate = subjTmpl

	bodyTmpl, err := template.New("body_template").Parse(c.BodyTemplateStr)
	if err != nil {
		return err
	}
	c.BodyTemplate = bodyTmpl

	return nil
}

type WebhookConfig struct {
	Template    *template.Template `toml:"-"`
	TemplateStr string             `toml:"template"`
	Endpoint    string             `toml:"endpoint"`
}

func (c *WebhookConfig) validate() error {
	for k, v := range map[string]string{
		"template": c.TemplateStr,
		"endpoint": c.Endpoint,
	} {
		if v == "" {
			return fmt.Errorf("you must provide a valid %s", k)
		}
	}

	// parse the template
	tmpl, err := template.New("webhook").Parse(c.TemplateStr)
	if err != nil {
		return err
	}
	c.Template = tmpl

	return nil
}

type Config struct {
	Server   string `toml:"server"`
	Username string `toml:"username"`
	Password string `toml:"password"`
	DbPath   string `toml:"db_path"`

	EmailConfig   EmailConfig   `toml:"email"`
	WebHookConfig WebhookConfig `toml:"webhook"`
}

func LoadConfig(configPath string) (*Config, error) {


@@ 33,24 102,23 @@ func LoadConfig(configPath string) (*Config, error) {

func (c *Config) validate() error {
	for k, v := range map[string]string{
		"server url":       c.Server,
		"username":         c.Username,
		"password":         c.Password,
		"database path":    c.DbPath,
		"webhook template": c.WebhookTemplateStr,
		"webhook url":      c.WebhookURL,
		"server url":    c.Server,
		"username":      c.Username,
		"password":      c.Password,
		"database path": c.DbPath,
	} {
		if v == "" {
			return fmt.Errorf("you must provide a valid %s", k)
		}
	}

	// parse the template
	tmpl, err := template.New("webhook").Parse(c.WebhookTemplateStr)
	if err != nil {
		return err
	if err := c.EmailConfig.validate(); err != nil {
		return fmt.Errorf("invalid email config: %s", err)
	}

	if err := c.WebHookConfig.validate(); err != nil {
		return fmt.Errorf("invalid webhook config: %s", err)
	}
	c.WebhookTemplate = tmpl

	return nil
}

M main.go => main.go +3 -2
@@ 91,10 91,11 @@ func main() {
			log.Fatalf("Invalid notifier: %s\n", testNotifier)
		}

		log.Println("Sending test notification...")
		if err := notifier.Test(); err != nil {
		log.Printf("Sending test %s notification...\n", testNotifier)
		if err := Test(notifier); err != nil {
			log.Fatalf("Test notification failed: %s\n", err)
		}
		log.Printf("Test %s notification sent!\n", testNotifier)
		os.Exit(0)
	}


M notify.go => notify.go +25 -2
@@ 1,8 1,31 @@
package main

import mf "miniflux.app/client"
import (
	"time"

	mf "miniflux.app/client"
)

type Notifier interface {
	Notify(*mf.Entry) error
	Test() error
}

func Test(n Notifier) error {
	e := &mf.Entry{
		ID:         0,
		UserID:     0,
		FeedID:     0,
		Status:     "unread",
		Hash:       "deadbeef",
		Title:      "Test Entry",
		URL:        "https://miniflux.app",
		Date:       time.Now(),
		Content:    "This is a test miniflux entry",
		Author:     "Author",
		Starred:    false,
		Enclosures: make([]*mf.Enclosure, 0),
		Feed:       nil,
	}

	return n.Notify(e)
}

M webhook.go => webhook.go +4 -25
@@ 7,7 7,6 @@ import (
	"io/ioutil"
	"log"
	"net/http"
	"time"

	mf "miniflux.app/client"
)


@@ 15,13 14,13 @@ import (
type Webhook struct {
	// Notifier
	template *template.Template
	url      string
	endpoint string
}

func NewWebhook(conf *Config) *Webhook {
	return &Webhook{
		template: conf.WebhookTemplate,
		url:      conf.WebhookURL,
		template: conf.WebHookConfig.Template,
		endpoint: conf.WebHookConfig.Endpoint,
	}
}



@@ 37,7 36,7 @@ func (w *Webhook) Notify(entry *mf.Entry) error {
		return err
	}

	res, err := http.Post(w.url, "application/json", bytes.NewBuffer(jsonVal))
	res, err := http.Post(w.endpoint, "application/json", bytes.NewBuffer(jsonVal))
	if err != nil {
		return err
	}


@@ 49,23 48,3 @@ func (w *Webhook) Notify(entry *mf.Entry) error {

	return nil
}

func (w *Webhook) Test() error {
	e := &mf.Entry{
		ID:         0,
		UserID:     0,
		FeedID:     0,
		Status:     "unread",
		Hash:       "deadbeef",
		Title:      "Test Entry",
		URL:        "https://miniflux.app",
		Date:       time.Now(),
		Content:    "This is a test miniflux entry",
		Author:     "Author",
		Starred:    false,
		Enclosures: make([]*mf.Enclosure, 0),
		Feed:       nil,
	}

	return w.Notify(e)
}