Skip to content

Commit

Permalink
Merge pull request #17 from Stratoscale/proxy
Browse files Browse the repository at this point in the history
More fixes for running behind a proxy
  • Loading branch information
shimzim committed Feb 4, 2018
2 parents 8b54071 + 0254463 commit 67f8766
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test run-example run-logstack-example client
.PHONY: test run-example run-example-dynamic client

test:
go test -race ./... -timeout 30s
Expand Down
3 changes: 2 additions & 1 deletion client/app/components/sockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default class SocketContainer extends Component {

componentWillMount() {
if (!socket) {
socket = new WebSocket(`ws://${window.location.host}${window.__INIT__.basePath}/_ws`)
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
socket = new WebSocket(`${protocol}://${window.location.host}${window.__INIT__.basePath}/_ws`)

// Connection opened
socket.addEventListener('open', (event) => {
Expand Down
8 changes: 2 additions & 6 deletions dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rtr := mux.NewRouter()

// add index.html serving at the serverPath which is the dynamic root
err = route.Index(rtr, route.Config{
err = route.Index(rtr, serverPath, route.Config{
// BasePath is used to determined the websocket path
BasePath: filepath.Join(h.routeCfg.RootPath, serverPath),
// RootPath is for taking the static files, which are served by the root handler
Expand All @@ -101,11 +101,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// add websocket handler on the server root
route.Engine(
rtr,
route.Config{RootPath: ""},
engine.New(h.engineCfg, src, h.parse, h.cache),
)
route.Engine(rtr, "/", engine.New(h.engineCfg, src, h.parse, h.cache))

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
6 changes: 5 additions & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ type FileInstance struct {
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Infof("New WS Client from: %s", r.RemoteAddr)
defer log.Info("Disconnected WS Client from: %s", r.RemoteAddr)
u := new(websocket.Upgrader)
u := &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
conn, err := u.Upgrade(w, r, nil)
if err != nil {
log.WithError(err).Errorf("Failed upgrade from %s", r.RemoteAddr)
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,21 @@ func main() {
cache := cache.New(cfg.Cache)

r := mux.NewRouter()
route.Static(r, cfg.Route)
route.Static(r)

if !options.dynamic {
s, err := source.New(cfg.Sources, cache)
failOnErr(err, "Creating config")
defer s.CloseSources()

failOnErr(route.Index(r, cfg.Route), "Creating index")
route.Engine(r, cfg.Route, engine.New(cfg.Global, s, parser, cache))
failOnErr(route.Index(r, "/", cfg.Route), "Creating index")

// put websocket handler behind the root and behined the proxy path
eng := engine.New(cfg.Global, s, parser, cache)
route.Engine(r, "/", eng)
if cfg.Route.RootPath != "" && cfg.Route.RootPath != "/" {
route.Engine(r, cfg.Route.RootPath, eng)
}

} else {
var err error
Expand Down
37 changes: 9 additions & 28 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"path/filepath"
"text/template"

"strings"

"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
Expand All @@ -21,23 +19,19 @@ const (
var (
indexTemplate = template.Must(template.ParseFiles("./client/dist/index.html"))
log = logrus.WithField("pkg", "router")
static = http.FileServer(http.Dir("./client/dist"))
)

type Config struct {
BasePath string `json:"base_path"`
RootPath string `json:"root_path"`
}

func Static(r *mux.Router, c Config) {
var (
static = http.FileServer(http.Dir("./client/dist"))
path = filepath.Join(c.RootPath, pathStatic)
)
log.Infof("Adding static file serving on %s", path)
r.PathPrefix(path + "/").Handler(http.StripPrefix(path, static))
func Static(r *mux.Router) {
r.PathPrefix(pathStatic + "/").Handler(http.StripPrefix(pathStatic, static))
}

func Index(r *mux.Router, c Config) error {
func Index(r *mux.Router, basePath string, c Config) error {

if c.BasePath == "" && c.RootPath != "" {
c.BasePath = c.RootPath
Expand All @@ -48,13 +42,7 @@ func Index(r *mux.Router, c Config) error {
return fmt.Errorf("executing index template: %s", err)
}

path := c.RootPath
if len(path) == 0 || path[len(path)-1] != '/' {
path += "/"
}

log.Infof("Adding index route on %s", path)
r.Path(path).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Path(basePath).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write(index.Bytes()); err != nil {
log.WithError(err).Errorf("Writing index to response")
Expand All @@ -63,8 +51,8 @@ func Index(r *mux.Router, c Config) error {
return nil
}

func Engine(r *mux.Router, c Config, engine http.Handler) {
path := filepath.Join(c.RootPath, pathWS)
func Engine(r *mux.Router, basePath string, engine http.Handler) {
path := filepath.Join(basePath, pathWS)
log.Debugf("Adding engine route on %s", path)
r.Path(path).Handler(engine)
}
Expand All @@ -73,15 +61,8 @@ func Redirect(r *mux.Router, c Config) {
if c.RootPath == "" {
return
}
r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, c.RootPath) {
http.NotFound(w, r)
return
}
dest := filepath.Join(c.RootPath, r.URL.Path)
if len(r.URL.Path) == 0 || r.URL.Path[len(r.URL.Path)-1] == '/' {
dest += "/"
}
r.PathPrefix(c.RootPath + "/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dest := r.URL.Path[len(c.RootPath):]
log.Printf("Redirecting to %s", dest)
http.Redirect(w, r, dest, http.StatusTemporaryRedirect)
})
Expand Down

0 comments on commit 67f8766

Please sign in to comment.