Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
SDI-1659: support for CORS header
Browse files Browse the repository at this point in the history
  • Loading branch information
candysmurf committed Feb 7, 2017
1 parent 791370a commit 73f756f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions mgmt/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
"github.com/urfave/negroni"

"github.com/intelsdi-x/snap/mgmt/rest/api"
Expand Down Expand Up @@ -92,6 +93,15 @@ func New(cfg *Config) (*Server, error) {
negroni.HandlerFunc(s.authMiddleware),
)
s.r = httprouter.New()

// Deals with CORS
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET, POST, DELETE, PUT, PATCH, OPTIONS"},
AllowedHeaders: []string{"Origin, X-Requested-With, Content-Type, Accept"},
})
s.n.Use(c)

// Use negroni to handle routes
s.n.UseHandler(s.r)
return s, nil
Expand Down Expand Up @@ -133,6 +143,11 @@ func (s *Server) SetAPIAuthPwd(pwd string) {

// Auth Middleware for REST API
func (s *Server) authMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
// Deals with CORS
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, OPTIONS")
rw.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

defer r.Body.Close()
if s.auth {
_, password, ok := r.BasicAuth()
Expand Down

0 comments on commit 73f756f

Please sign in to comment.