Skip to content

Commit

Permalink
fix: reverse proxy support websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
tympanix committed Jul 10, 2018
1 parent cb106d3 commit 214c5fd
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion api/api_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

type jsonConfig struct {
Lang []jsonLang `json:"languages"`
Lang []jsonLang `json:"languages"`
Proxypath string `json:"proxypath"`
}

type jsonLang struct {
Expand All @@ -30,6 +31,7 @@ func (a *API) config(w http.ResponseWriter, r *http.Request) interface{} {
}
return jsonConfig{
langs,
a.Config().ProxyPath(),
}
}
err := errors.New("Method not allowed")
Expand Down
1 change: 1 addition & 0 deletions app/app_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (c fakeConfig) Providers() []types.Provider { return c.providers }
func (c fakeConfig) Scrapers() []types.Scraper { return c.scrapers }
func (c fakeConfig) RenameAction() string { return c.action }
func (c fakeConfig) Evaluator() types.Evaluator { return c.evaluator }
func (c fakeConfig) ProxyPath() string { return "/" }

type fakeTemplates struct {
output string
Expand Down
4 changes: 4 additions & 0 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,7 @@ func (v viperConfig) RenameAction() string {
func (v viperConfig) Evaluator() types.Evaluator {
return new(score.DefaultEvaluator)
}

func (v viperConfig) ProxyPath() string {
return viper.GetString("proxypath")
}
2 changes: 2 additions & 0 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
func init() {
webCmd.Flags().IntP("port", "p", 5670, "port used to serve the web application")
webCmd.Flags().String("static", "", "path to the web files to serve")
webCmd.Flags().String("proxypath", "/", "base path for reverse proxy")

viper.BindPFlag("port", webCmd.Flags().Lookup("port"))
viper.BindPFlag("static", webCmd.Flags().Lookup("static"))
viper.BindPFlag("proxypath", webCmd.Flags().Lookup("proxypath"))

rootCmd.AddCommand(webCmd)
}
Expand Down
3 changes: 3 additions & 0 deletions etc/supper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ impared: false
# Bind web server to port
port: 5670

# Base path for reverse proxy
proxypath: "/"

# Location of web root for static resources
static: /var/lib/supper

Expand Down
1 change: 1 addition & 0 deletions types/app_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Config interface {
MediaFilter() MediaFilter
RenameAction() string
Evaluator() Evaluator
ProxyPath() string
}

// APIKeys is the interface for configuration of 3rd party APIs
Expand Down
27 changes: 25 additions & 2 deletions web/js/websocket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { EventEmitter } from 'events'

import Snackbar from './comp/Snackbar'
import { configStore } from './stores'
import { join } from 'path'

const loggers = {
"debug": Snackbar.notify,
Expand All @@ -14,12 +16,33 @@ class Websocket extends EventEmitter {

constructor() {
super()
this.ws = new WebSocket("ws://" + document.location.host + "/api/ws")
this.ws.onmessage = this.__handle.bind(this)
this.__listen()
this.__connect()
this.__id = 0
this.__handlers = []
}

__create() {
this.__proto = document.location.protocol === "http:" ? "ws://" : "wss://"
this.__path = join(document.location.host, this.__proxypath || "", "/api/ws")
this.ws = new WebSocket(this.__proto + this.__path)
this.ws.onmessage = this.__handle.bind(this)
}

__connect() {
this.__proxypath = configStore.getAll().proxypath
if (this.__proxypath) {
this.__create()
}
}

__listen() {
let self = this
configStore.on("change", () => {
self.__connect()
})
}

__handle(event) {
console.log(event.data)
var data = this.__json(event.data)
Expand Down

0 comments on commit 214c5fd

Please sign in to comment.