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