Add basic auth support to api

This commit is contained in:
deadprogram 2014-04-18 15:45:42 -07:00
parent 0ee37bd33d
commit ed2bdfb510
2 changed files with 39 additions and 4 deletions

17
api.go
View File

@ -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"

View File

@ -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()
}