1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00

290 lines
8.5 KiB
Go
Raw Normal View History

2014-04-29 13:20:32 -07:00
package api
2013-11-23 10:36:08 -08:00
2013-11-23 16:19:11 -08:00
import (
"crypto/subtle"
"encoding/base64"
2013-11-23 16:19:11 -08:00
"encoding/json"
2014-04-22 20:48:08 -07:00
"log"
2013-11-23 16:19:11 -08:00
"net/http"
2014-07-01 23:10:12 -07:00
"strings"
2014-07-10 11:35:00 -07:00
"github.com/bmizerany/pat"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/api/robeaux"
2013-11-23 16:19:11 -08:00
)
2013-11-23 10:36:08 -08:00
2014-04-29 13:20:32 -07:00
// Optional restful API through Gobot has access
2014-04-26 10:13:33 -06:00
// all the robots.
type api struct {
2014-04-29 13:20:32 -07:00
gobot *gobot.Gobot
2014-07-23 13:37:05 -07:00
router *pat.PatternServeMux
2014-04-29 13:20:32 -07:00
Host string
Port string
Username string
Password string
Cert string
Key string
2014-07-21 22:19:04 -07:00
handlers []func(http.ResponseWriter, *http.Request)
2014-04-29 13:20:32 -07:00
start func(*api)
}
2014-06-10 15:16:11 -07:00
func NewAPI(g *gobot.Gobot) *api {
2014-04-29 13:20:32 -07:00
return &api{
2014-07-23 13:50:46 -07:00
gobot: g,
router: pat.New(),
Port: "3000",
2014-05-15 11:50:45 -07:00
start: func(a *api) {
2014-07-21 21:14:00 -07:00
log.Println("Initializing API on " + a.Host + ":" + a.Port + "...")
2014-07-23 13:37:05 -07:00
http.Handle("/", a)
2014-07-01 23:10:12 -07:00
2014-04-29 13:20:32 -07:00
go func() {
2014-07-21 21:14:00 -07:00
if a.Cert != "" && a.Key != "" {
http.ListenAndServeTLS(a.Host+":"+a.Port, a.Cert, a.Key, nil)
2014-04-29 13:20:32 -07:00
} else {
2014-07-21 21:14:00 -07: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-29 13:20:32 -07:00
}
}()
},
}
2014-04-26 10:13:33 -06:00
}
2014-07-23 13:37:05 -07:00
func (a *api) ServeHTTP(res http.ResponseWriter, req *http.Request) {
for _, handler := range a.handlers {
handler(res, req)
}
a.router.ServeHTTP(res, req)
}
func (a *api) Post(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Post(path, http.HandlerFunc(f))
}
func (a *api) Put(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Put(path, http.HandlerFunc(f))
}
func (a *api) Delete(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Del(path, http.HandlerFunc(f))
}
func (a *api) Options(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Options(path, http.HandlerFunc(f))
}
func (a *api) Get(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Get(path, http.HandlerFunc(f))
}
func (a *api) Head(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Head(path, http.HandlerFunc(f))
}
2014-07-21 22:19:04 -07:00
func (a *api) AddHandler(f func(http.ResponseWriter, *http.Request)) {
a.handlers = append(a.handlers, f)
}
2014-07-23 13:37:05 -07:00
func (a *api) SetBasicAuth(user, password string) {
a.Username = user
a.Password = password
a.AddHandler(a.basicAuth)
}
func (a *api) SetDebug() {
a.AddHandler(func(res http.ResponseWriter, req *http.Request) {
log.Println(req)
})
}
2014-04-26 10:13:33 -06:00
// start starts the api using the start function
// sets on the API on initialization.
2014-04-29 13:20:32 -07:00
func (a *api) Start() {
2014-07-23 13:37:05 -07:00
// api
2014-07-24 16:39:27 -07:00
mcpCommandRoute := "/api/commands/:command"
deviceCommandRoute := "/api/robots/:robot/devices/:device/commands/:command"
robotCommandRoute := "/api/robots/:robot/commands/:command"
a.Get("/api/commands", a.mcpCommands)
2014-07-23 13:37:05 -07:00
a.Get(mcpCommandRoute, a.executeMcpCommand)
a.Post(mcpCommandRoute, a.executeMcpCommand)
2014-07-24 16:39:27 -07:00
a.Get("/api/robots", a.robots)
a.Get("/api/robots/:robot", a.robot)
a.Get("/api/robots/:robot/commands", a.robotCommands)
2014-07-23 13:37:05 -07:00
a.Get(robotCommandRoute, a.executeRobotCommand)
a.Post(robotCommandRoute, a.executeRobotCommand)
2014-07-24 16:39:27 -07:00
a.Get("/api/robots/:robot/devices", a.robotDevices)
a.Get("/api/robots/:robot/devices/:device", a.robotDevice)
a.Get("/api/robots/:robot/devices/:device/commands", a.robotDeviceCommands)
2014-07-23 13:37:05 -07:00
a.Get(deviceCommandRoute, a.executeDeviceCommand)
a.Post(deviceCommandRoute, a.executeDeviceCommand)
2014-07-24 16:39:27 -07:00
a.Get("/api/robots/:robot/connections", a.robotConnections)
a.Get("/api/robots/:robot/connections/:connection", a.robotConnection)
a.Get("/api/", a.mcp)
2014-07-23 13:37:05 -07:00
// robeaux
2014-07-24 16:39:27 -07:00
a.Get("/", func(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "/index.html", http.StatusMovedPermanently)
})
2014-07-23 13:37:05 -07:00
a.Get("/index.html", a.robeaux)
a.Get("/images/:a", a.robeaux)
a.Get("/js/:a", a.robeaux)
a.Get("/js/:a/", a.robeaux)
a.Get("/js/:a/:b", a.robeaux)
a.Get("/css/:a", a.robeaux)
a.Get("/css/:a/", a.robeaux)
a.Get("/css/:a/:b", a.robeaux)
a.Get("/partials/:a", a.robeaux)
2013-11-23 16:19:11 -08:00
a.start(a)
}
2014-04-18 22:44:50 -07:00
2014-07-01 23:10:12 -07:00
func (a *api) robeaux(res http.ResponseWriter, req *http.Request) {
path := req.URL.Path
2014-07-10 11:35:00 -07:00
buf, err := robeaux.Asset(path[1:])
2014-07-01 23:10:12 -07:00
if err != nil {
2014-07-21 21:14:00 -07:00
http.Error(res, err.Error(), http.StatusNotFound)
2014-07-01 23:10:12 -07: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-21 21:14:00 -07:00
func (a *api) mcp(res http.ResponseWriter, req *http.Request) {
2014-07-24 16:39:27 -07:00
a.writeJSON(map[string]interface{}{"MCP": a.gobot.ToJSON()}, res)
2014-06-12 20:58:54 -07:00
}
2014-07-21 21:14:00 -07:00
func (a *api) mcpCommands(res http.ResponseWriter, req *http.Request) {
2014-07-24 16:39:27 -07:00
a.writeJSON(map[string]interface{}{"commands": a.gobot.ToJSON().Commands}, res)
2014-06-12 20:58:54 -07:00
}
2014-05-15 11:50:45 -07:00
func (a *api) robots(res http.ResponseWriter, req *http.Request) {
2014-06-10 15:16:11 -07:00
jsonRobots := []*gobot.JSONRobot{}
2014-07-02 18:08:44 -07:00
a.gobot.Robots().Each(func(r *gobot.Robot) {
jsonRobots = append(jsonRobots, r.ToJSON())
})
2014-07-24 16:39:27 -07:00
a.writeJSON(map[string]interface{}{"robots": jsonRobots}, res)
2014-04-15 17:59:44 -07:00
}
func (a *api) robot(res http.ResponseWriter, req *http.Request) {
2014-07-24 16:39:27 -07:00
a.writeJSON(map[string]interface{}{"robot": a.gobot.Robot(req.URL.Query().Get(":robot")).ToJSON()}, res)
2014-04-15 17:59:44 -07:00
}
func (a *api) robotCommands(res http.ResponseWriter, req *http.Request) {
2014-07-24 16:39:27 -07:00
a.writeJSON(map[string]interface{}{"commands": a.gobot.Robot(req.URL.Query().Get(":robot")).ToJSON().Commands}, res)
2014-04-15 17:59:44 -07:00
}
func (a *api) robotDevices(res http.ResponseWriter, req *http.Request) {
2014-06-10 15:16:11 -07:00
jsonDevices := []*gobot.JSONDevice{}
2014-07-23 11:24:41 -07:00
a.gobot.Robot(req.URL.Query().Get(":robot")).Devices().Each(func(d gobot.Device) {
2014-07-02 18:08:44 -07:00
jsonDevices = append(jsonDevices, d.ToJSON())
})
2014-07-24 16:39:27 -07:00
a.writeJSON(map[string]interface{}{"devices": jsonDevices}, res)
2014-04-15 17:59:44 -07:00
}
func (a *api) robotDevice(res http.ResponseWriter, req *http.Request) {
2014-07-23 11:24:41 -07:00
a.writeJSON(
2014-07-24 16:39:27 -07:00
map[string]interface{}{"device": a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).ToJSON()}, res,
2014-07-23 11:24:41 -07:00
)
2014-04-15 17:59:44 -07:00
}
func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
2014-07-23 11:24:41 -07:00
a.writeJSON(
2014-07-24 16:39:27 -07:00
map[string]interface{}{"commands": a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).ToJSON().Commands}, res,
2014-07-23 11:24:41 -07:00
)
2013-11-23 10:36:08 -08:00
}
2013-11-24 15:47:48 -08:00
func (a *api) robotConnections(res http.ResponseWriter, req *http.Request) {
2014-06-10 15:16:11 -07:00
jsonConnections := []*gobot.JSONConnection{}
2014-07-23 11:24:41 -07:00
a.gobot.Robot(req.URL.Query().Get(":robot")).Connections().Each(func(c gobot.Connection) {
2014-07-02 18:08:44 -07:00
jsonConnections = append(jsonConnections, c.ToJSON())
})
2014-07-24 16:39:27 -07:00
a.writeJSON(map[string]interface{}{"connections": jsonConnections}, res)
2014-04-18 22:44:50 -07:00
}
func (a *api) robotConnection(res http.ResponseWriter, req *http.Request) {
2014-07-23 11:24:41 -07:00
a.writeJSON(
2014-07-24 16:39:27 -07:00
map[string]interface{}{"connection": a.gobot.Robot(req.URL.Query().Get(":robot")).
Connection(req.URL.Query().Get(":connection")).ToJSON()},
2014-07-23 11:24:41 -07:00
res,
)
2014-04-18 22:44:50 -07:00
}
2014-07-21 21:14:00 -07:00
func (a *api) executeMcpCommand(res http.ResponseWriter, req *http.Request) {
2014-07-23 11:24:41 -07:00
a.executeCommand(a.gobot.Command(req.URL.Query().Get(":command")),
res,
req,
)
2014-06-12 20:58:54 -07:00
}
2014-06-11 17:41:04 -07:00
func (a *api) executeDeviceCommand(res http.ResponseWriter, req *http.Request) {
2014-07-23 11:24:41 -07:00
a.executeCommand(
a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).
Command(req.URL.Query().Get(":command")),
res,
req,
)
2013-11-24 15:47:48 -08:00
}
2013-11-27 20:05:45 -08:00
func (a *api) executeRobotCommand(res http.ResponseWriter, req *http.Request) {
2014-07-23 11:24:41 -07:00
a.executeCommand(
a.gobot.Robot(req.URL.Query().Get(":robot")).
Command(req.URL.Query().Get(":command")),
res,
req,
)
}
2014-07-21 21:14:00 -07:00
2014-07-23 11:24:41 -07:00
func (a *api) executeCommand(f func(map[string]interface{}) interface{},
res http.ResponseWriter,
req *http.Request,
) {
2014-07-21 22:19:04 -07:00
body := make(map[string]interface{})
2014-07-21 21:14:00 -07:00
json.NewDecoder(req.Body).Decode(&body)
2014-06-11 16:44:23 -07:00
if f != nil {
2014-07-24 16:39:27 -07:00
a.writeJSON(map[string]interface{}{"result": f(body)}, res)
2014-04-15 19:19:14 -07:00
} else {
2014-07-23 11:24:41 -07:00
a.writeJSON("Unknown Command", res)
2013-11-27 20:05:45 -08:00
}
2014-06-11 16:44:23 -07:00
2013-11-27 20:05:45 -08:00
}
2014-07-21 21:14:00 -07:00
// basic auth inspired by
// https://github.com/codegangsta/martini-contrib/blob/master/auth/
2014-07-21 22:19:04 -07:00
func (a *api) basicAuth(res http.ResponseWriter, req *http.Request) {
2014-07-21 21:14:00 -07:00
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)
}
}
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
}
2014-07-23 11:24:41 -07:00
func (a *api) writeJSON(j interface{}, res http.ResponseWriter) {
data, _ := json.Marshal(j)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}