Add basic auth support to api
This commit is contained in:
parent
0ee37bd33d
commit
ed2bdfb510
17
api.go
17
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"
|
||||
|
|
|
@ -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()
|
||||
}
|
Loading…
Reference in New Issue