|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/url" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/gotify/plugin-api" |
| 11 | +) |
| 12 | + |
| 13 | +// GetGotifyPluginInfo returns gotify plugin info. |
| 14 | +func GetGotifyPluginInfo() plugin.Info { |
| 15 | + return plugin.Info{ |
| 16 | + ModulePath: "github.com/gotify/server/v2/plugin/example/echo", |
| 17 | + Name: "test plugin", |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// EchoPlugin is the gotify plugin instance. |
| 22 | +type EchoPlugin struct { |
| 23 | + msgHandler plugin.MessageHandler |
| 24 | + storageHandler plugin.StorageHandler |
| 25 | + config *Config |
| 26 | + basePath string |
| 27 | +} |
| 28 | + |
| 29 | +// SetStorageHandler implements plugin.Storager |
| 30 | +func (c *EchoPlugin) SetStorageHandler(h plugin.StorageHandler) { |
| 31 | + c.storageHandler = h |
| 32 | +} |
| 33 | + |
| 34 | +// SetMessageHandler implements plugin.Messenger. |
| 35 | +func (c *EchoPlugin) SetMessageHandler(h plugin.MessageHandler) { |
| 36 | + c.msgHandler = h |
| 37 | +} |
| 38 | + |
| 39 | +// Storage defines the plugin storage scheme |
| 40 | +type Storage struct { |
| 41 | + CalledTimes int `json:"called_times"` |
| 42 | +} |
| 43 | + |
| 44 | +// Config defines the plugin config scheme |
| 45 | +type Config struct { |
| 46 | + MagicString string `yaml:"magic_string"` |
| 47 | +} |
| 48 | + |
| 49 | +// DefaultConfig implements plugin.Configurer |
| 50 | +func (c *EchoPlugin) DefaultConfig() interface{} { |
| 51 | + return &Config{ |
| 52 | + MagicString: "hello world", |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// ValidateAndSetConfig implements plugin.Configurer |
| 57 | +func (c *EchoPlugin) ValidateAndSetConfig(config interface{}) error { |
| 58 | + c.config = config.(*Config) |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// Enable enables the plugin. |
| 63 | +func (c *EchoPlugin) Enable() error { |
| 64 | + log.Println("echo plugin enabled") |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// Disable disables the plugin. |
| 69 | +func (c *EchoPlugin) Disable() error { |
| 70 | + log.Println("echo plugin disbled") |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +// RegisterWebhook implements plugin.Webhooker. |
| 75 | +func (c *EchoPlugin) RegisterWebhook(baseURL string, g *gin.RouterGroup) { |
| 76 | + c.basePath = baseURL |
| 77 | + g.GET("/echo", func(ctx *gin.Context) { |
| 78 | + |
| 79 | + storage, _ := c.storageHandler.Load() |
| 80 | + conf := new(Storage) |
| 81 | + json.Unmarshal(storage, conf) |
| 82 | + conf.CalledTimes++ |
| 83 | + newStorage, _ := json.Marshal(conf) |
| 84 | + c.storageHandler.Save(newStorage) |
| 85 | + |
| 86 | + c.msgHandler.SendMessage(plugin.Message{ |
| 87 | + Title: "Hello received", |
| 88 | + Message: fmt.Sprintf("echo server received a hello message %d times", conf.CalledTimes), |
| 89 | + Priority: 2, |
| 90 | + Extras: map[string]any{ |
| 91 | + "plugin::name": "echo", |
| 92 | + }, |
| 93 | + }) |
| 94 | + ctx.Writer.WriteString(fmt.Sprintf("Magic string is: %s\r\nEcho server running at %secho", c.config.MagicString, c.basePath)) |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +// GetDisplay implements plugin.Displayer. |
| 99 | +func (c *EchoPlugin) GetDisplay(location *url.URL) string { |
| 100 | + loc := &url.URL{ |
| 101 | + Path: c.basePath, |
| 102 | + } |
| 103 | + if location != nil { |
| 104 | + loc.Scheme = location.Scheme |
| 105 | + loc.Host = location.Host |
| 106 | + } |
| 107 | + loc = loc.ResolveReference(&url.URL{ |
| 108 | + Path: "echo", |
| 109 | + }) |
| 110 | + return "Echo plugin running at: " + loc.String() |
| 111 | +} |
| 112 | + |
| 113 | +// NewGotifyPluginInstance creates a plugin instance for a user context. |
| 114 | +func NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin { |
| 115 | + return &EchoPlugin{} |
| 116 | +} |
| 117 | + |
| 118 | +func main() { |
| 119 | + panic("this should be built as go plugin") |
| 120 | +} |
0 commit comments