WIP for API host/port params

This commit is contained in:
deadprogram 2014-04-18 14:39:17 -07:00
parent 5cd894fdcd
commit 189dddc3c0
4 changed files with 17 additions and 7 deletions

10
api.go
View File

@ -10,6 +10,7 @@ import (
type api struct { type api struct {
master *Master master *Master
server *martini.ClassicMartini
} }
type jsonRobot struct { type jsonRobot struct {
@ -32,14 +33,17 @@ type jsonConnection struct {
Adaptor string `json:"adaptor"` Adaptor string `json:"adaptor"`
} }
var startApi = func(m *martini.ClassicMartini) { func (me *api) StartApi() {
go m.Run() go me.server.Run()
} }
func Api(bot *Master) *api { func Api(bot *Master) *api {
a := new(api) a := new(api)
a.master = bot a.master = bot
bot.Api = a
m := martini.Classic() m := martini.Classic()
a.server = m
m.Use(martini.Static("robeaux")) m.Use(martini.Static("robeaux"))
@ -85,7 +89,7 @@ func Api(bot *Master) *api {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req) a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
}) })
startApi(m) //startApi(m)
return a return a
} }

View File

@ -3,7 +3,7 @@ package gobot
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"github.com/go-martini/martini" //"github.com/go-martini/martini"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"io/ioutil" "io/ioutil"
@ -20,7 +20,7 @@ var _ = Describe("Master", func() {
BeforeEach(func() { BeforeEach(func() {
myMaster = GobotMaster() myMaster = GobotMaster()
startApi = func(m *martini.ClassicMartini) {} //startApi = func(m *martini.ClassicMartini) {}
a = Api(myMaster) a = Api(myMaster)
myMaster.Robots = []*Robot{ myMaster.Robots = []*Robot{
newTestRobot("Robot 1"), newTestRobot("Robot 1"),

View File

@ -12,7 +12,8 @@ func Hello(params map[string]interface{}) string {
func main() { func main() {
master := gobot.GobotMaster() master := gobot.GobotMaster()
gobot.Api(master) api := gobot.Api(master)
api.Port = "3000"
hello := new(gobot.Robot) hello := new(gobot.Robot)
hello.Name = "hello" hello.Name = "hello"

View File

@ -9,6 +9,7 @@ import (
type Master struct { type Master struct {
Robots []*Robot Robots []*Robot
NumCPU int NumCPU int
Api *api
} }
func GobotMaster() *Master { func GobotMaster() *Master {
@ -24,6 +25,10 @@ var trap = func(c chan os.Signal) {
func (m *Master) Start() { func (m *Master) Start() {
runtime.GOMAXPROCS(m.NumCPU) runtime.GOMAXPROCS(m.NumCPU)
if m.Api != nil {
m.Api.StartApi()
}
for s := range m.Robots { for s := range m.Robots {
m.Robots[s].startRobot() m.Robots[s].startRobot()
} }