项目作者: kamikazechaser

项目描述 :
Standalone server for sending out notifications with pluggable service providers
高级语言: Go
项目地址: git://github.com/kamikazechaser/hedwig.git
创建时间: 2021-07-28T18:33:14Z
项目社区:https://github.com/kamikazechaser/hedwig

开源协议:The Unlicense

下载


Hedwig

Standalone server for sending out notifications with pluggable service providers


Go Report Card
license: Unlicense

Background

The current design is based on Go plugins and follow a specific plugin spec. The compiled plugins are loaded during runtime. Any plugin specific configuration is baked into the plugin itself.

Hedwig exposes a protected HTTP endpoint to enqueue incoming messages which are required to follow a common message spec.

Status: This project is currently undergoing development. Expect breaking changes (especially to plugins).

Features

  • Service providers are loaded as compiled Go plugins
  • Underlying resiliancy and persistance provided by asynq
  • Scheduled notifications
  • Queue from any source that talks HTTP

Plugins

A sample plugin would look like:

  1. package main
  2. import (
  3. "context"
  4. "time"
  5. "github.com/kamikazechaser/hedwig/internal/message"
  6. "github.com/mailgun/mailgun-go/v4"
  7. )
  8. const (
  9. pluginName string = "mailgun"
  10. )
  11. type client struct {
  12. mailgunClient *mailgun.MailgunImpl
  13. }
  14. func New() (interface{}, error) {
  15. mg := mailgun.NewMailgun("", "")
  16. return &client{
  17. mailgunClient: mg,
  18. }, nil
  19. }
  20. func (c *client) PluginName() string {
  21. return pluginName
  22. }
  23. func (c *client) HealthCheck() bool {
  24. // check if you have enough credits e.t.c.
  25. // TODO: not implemnted in Hedwig yet
  26. return true
  27. }
  28. func (c *client) Push(msg message.Message) error {
  29. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  30. defer cancel()
  31. mail := c.mailgunClient.NewMessage("Domain <noreply@your.domain>", msg.Title, "", msg.To)
  32. mail.SetTemplate("master")
  33. // msg.Params for additional properties that you want to include
  34. mail.AddVariable("link", msg.Params[0])
  35. mail.AddVariable("action", msg.Params[1])
  36. mail.AddVariable("message", msg.Content)
  37. _, _, err := c.mailgunClient.Send(ctx, mail)
  38. if err != nil {
  39. // return errors only if you want to keep retrying
  40. // return nil in cases where the service is blocked by the receipient e.t.c.
  41. return err
  42. }
  43. return nil
  44. }

Usage

prerequisites

  • Go 1.6
  • Make
  • Redis server (add to config.json)
  1. $ cp config.example.json config.json
  2. # Edit config.json
  3. $ make build
  4. $ ./hedwig
  5. # Hedwig will look for plugins in the root folder and load them at run time

Queue notifications by sending a request in the format:

Request:

  1. > POST /push?key=test HTTP/1.1
  2. > Host: localhost:3000
  3. > Content-Type: application/json
  4. > Accept: */*
  5. | {
  6. | "to": "135207785",
  7. | "service": "telegram",
  8. | "title": "hello from hedwig",
  9. | "content": "test123",
  10. | "delay": 1,
  11. | }

Response:

  1. < HTTP/1.1 200 OK
  2. < Content-Type: application/json; charset=utf-8
  3. | {
  4. | "message": "6c9d0793-41e8-438c-9181-170af6a6da7f",
  5. | "ok": true
  6. | }