2014-04-30 04:20:32 +08:00
|
|
|
package api
|
2013-11-24 02:36:08 +08:00
|
|
|
|
2013-11-24 08:19:11 +08:00
|
|
|
import (
|
2014-06-12 06:37:49 +08:00
|
|
|
"crypto/subtle"
|
|
|
|
"encoding/base64"
|
2013-11-24 08:19:11 +08:00
|
|
|
"encoding/json"
|
2014-04-23 11:48:08 +08:00
|
|
|
"log"
|
2013-11-24 08:19:11 +08:00
|
|
|
"net/http"
|
2014-07-02 14:10:12 +08:00
|
|
|
"strings"
|
2014-07-11 02:35:00 +08:00
|
|
|
|
|
|
|
"github.com/bmizerany/pat"
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
"github.com/hybridgroup/gobot/api/robeaux"
|
2013-11-24 08:19:11 +08:00
|
|
|
)
|
2013-11-24 02:36:08 +08:00
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
// Optional restful API through Gobot has access
|
2014-04-27 00:13:33 +08:00
|
|
|
// all the robots.
|
|
|
|
type api struct {
|
2014-04-30 04:20:32 +08:00
|
|
|
gobot *gobot.Gobot
|
2014-06-12 06:37:49 +08:00
|
|
|
server *pat.PatternServeMux
|
2014-04-30 04:20:32 +08:00
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Cert string
|
|
|
|
Key string
|
2014-06-12 06:37:49 +08:00
|
|
|
Debug bool
|
2014-04-30 04:20:32 +08:00
|
|
|
start func(*api)
|
2014-01-28 05:15:52 +08:00
|
|
|
}
|
|
|
|
|
2014-06-11 06:16:11 +08:00
|
|
|
func NewAPI(g *gobot.Gobot) *api {
|
2014-04-30 04:20:32 +08:00
|
|
|
return &api{
|
2014-05-16 02:50:45 +08:00
|
|
|
gobot: g,
|
2014-07-22 12:14:00 +08:00
|
|
|
Port: "3000",
|
2014-05-16 02:50:45 +08:00
|
|
|
start: func(a *api) {
|
2014-07-22 12:14:00 +08:00
|
|
|
log.Println("Initializing API on " + a.Host + ":" + a.Port + "...")
|
2014-06-12 06:37:49 +08:00
|
|
|
http.Handle("/", a.server)
|
2014-07-02 14:10:12 +08:00
|
|
|
|
2014-04-30 04:20:32 +08:00
|
|
|
go func() {
|
2014-07-22 12:14:00 +08:00
|
|
|
if a.Cert != "" && a.Key != "" {
|
|
|
|
http.ListenAndServeTLS(a.Host+":"+a.Port, a.Cert, a.Key, nil)
|
2014-04-30 04:20:32 +08:00
|
|
|
} else {
|
2014-07-22 12:14:00 +08:00
|
|
|
log.Println("WARNING: API using insecure connection. " +
|
|
|
|
"We recommend using an SSL certificate with Gobot.")
|
|
|
|
http.ListenAndServe(a.Host+":"+a.Port, nil)
|
2014-04-30 04:20:32 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
},
|
2014-04-19 06:19:09 +08:00
|
|
|
}
|
2014-04-27 00:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// start starts the api using the start function
|
|
|
|
// sets on the API on initialization.
|
2014-04-30 04:20:32 +08:00
|
|
|
func (a *api) Start() {
|
2014-06-12 06:37:49 +08:00
|
|
|
a.server = pat.New()
|
2013-11-28 12:05:45 +08:00
|
|
|
|
2014-07-22 12:14:00 +08:00
|
|
|
mcpCommandRoute := "/commands/:command"
|
2014-06-13 11:58:54 +08:00
|
|
|
deviceCommandRoute := "/robots/:robot/devices/:device/commands/:command"
|
2014-06-12 06:37:49 +08:00
|
|
|
robotCommandRoute := "/robots/:robot/commands/:command"
|
2013-11-28 12:05:45 +08:00
|
|
|
|
2014-07-22 12:14:00 +08:00
|
|
|
a.server.Get("/", a.setHeaders(a.mcp))
|
|
|
|
a.server.Get("/commands", a.setHeaders(a.mcpCommands))
|
|
|
|
a.server.Get(mcpCommandRoute, a.setHeaders(a.executeMcpCommand))
|
|
|
|
a.server.Post(mcpCommandRoute, a.setHeaders(a.executeMcpCommand))
|
2014-06-12 06:37:49 +08:00
|
|
|
a.server.Get("/robots", a.setHeaders(a.robots))
|
|
|
|
a.server.Get("/robots/:robot", a.setHeaders(a.robot))
|
|
|
|
a.server.Get("/robots/:robot/commands", a.setHeaders(a.robotCommands))
|
|
|
|
a.server.Get(robotCommandRoute, a.setHeaders(a.executeRobotCommand))
|
|
|
|
a.server.Post(robotCommandRoute, a.setHeaders(a.executeRobotCommand))
|
|
|
|
a.server.Get("/robots/:robot/devices", a.setHeaders(a.robotDevices))
|
|
|
|
a.server.Get("/robots/:robot/devices/:device", a.setHeaders(a.robotDevice))
|
2014-07-22 12:14:00 +08:00
|
|
|
a.server.Get("/robots/:robot/devices/:device/commands",
|
|
|
|
a.setHeaders(a.robotDeviceCommands),
|
|
|
|
)
|
2014-06-13 11:58:54 +08:00
|
|
|
a.server.Get(deviceCommandRoute, a.setHeaders(a.executeDeviceCommand))
|
|
|
|
a.server.Post(deviceCommandRoute, a.setHeaders(a.executeDeviceCommand))
|
2014-06-12 06:37:49 +08:00
|
|
|
a.server.Get("/robots/:robot/connections", a.setHeaders(a.robotConnections))
|
2014-07-22 12:14:00 +08:00
|
|
|
a.server.Get("/robots/:robot/connections/:connection",
|
|
|
|
a.setHeaders(a.robotConnection),
|
|
|
|
)
|
2014-07-02 14:10:12 +08:00
|
|
|
a.server.Get("/:a", a.setHeaders(a.robeaux))
|
|
|
|
a.server.Get("/:a/", a.setHeaders(a.robeaux))
|
|
|
|
a.server.Get("/:a/:b", a.setHeaders(a.robeaux))
|
|
|
|
a.server.Get("/:a/:b/", a.setHeaders(a.robeaux))
|
|
|
|
a.server.Get("/:a/:b/:c", a.setHeaders(a.robeaux))
|
|
|
|
a.server.Get("/:a/:b/:c/", a.setHeaders(a.robeaux))
|
2013-11-24 08:19:11 +08:00
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
a.start(a)
|
|
|
|
}
|
2014-04-19 13:44:50 +08:00
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) setHeaders(f func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|
|
|
return func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
if a.Debug {
|
|
|
|
log.Println(req)
|
|
|
|
}
|
|
|
|
if a.Username != "" {
|
|
|
|
if !a.basicAuth(res, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(res, req)
|
|
|
|
}
|
2014-04-16 08:59:44 +08:00
|
|
|
}
|
|
|
|
|
2014-07-02 14:10:12 +08:00
|
|
|
func (a *api) robeaux(res http.ResponseWriter, req *http.Request) {
|
|
|
|
path := req.URL.Path
|
2014-07-11 02:35:00 +08:00
|
|
|
buf, err := robeaux.Asset(path[1:])
|
2014-07-02 14:10:12 +08:00
|
|
|
if err != nil {
|
2014-07-22 12:14:00 +08:00
|
|
|
http.Error(res, err.Error(), http.StatusNotFound)
|
2014-07-02 14:10:12 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
t := strings.Split(path, ".")
|
|
|
|
if t[len(t)-1] == "js" {
|
|
|
|
res.Header().Set("Content-Type", "text/javascript; charset=utf-8")
|
|
|
|
} else if t[len(t)-1] == "css" {
|
|
|
|
res.Header().Set("Content-Type", "text/css; charset=utf-8")
|
|
|
|
}
|
|
|
|
res.Write(buf)
|
|
|
|
}
|
|
|
|
|
2014-07-22 12:14:00 +08:00
|
|
|
func (a *api) mcp(res http.ResponseWriter, req *http.Request) {
|
2014-06-13 11:58:54 +08:00
|
|
|
data, _ := json.Marshal(a.gobot.ToJSON())
|
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-07-22 12:14:00 +08:00
|
|
|
func (a *api) mcpCommands(res http.ResponseWriter, req *http.Request) {
|
2014-06-13 11:58:54 +08:00
|
|
|
data, _ := json.Marshal(a.gobot.ToJSON().Commands)
|
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:50:45 +08:00
|
|
|
func (a *api) robots(res http.ResponseWriter, req *http.Request) {
|
2014-06-11 06:16:11 +08:00
|
|
|
jsonRobots := []*gobot.JSONRobot{}
|
2014-07-03 09:08:44 +08:00
|
|
|
a.gobot.Robots().Each(func(r *gobot.Robot) {
|
|
|
|
jsonRobots = append(jsonRobots, r.ToJSON())
|
|
|
|
})
|
2014-04-16 08:59:44 +08:00
|
|
|
data, _ := json.Marshal(jsonRobots)
|
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) robot(res http.ResponseWriter, req *http.Request) {
|
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
|
|
|
|
data, _ := json.Marshal(a.gobot.Robot(robot).ToJSON())
|
2014-04-16 08:59:44 +08:00
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) robotCommands(res http.ResponseWriter, req *http.Request) {
|
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
|
2014-06-12 07:44:23 +08:00
|
|
|
data, _ := json.Marshal(a.gobot.Robot(robot).ToJSON().Commands)
|
2014-04-16 08:59:44 +08:00
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) robotDevices(res http.ResponseWriter, req *http.Request) {
|
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
|
2014-06-11 06:16:11 +08:00
|
|
|
jsonDevices := []*gobot.JSONDevice{}
|
2014-07-03 09:08:44 +08:00
|
|
|
a.gobot.Robot(robot).Devices().Each(func(d gobot.Device) {
|
|
|
|
jsonDevices = append(jsonDevices, d.ToJSON())
|
|
|
|
})
|
2014-04-16 08:59:44 +08:00
|
|
|
data, _ := json.Marshal(jsonDevices)
|
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) robotDevice(res http.ResponseWriter, req *http.Request) {
|
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
device := req.URL.Query().Get(":device")
|
|
|
|
|
2014-06-11 06:16:11 +08:00
|
|
|
data, _ := json.Marshal(a.gobot.Robot(robot).Device(device).ToJSON())
|
2014-04-16 08:59:44 +08:00
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
|
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
device := req.URL.Query().Get(":device")
|
|
|
|
|
2014-06-12 08:41:04 +08:00
|
|
|
data, _ := json.Marshal(a.gobot.Robot(robot).Device(device).ToJSON().Commands)
|
2014-04-16 08:59:44 +08:00
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
2013-11-24 02:36:08 +08:00
|
|
|
}
|
2013-11-25 07:47:48 +08:00
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) robotConnections(res http.ResponseWriter, req *http.Request) {
|
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
|
2014-06-11 06:16:11 +08:00
|
|
|
jsonConnections := []*gobot.JSONConnection{}
|
2014-07-03 09:08:44 +08:00
|
|
|
a.gobot.Robot(robot).Connections().Each(func(c gobot.Connection) {
|
|
|
|
jsonConnections = append(jsonConnections, c.ToJSON())
|
|
|
|
})
|
2014-04-19 13:44:50 +08:00
|
|
|
data, _ := json.Marshal(jsonConnections)
|
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) robotConnection(res http.ResponseWriter, req *http.Request) {
|
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
connection := req.URL.Query().Get(":connection")
|
|
|
|
|
2014-06-11 06:16:11 +08:00
|
|
|
data, _ := json.Marshal(a.gobot.Robot(robot).Connection(connection).ToJSON())
|
2014-04-19 13:44:50 +08:00
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-07-22 12:14:00 +08:00
|
|
|
func (a *api) executeMcpCommand(res http.ResponseWriter, req *http.Request) {
|
|
|
|
var data []byte
|
|
|
|
var body map[string]interface{}
|
2014-06-13 11:58:54 +08:00
|
|
|
command := req.URL.Query().Get(":command")
|
|
|
|
|
2014-07-22 12:14:00 +08:00
|
|
|
json.NewDecoder(req.Body).Decode(&body)
|
2014-07-04 10:14:04 +08:00
|
|
|
f := a.gobot.Command(command)
|
2014-06-13 11:58:54 +08:00
|
|
|
|
|
|
|
if f != nil {
|
|
|
|
data, _ = json.Marshal(f(body))
|
|
|
|
} else {
|
|
|
|
data, _ = json.Marshal("Unknown Command")
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-06-12 08:41:04 +08:00
|
|
|
func (a *api) executeDeviceCommand(res http.ResponseWriter, req *http.Request) {
|
2014-07-22 12:14:00 +08:00
|
|
|
var body map[string]interface{}
|
|
|
|
var data []byte
|
2014-06-12 06:37:49 +08:00
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
device := req.URL.Query().Get(":device")
|
|
|
|
command := req.URL.Query().Get(":command")
|
|
|
|
|
2014-07-22 12:14:00 +08:00
|
|
|
json.NewDecoder(req.Body).Decode(&body)
|
2014-06-12 06:37:49 +08:00
|
|
|
d := a.gobot.Robot(robot).Device(device)
|
2014-06-12 08:41:04 +08:00
|
|
|
body["robot"] = robot
|
2014-07-04 10:14:04 +08:00
|
|
|
f := d.Command(command)
|
2014-06-12 08:41:04 +08:00
|
|
|
|
|
|
|
if f != nil {
|
|
|
|
data, _ = json.Marshal(f(body))
|
|
|
|
} else {
|
|
|
|
data, _ = json.Marshal("Unknown Command")
|
2013-11-25 07:47:48 +08:00
|
|
|
}
|
2014-06-12 08:41:04 +08:00
|
|
|
|
2014-04-16 08:59:44 +08:00
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
2013-11-25 07:47:48 +08:00
|
|
|
}
|
2013-11-28 12:05:45 +08:00
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
func (a *api) executeRobotCommand(res http.ResponseWriter, req *http.Request) {
|
2014-07-22 12:14:00 +08:00
|
|
|
var data []byte
|
|
|
|
var body map[string]interface{}
|
|
|
|
|
2014-06-12 06:37:49 +08:00
|
|
|
robot := req.URL.Query().Get(":robot")
|
|
|
|
command := req.URL.Query().Get(":command")
|
|
|
|
|
2014-07-22 12:14:00 +08:00
|
|
|
json.NewDecoder(req.Body).Decode(&body)
|
2014-06-12 06:37:49 +08:00
|
|
|
r := a.gobot.Robot(robot)
|
|
|
|
body["robot"] = robot
|
2014-07-04 10:14:04 +08:00
|
|
|
f := r.Command(command)
|
2014-06-12 07:44:23 +08:00
|
|
|
|
|
|
|
if f != nil {
|
|
|
|
data, _ = json.Marshal(f(body))
|
2014-04-16 10:19:14 +08:00
|
|
|
} else {
|
2014-06-12 07:44:23 +08:00
|
|
|
data, _ = json.Marshal("Unknown Command")
|
2013-11-28 12:05:45 +08:00
|
|
|
}
|
2014-06-12 07:44:23 +08:00
|
|
|
|
2014-04-16 10:19:14 +08:00
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
res.Write(data)
|
2013-11-28 12:05:45 +08:00
|
|
|
}
|
2014-07-22 12:14:00 +08:00
|
|
|
|
|
|
|
// basic auth inspired by
|
|
|
|
// https://github.com/codegangsta/martini-contrib/blob/master/auth/
|
|
|
|
func (a *api) basicAuth(res http.ResponseWriter, req *http.Request) bool {
|
|
|
|
auth := req.Header.Get("Authorization")
|
|
|
|
if !a.secureCompare(auth,
|
|
|
|
"Basic "+base64.StdEncoding.EncodeToString([]byte(a.Username+":"+a.Password)),
|
|
|
|
) {
|
|
|
|
res.Header().Set("WWW-Authenticate",
|
|
|
|
"Basic realm=\"Authorization Required\"",
|
|
|
|
)
|
|
|
|
http.Error(res, "Not Authorized", http.StatusUnauthorized)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) secureCompare(given string, actual string) bool {
|
|
|
|
if subtle.ConstantTimeEq(int32(len(given)), int32(len(actual))) == 1 {
|
|
|
|
return subtle.ConstantTimeCompare([]byte(given), []byte(actual)) == 1
|
|
|
|
}
|
|
|
|
// Securely compare actual to itself to keep constant time,
|
|
|
|
// but always return false
|
|
|
|
return subtle.ConstantTimeCompare([]byte(actual), []byte(actual)) == 1 && false
|
|
|
|
}
|