package main import ( "net/http" "github.com/gorilla/mux" "github.com/mattermost/mattermost/server/public/plugin" ) // ServeHTTP demonstrates a plugin that handles HTTP requests by greeting the world. // The root URL is currently /plugins/com.mattermost.bridge-xmpp/api/v1/. Replace com.mattermost.bridge-xmpp with the plugin ID. func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) { router := mux.NewRouter() // Middleware to require that the user is logged in router.Use(p.MattermostAuthorizationRequired) apiRouter := router.PathPrefix("/api/v1").Subrouter() apiRouter.HandleFunc("/hello", p.HelloWorld).Methods(http.MethodGet) router.ServeHTTP(w, r) } func (p *Plugin) MattermostAuthorizationRequired(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { userID := r.Header.Get("Mattermost-User-ID") if userID == "" { http.Error(w, "Not authorized", http.StatusUnauthorized) return } next.ServeHTTP(w, r) }) } func (p *Plugin) HelloWorld(w http.ResponseWriter, r *http.Request) { if _, err := w.Write([]byte("Hello, world!")); err != nil { p.API.LogError("Failed to write response", "error", err) http.Error(w, err.Error(), http.StatusInternalServerError) } }