From 189dddc3c0a1c4fa1e1b975f803ebf21c5fc609d Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 18 Apr 2014 14:39:17 -0700 Subject: [PATCH 1/6] WIP for API host/port params --- api.go | 10 +++++++--- api_test.go | 6 +++--- examples/hello_api.go | 3 ++- master.go | 5 +++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/api.go b/api.go index 0c52b7dfc..2314c4791 100644 --- a/api.go +++ b/api.go @@ -10,6 +10,7 @@ import ( type api struct { master *Master + server *martini.ClassicMartini } type jsonRobot struct { @@ -32,14 +33,17 @@ type jsonConnection struct { Adaptor string `json:"adaptor"` } -var startApi = func(m *martini.ClassicMartini) { - go m.Run() +func (me *api) StartApi() { + go me.server.Run() } func Api(bot *Master) *api { a := new(api) a.master = bot + bot.Api = a + m := martini.Classic() + a.server = m m.Use(martini.Static("robeaux")) @@ -85,7 +89,7 @@ func Api(bot *Master) *api { a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req) }) - startApi(m) + //startApi(m) return a } diff --git a/api_test.go b/api_test.go index 82d2fcae3..ff4569981 100644 --- a/api_test.go +++ b/api_test.go @@ -3,7 +3,7 @@ package gobot import ( "bytes" "encoding/json" - "github.com/go-martini/martini" + //"github.com/go-martini/martini" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "io/ioutil" @@ -19,8 +19,8 @@ var _ = Describe("Master", func() { ) BeforeEach(func() { - myMaster = GobotMaster() - startApi = func(m *martini.ClassicMartini) {} + myMaster = GobotMaster() + //startApi = func(m *martini.ClassicMartini) {} a = Api(myMaster) myMaster.Robots = []*Robot{ newTestRobot("Robot 1"), diff --git a/examples/hello_api.go b/examples/hello_api.go index 1301295a1..26ef608fa 100644 --- a/examples/hello_api.go +++ b/examples/hello_api.go @@ -12,7 +12,8 @@ func Hello(params map[string]interface{}) string { func main() { master := gobot.GobotMaster() - gobot.Api(master) + api := gobot.Api(master) + api.Port = "3000" hello := new(gobot.Robot) hello.Name = "hello" diff --git a/master.go b/master.go index 8f9cc395f..ec70511d6 100644 --- a/master.go +++ b/master.go @@ -9,6 +9,7 @@ import ( type Master struct { Robots []*Robot NumCPU int + Api *api } func GobotMaster() *Master { @@ -24,6 +25,10 @@ var trap = func(c chan os.Signal) { func (m *Master) Start() { runtime.GOMAXPROCS(m.NumCPU) + if m.Api != nil { + m.Api.StartApi() + } + for s := range m.Robots { m.Robots[s].startRobot() } From d1fc8438383c0e84969784a281ccd4f85e08453a Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Fri, 18 Apr 2014 14:45:50 -0700 Subject: [PATCH 2/6] Green tests --- api.go | 6 +++++- api_test.go | 5 ++--- master_test.go | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/api.go b/api.go index 2314c4791..3eadd74e8 100644 --- a/api.go +++ b/api.go @@ -33,10 +33,14 @@ type jsonConnection struct { Adaptor string `json:"adaptor"` } -func (me *api) StartApi() { +var startApi = func(me *api) { go me.server.Run() } +func (me *api) StartApi() { + startApi(me) +} + func Api(bot *Master) *api { a := new(api) a.master = bot diff --git a/api_test.go b/api_test.go index ff4569981..246d18811 100644 --- a/api_test.go +++ b/api_test.go @@ -3,7 +3,6 @@ package gobot import ( "bytes" "encoding/json" - //"github.com/go-martini/martini" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "io/ioutil" @@ -19,8 +18,8 @@ var _ = Describe("Master", func() { ) BeforeEach(func() { - myMaster = GobotMaster() - //startApi = func(m *martini.ClassicMartini) {} + myMaster = GobotMaster() + startApi = func(m *api) {} a = Api(myMaster) myMaster.Robots = []*Robot{ newTestRobot("Robot 1"), diff --git a/master_test.go b/master_test.go index c40277384..26c1c212b 100644 --- a/master_test.go +++ b/master_test.go @@ -18,6 +18,7 @@ var _ = Describe("Master", func() { newTestRobot("Robot 2"), newTestRobot("Robot 3"), } + startApi = func(m *api) {} trap = func(c chan os.Signal) { c <- os.Interrupt } From 0ee37bd33d1e4fa368816cb649d55267c54eaca1 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 18 Apr 2014 15:19:09 -0700 Subject: [PATCH 3/6] Allow user to set Host and Port when starting up Api --- api.go | 11 +++++++++-- examples/hello_api.go | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/api.go b/api.go index 3eadd74e8..14ebf1cd6 100644 --- a/api.go +++ b/api.go @@ -11,6 +11,8 @@ import ( type api struct { master *Master server *martini.ClassicMartini + Host string + Port string } type jsonRobot struct { @@ -34,7 +36,13 @@ type jsonConnection struct { } var startApi = func(me *api) { - go me.server.Run() + port := me.Port + if port == "" { + port = "3000" + } + + host := me.Host + go http.ListenAndServe(host+":"+port, me.server) } func (me *api) StartApi() { @@ -93,7 +101,6 @@ func Api(bot *Master) *api { a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req) }) - //startApi(m) return a } diff --git a/examples/hello_api.go b/examples/hello_api.go index 26ef608fa..73d1c6bcf 100644 --- a/examples/hello_api.go +++ b/examples/hello_api.go @@ -13,7 +13,7 @@ func Hello(params map[string]interface{}) string { func main() { master := gobot.GobotMaster() api := gobot.Api(master) - api.Port = "3000" + api.Port = "4000" hello := new(gobot.Robot) hello.Name = "hello" From ed2bdfb5108887936c8eb7f1fed56e3acb541929 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 18 Apr 2014 15:45:42 -0700 Subject: [PATCH 4/6] Add basic auth support to api --- api.go | 17 +++++++++++++---- examples/hello_api_auth.go | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 examples/hello_api_auth.go diff --git a/api.go b/api.go index 14ebf1cd6..26d44de39 100644 --- a/api.go +++ b/api.go @@ -3,16 +3,19 @@ package gobot import ( "encoding/json" "github.com/go-martini/martini" + "github.com/martini-contrib/auth" "io/ioutil" "net/http" "reflect" ) type api struct { - master *Master - server *martini.ClassicMartini - Host string - Port string + master *Master + server *martini.ClassicMartini + Host string + Port string + Username string + Password string } type jsonRobot struct { @@ -36,6 +39,12 @@ type jsonConnection struct { } var startApi = func(me *api) { + username := me.Username + if username != "" { + password := me.Password + me.server.Use(auth.Basic(username, password)) + } + port := me.Port if port == "" { port = "3000" diff --git a/examples/hello_api_auth.go b/examples/hello_api_auth.go new file mode 100644 index 000000000..5b0a3441e --- /dev/null +++ b/examples/hello_api_auth.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "github.com/hybridgroup/gobot" +) + +func Hello(params map[string]interface{}) string { + name := params["name"].(string) + return fmt.Sprintf("hi %v", name) +} + +func main() { + master := gobot.GobotMaster() + api := gobot.Api(master) + api.Username = "gort" + api.Password = "klatuu" + + hello := new(gobot.Robot) + hello.Name = "hello" + hello.Commands = map[string]interface{}{"Hello": Hello} + + master.Robots = append(master.Robots, hello) + + master.Start() +} From 1719a02370eb68dca1694dd224b6eb49c351a9ed Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 18 Apr 2014 16:04:49 -0700 Subject: [PATCH 5/6] SSL support in Api --- api.go | 23 ++++++++++++++++------- master.go | 4 ++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/api.go b/api.go index 26d44de39..3a9e6e2e2 100644 --- a/api.go +++ b/api.go @@ -10,12 +10,14 @@ import ( ) type api struct { - master *Master - server *martini.ClassicMartini - Host string - Port string - Username string - Password string + master *Master + server *martini.ClassicMartini + Host string + Port string + Username string + Password string + Cert string + Key string } type jsonRobot struct { @@ -51,7 +53,14 @@ var startApi = func(me *api) { } host := me.Host - go http.ListenAndServe(host+":"+port, me.server) + cert := me.Cert + key := me.Key + + if cert != "" && key != "" { + go http.ListenAndServeTLS(host+":"+port, cert, key, me.server) + } else { + go http.ListenAndServe(host+":"+port, me.server) + } } func (me *api) StartApi() { diff --git a/master.go b/master.go index ec70511d6..f77759172 100644 --- a/master.go +++ b/master.go @@ -9,7 +9,7 @@ import ( type Master struct { Robots []*Robot NumCPU int - Api *api + Api *api } func GobotMaster() *Master { @@ -26,7 +26,7 @@ func (m *Master) Start() { runtime.GOMAXPROCS(m.NumCPU) if m.Api != nil { - m.Api.StartApi() + m.Api.StartApi() } for s := range m.Robots { From 58530eefdd4494a08dfdd8bf4a4f124b5b44a664 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 18 Apr 2014 18:12:38 -0700 Subject: [PATCH 6/6] Change startApi to private function --- api.go | 2 +- master.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api.go b/api.go index 3a9e6e2e2..7906633db 100644 --- a/api.go +++ b/api.go @@ -63,7 +63,7 @@ var startApi = func(me *api) { } } -func (me *api) StartApi() { +func (me *api) startApi() { startApi(me) } diff --git a/master.go b/master.go index f77759172..6a78ea1fa 100644 --- a/master.go +++ b/master.go @@ -26,7 +26,7 @@ func (m *Master) Start() { runtime.GOMAXPROCS(m.NumCPU) if m.Api != nil { - m.Api.StartApi() + m.Api.startApi() } for s := range m.Robots {