Initial commit from mattermost-plugin-starter-template
This commit is contained in:
commit
acbc69f7eb
57 changed files with 27772 additions and 0 deletions
42
server/api.go
Normal file
42
server/api.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
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 <siteUrl>/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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue