Skip to content

Commit

Permalink
Fix robeaux support
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 2, 2014
1 parent fbc11de commit 9fdc9ab
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ cover:
cat tmp.cov | grep -v "mode: count" >> profile.cov ; \
done ; \
rm tmp.cov ; \

robeaux:
ifeq (,$(shell which go-bindata))
$(error robeaux not built! https://github.com/jteeuwen/go-bindata is required to build robeaux assets )
endif
cd api ; \
git clone --depth 1 git://github.com/hybridgroup/robeaux.git ; \
cd robeaux ; \
echo "Updating robeaux to $(shell git rev-parse HEAD)" ; \
go-bindata -pkg="api" -o robeaux.go -ignore=\\.git ./... ; \
mv robeaux.go .. ; \
cd .. ; \
rm -rf robeaux/ ; \
go fmt ./robeaux.go ; \

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ You can also specify the api host and port, and turn on authentication:
server.Start()
```

In order to use the [robeaux](https://github.com/hybridgroup/robeaux) AngularJS interface with Gobot you simply clone the robeaux repo and place it in the directory of your Gobot program. The robeaux assets must be in a folder called `robeaux`.
In order to use the [robeaux](https://github.com/hybridgroup/robeaux) AngularJS interface with Gobot you must have [go-bindata](https://github.com/jteeuwen/go-bindata) installed and then run `$ make robeaux`. You may now access robeaux by navigating to `http://localhost:3000/index.html`.

## Documentation
We're busy adding documentation to our web site at http://gobot.io/ please check there as we continue to work on Gobot
Expand Down
25 changes: 25 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
)

// Optional restful API through Gobot has access
Expand Down Expand Up @@ -45,6 +46,7 @@ func NewAPI(g *gobot.Gobot) *api {

log.Println("Initializing API on " + host + ":" + port + "...")
http.Handle("/", a.server)

go func() {
if cert != "" && key != "" {
http.ListenAndServeTLS(host+":"+port, cert, key, nil)
Expand Down Expand Up @@ -82,6 +84,12 @@ func (a *api) Start() {
a.server.Post(deviceCommandRoute, a.setHeaders(a.executeDeviceCommand))
a.server.Get("/robots/:robot/connections", a.setHeaders(a.robotConnections))
a.server.Get("/robots/:robot/connections/:connection", a.setHeaders(a.robotConnection))
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))

a.start(a)
}
Expand Down Expand Up @@ -118,6 +126,23 @@ func (a *api) setHeaders(f func(http.ResponseWriter, *http.Request)) http.Handle
}
}

func (a *api) robeaux(res http.ResponseWriter, req *http.Request) {
path := req.URL.Path
buf, err := Asset(path[1:])
if err != nil {
log.Println("Error serving static file:", err.Error())
res.Write([]byte(err.Error()))
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)
}

func (a *api) root(res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(a.gobot.ToJSON())
res.Header().Set("Content-Type", "application/json; charset=utf-8")
Expand Down
9 changes: 9 additions & 0 deletions api/robeaux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package api

import (
"fmt"
)

func Asset(name string) ([]byte, error) {
return nil, fmt.Errorf("Please run '$ make robeaux' for robeaux support")
}

0 comments on commit 9fdc9ab

Please sign in to comment.