mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-02 22:51:34 -06:00
fix proxy.
This commit is contained in:
+26
-39
@@ -22,7 +22,7 @@ type entry struct {
|
|||||||
id string
|
id string
|
||||||
running bool
|
running bool
|
||||||
ready bool
|
ready bool
|
||||||
handler *httputil.ReverseProxy
|
handler http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
type wait struct {
|
type wait struct {
|
||||||
@@ -121,10 +121,7 @@ func (p *ProxyManagerCtx) Start() {
|
|||||||
|
|
||||||
// if proxying is disabled
|
// if proxying is disabled
|
||||||
if enabled {
|
if enabled {
|
||||||
e.handler = httputil.NewSingleHostReverseProxy(&url.URL{
|
e.handler = p.newProxyHandler(path, host)
|
||||||
Scheme: "http",
|
|
||||||
Host: host,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p.handlers.Insert(path, e)
|
p.handlers.Insert(path, e)
|
||||||
@@ -174,10 +171,7 @@ func (p *ProxyManagerCtx) Refresh() error {
|
|||||||
|
|
||||||
// if proxying is enabled and room is ready
|
// if proxying is enabled and room is ready
|
||||||
if enabled && room.IsReady {
|
if enabled && room.IsReady {
|
||||||
entry.handler = httputil.NewSingleHostReverseProxy(&url.URL{
|
entry.handler = p.newProxyHandler(path, host)
|
||||||
Scheme: "http",
|
|
||||||
Host: host,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p.handlers.Insert(path, entry)
|
p.handlers.Insert(path, entry)
|
||||||
@@ -205,6 +199,18 @@ func (p *ProxyManagerCtx) parseLabels(labels map[string]string) (enabled bool, p
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *ProxyManagerCtx) newProxyHandler(prefix, host string) http.Handler {
|
||||||
|
handler := httputil.NewSingleHostReverseProxy(&url.URL{
|
||||||
|
Scheme: "http",
|
||||||
|
Host: host,
|
||||||
|
})
|
||||||
|
handler.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
|
p.logger.Err(err).Str("prefix", prefix).Msg("proxy error")
|
||||||
|
http.Error(w, "unable to connect to room", http.StatusBadGateway)
|
||||||
|
}
|
||||||
|
return http.StripPrefix(prefix, handler)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *ProxyManagerCtx) waitForPath(w http.ResponseWriter, r *http.Request, path string) {
|
func (p *ProxyManagerCtx) waitForPath(w http.ResponseWriter, r *http.Request, path string) {
|
||||||
p.logger.Debug().Str("path", path).Msg("adding new wait handler")
|
p.logger.Debug().Str("path", path).Msg("adding new wait handler")
|
||||||
|
|
||||||
@@ -247,15 +253,21 @@ func (p *ProxyManagerCtx) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
proxy, prefix, ok := p.handlers.Match(cleanPath)
|
proxy, prefix, ok := p.handlers.Match(cleanPath)
|
||||||
p.mu.RUnlock()
|
p.mu.RUnlock()
|
||||||
|
|
||||||
// if room not found
|
// if room is not ready
|
||||||
if !ok {
|
if !ok || !proxy.running || !proxy.ready {
|
||||||
// blocking until room is created
|
// blocking until room is ready
|
||||||
if r.URL.Query().Has("wait") && p.waitEnabled {
|
if r.URL.Query().Has("wait") && p.waitEnabled {
|
||||||
p.waitForPath(w, r, cleanPath)
|
p.waitForPath(w, r, cleanPath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
RoomNotFound(w, r, p.waitEnabled)
|
if !ok {
|
||||||
|
RoomNotFound(w, r, p.waitEnabled)
|
||||||
|
} else if !proxy.running {
|
||||||
|
RoomNotRunning(w, r, p.waitEnabled)
|
||||||
|
} else {
|
||||||
|
RoomNotReady(w, r, p.waitEnabled)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,37 +278,12 @@ func (p *ProxyManagerCtx) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if room not running
|
|
||||||
if !proxy.running || !proxy.ready {
|
|
||||||
// blocking until room is ready
|
|
||||||
if r.URL.Query().Has("wait") && p.waitEnabled {
|
|
||||||
p.waitForPath(w, r, cleanPath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !proxy.running {
|
|
||||||
RoomNotRunning(w, r, p.waitEnabled)
|
|
||||||
} else {
|
|
||||||
RoomNotReady(w, r, p.waitEnabled)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// if not proxying, just return room ready
|
// if not proxying, just return room ready
|
||||||
if proxy.handler == nil {
|
if proxy.handler == nil {
|
||||||
RoomReady(w, r)
|
RoomReady(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle not ready room
|
|
||||||
proxy.handler.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
|
||||||
p.logger.Err(err).Str("prefix", prefix).Msg("proxying error")
|
|
||||||
http.Error(w, "unable to connect to room", http.StatusBadGateway)
|
|
||||||
}
|
|
||||||
|
|
||||||
// strip prefix from proxy
|
|
||||||
handler := http.StripPrefix(prefix, proxy.handler)
|
|
||||||
|
|
||||||
// handle by proxy
|
// handle by proxy
|
||||||
handler.ServeHTTP(w, r)
|
proxy.handler.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user