Skip to content

Commit

Permalink
Added cli-args 'bind' & 'canonicalurl'
Browse files Browse the repository at this point in the history
'bind' is an address that lets you configure which IP & port Beehive binds its
API & HTTP server to. The default is "localhost:8181".

'canonicalurl' tells Beehive which canonical URL the API & HTTP server runs on.
The default is "http://localhost:8181". This is useful when running Beehive
behind a reverse-proxy.

Fixes #95.
  • Loading branch information
muesli committed Feb 19, 2017
1 parent 048709b commit 2f7565f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
76 changes: 69 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,42 @@
package api

import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strings"
"time"

log "github.com/Sirupsen/logrus"
"github.com/emicklei/go-restful"
"github.com/muesli/smolder"

"github.com/muesli/beehive/api/context"
"github.com/muesli/beehive/api/resources/actions"
"github.com/muesli/beehive/api/resources/bees"
"github.com/muesli/beehive/api/resources/chains"
"github.com/muesli/beehive/api/resources/hives"
"github.com/muesli/smolder"
"github.com/muesli/beehive/app"
)

var (
bind string
canonicalURL string
)

const (
defaultBind = "localhost:8181"
defaultURL = "http://localhost:8181"
)

func escapeURL(u string) string {
return strings.Replace(url.QueryEscape(u), "%2F", "/", -1)
}

func configFromPathParam(req *restful.Request, resp *restful.Response) {
rootdir := "./config"

Expand All @@ -45,20 +66,42 @@ func configFromPathParam(req *restful.Request, resp *restful.Response) {
subpath = "index.html"
}
actual := path.Join(rootdir, subpath)
log.Printf("serving %s ... (from %s)", actual, req.PathParameter("subpath"))

log.Printf("serving %s ... (from %s)\n", actual, req.PathParameter("subpath"))
http.ServeFile(
b, err := ioutil.ReadFile(actual)
if err != nil {
log.Errorln("Failed reading", actual)
http.Error(resp.ResponseWriter, "Failed reading file", http.StatusInternalServerError)
return
}

if defaultURL != canonicalURL {
// We're serving files on a non-default canonical URL
// Make sure the HTML we serve references API & assets with the correct URL
if actual == "config/index.html" {
// Since we patch the content of the files, we must drop the integrity SHA-sums
// TODO: Would be nicer to recalculate them
re := regexp.MustCompile("integrity=\"([^\"]*)\"")
b = re.ReplaceAll(b, []byte{})
}
b = bytes.Replace(b, []byte(defaultURL), []byte(canonicalURL), -1)
b = bytes.Replace(b, []byte(escapeURL(defaultURL)), []byte(escapeURL(canonicalURL)), -1)
}

http.ServeContent(
resp.ResponseWriter,
req.Request,
actual)
actual,
time.Now(),
bytes.NewReader(b))
}

func imageFromPathParam(req *restful.Request, resp *restful.Response) {
rootdir := "./assets/bees"

subpath := req.PathParameter("subpath")
actual := path.Join(rootdir, subpath)
log.Printf("serving %s ... (from %s)\n", actual, req.PathParameter("subpath"))
log.Printf("serving %s ... (from %s)", actual, req.PathParameter("subpath"))
http.ServeFile(
resp.ResponseWriter,
req.Request,
Expand All @@ -74,7 +117,7 @@ func Run() {

// Setup web-service
smolderConfig := smolder.APIConfig{
BaseURL: "http://localhost:8181",
BaseURL: canonicalURL,
PathPrefix: "v1/",
}

Expand All @@ -97,8 +140,27 @@ func Run() {
&actions.ActionResource{},
)

server := &http.Server{Addr: ":8181", Handler: wsContainer}
server := &http.Server{Addr: bind, Handler: wsContainer}
go func() {
log.Fatal(server.ListenAndServe())
}()
}

func init() {
app.AddFlags([]app.CliFlag{
{
V: &bind,
Name: "bind",
Value: defaultBind,
Desc: "Which address to bind Beehive's API & admin interface to",
},
})
app.AddFlags([]app.CliFlag{
{
V: &canonicalURL,
Name: "canonicalurl",
Value: defaultURL,
Desc: "Canonical URL for the API & admin interface",
},
})
}
2 changes: 1 addition & 1 deletion api/resources/hives/hives_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func prepareHiveResponse(context smolder.APIContext, hive *bees.BeeFactoryInterf
ID: (*hive).ID(),
Name: (*hive).Name(),
Description: (*hive).Description(),
Image: "http://localhost:8181/images/" + (*hive).Image(),
Image: "/images/" + (*hive).Image(),
LogoColor: (*hive).LogoColor(),
Options: (*hive).Options(),
Events: (*hive).Events(),
Expand Down

0 comments on commit 2f7565f

Please sign in to comment.