From d2cff9b77cca87c339e65591179a2d7456a6915a Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 3 Nov 2017 11:34:44 -0500 Subject: more plugin doc updates (#7767) --- plugin/example_hello_user_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 plugin/example_hello_user_test.go (limited to 'plugin/example_hello_user_test.go') diff --git a/plugin/example_hello_user_test.go b/plugin/example_hello_user_test.go new file mode 100644 index 000000000..4aefbc5f5 --- /dev/null +++ b/plugin/example_hello_user_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 HelloUserPlugin struct { + api plugin.API +} + +func (p *HelloUserPlugin) OnActivate(api plugin.API) { + // Just save api for later when we need to look up users. + p.api = api +} + +func (p *HelloUserPlugin) 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_helloUser() { + rpcplugin.Main(&HelloUserPlugin{}) +} -- cgit v1.2.3-1-g7c22