diff options
author | Chris <ccbrown112@gmail.com> | 2017-11-02 16:23:41 -0500 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2017-11-02 17:23:41 -0400 |
commit | fed5324ce505c3725472af23757cd6659bb7139c (patch) | |
tree | c2ad489c627d99e30ed55e25a9f0058a7068a13a /plugin/example_hooks_test.go | |
parent | 8e81ded9ba425971bafdb8ad6bbe4b23c7420335 (diff) | |
download | chat-fed5324ce505c3725472af23757cd6659bb7139c.tar.gz chat-fed5324ce505c3725472af23757cd6659bb7139c.tar.bz2 chat-fed5324ce505c3725472af23757cd6659bb7139c.zip |
Add some plugin docs (#7757)
* add some plugin docs
* one more comment
* spacing
* example simplification
Diffstat (limited to 'plugin/example_hooks_test.go')
-rw-r--r-- | plugin/example_hooks_test.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/plugin/example_hooks_test.go b/plugin/example_hooks_test.go new file mode 100644 index 000000000..7ca1cf5f0 --- /dev/null +++ b/plugin/example_hooks_test.go @@ -0,0 +1,38 @@ +package plugin_test + +import ( + "fmt" + "net/http" + + "github.com/mattermost/mattermost-server/plugin" + "github.com/mattermost/mattermost-server/plugin/rpcplugin" +) + +type MyPlugin struct { + api plugin.API +} + +func (p *MyPlugin) OnActivate(api plugin.API) { + // Just save api for later when we need to look up users. + p.api = api +} + +func (p *MyPlugin) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if userId := r.Header.Get("Mattermost-User-Id"); userId == "" { + // Our visitor is unauthenticated. + fmt.Fprintf(w, "Hello, stranger!") + } else if user, err := p.api.GetUser(userId); err == nil { + // Greet the user by name! + fmt.Fprintf(w, "Welcome back, %v!", user.Username) + } else { + // This won't happen in normal circumstances, but let's just be safe. + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, err.Error()) + } +} + +// This example demonstrates a plugin that handles HTTP requests which respond by greeting the user +// by name. +func Example_plugin() { + rpcplugin.Main(&MyPlugin{}) +} |