diff --git a/api.go b/api.go index 0c52b7dfc..7906633db 100644 --- a/api.go +++ b/api.go @@ -3,13 +3,21 @@ 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 + master *Master + server *martini.ClassicMartini + Host string + Port string + Username string + Password string + Cert string + Key string } type jsonRobot struct { @@ -32,14 +40,40 @@ type jsonConnection struct { Adaptor string `json:"adaptor"` } -var startApi = func(m *martini.ClassicMartini) { - go m.Run() +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" + } + + host := me.Host + 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() { + startApi(me) } 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 +119,6 @@ func Api(bot *Master) *api { a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req) }) - startApi(m) return a } diff --git a/api_test.go b/api_test.go index 82d2fcae3..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" @@ -20,7 +19,7 @@ var _ = Describe("Master", func() { BeforeEach(func() { myMaster = GobotMaster() - startApi = func(m *martini.ClassicMartini) {} + startApi = func(m *api) {} a = Api(myMaster) myMaster.Robots = []*Robot{ newTestRobot("Robot 1"), diff --git a/examples/hello_api.go b/examples/hello_api.go index 1301295a1..73d1c6bcf 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 = "4000" hello := new(gobot.Robot) hello.Name = "hello" 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() +} diff --git a/master.go b/master.go index 8f9cc395f..6a78ea1fa 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() } 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 }