Skip to content

Commit

Permalink
Merge pull request hybridgroup#45 from hybridgroup/api-host-port
Browse files Browse the repository at this point in the history
Add SSL and basic auth support in API
  • Loading branch information
zankich committed Apr 19, 2014
2 parents 5cd894f + 58530ee commit c394fa7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
41 changes: 37 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"))

Expand Down Expand Up @@ -85,7 +119,6 @@ func Api(bot *Master) *api {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
})

startApi(m)
return a
}

Expand Down
3 changes: 1 addition & 2 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion examples/hello_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions examples/hello_api_auth.go
Original file line number Diff line number Diff line change
@@ -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()
}
5 changes: 5 additions & 0 deletions master.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Master struct {
Robots []*Robot
NumCPU int
Api *api
}

func GobotMaster() *Master {
Expand All @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c394fa7

Please sign in to comment.