From 4cf3b3ebb67d07427e68c7fe68f24400b84479ac Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Tue, 22 Apr 2014 20:48:08 -0700 Subject: [PATCH] Add cors support --- api.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/api.go b/api.go index 4c75176b9..98c68b389 100644 --- a/api.go +++ b/api.go @@ -4,10 +4,11 @@ import ( "encoding/json" "github.com/go-martini/martini" "github.com/martini-contrib/auth" + "github.com/martini-contrib/cors" "io/ioutil" + "log" "net/http" "reflect" - "log" ) type api struct { @@ -57,7 +58,7 @@ var startApi = func(me *api) { cert := me.Cert key := me.Key - log.Println("Initializing API on "+host+":"+port+"...") + log.Println("Initializing API on " + host + ":" + port + "...") if cert != "" && key != "" { go http.ListenAndServeTLS(host+":"+port, cert, key, me.server) } else { @@ -79,6 +80,9 @@ func Api(bot *Master) *api { a.server = m m.Use(martini.Static("robeaux")) + m.Use(cors.Allow(&cors.Options{ + AllowAllOrigins: true, + })) m.Get("/robots", func(res http.ResponseWriter, req *http.Request) { a.robots(res, req) @@ -247,7 +251,7 @@ func (a *api) executeCommand(robotname string, devicename string, commandname st func (a *api) executeRobotCommand(robotname string, commandname string, res http.ResponseWriter, req *http.Request) { data, _ := ioutil.ReadAll(req.Body) - var body map[string]interface{} + body := make(map[string]interface{}) json.Unmarshal(data, &body) robot := a.master.FindRobot(robotname) in := make([]reflect.Value, 1)