move lobby to proxy.

This commit is contained in:
Miroslav Šedivý
2022-07-31 18:11:44 +02:00
parent 552089ec85
commit 247f351206
3 changed files with 102 additions and 85 deletions
+5 -78
View File
@@ -5,8 +5,7 @@ import (
"strings"
"github.com/go-chi/chi"
"github.com/m1k1o/neko-rooms/internal/utils"
"github.com/m1k1o/neko-rooms/internal/proxy"
)
func (manager *ApiManagerCtx) RoomLobby(w http.ResponseWriter, r *http.Request) {
@@ -18,91 +17,19 @@ func (manager *ApiManagerCtx) RoomLobby(w http.ResponseWriter, r *http.Request)
response, err := manager.rooms.FindByName(roomName)
if err != nil || response.Name != roomName {
manager.roomNotFound(w, r)
proxy.RoomNotFound(w, r)
return
}
if !response.Running {
manager.roomNotRunning(w, r)
proxy.RoomNotRunning(w, r)
return
}
if strings.Contains(response.Status, "starting") {
manager.roomNotReady(w, r)
proxy.RoomNotReady(w, r)
return
}
manager.roomReady(w, r)
}
func (manager *ApiManagerCtx) roomNotFound(w http.ResponseWriter, r *http.Request) {
utils.Swal2Response(w, `
<div class="swal2-header">
<div class="swal2-icon swal2-error">
<div class="swal2-icon-content">X</div>
</div>
<h2 class="swal2-title">Room not found!</h2>
</div>
<div class="swal2-content">
<div>The room you are trying to join does not exist.</div>
</div>
`)
}
func (manager *ApiManagerCtx) roomNotRunning(w http.ResponseWriter, r *http.Request) {
utils.Swal2Response(w, `
<div class="swal2-header">
<div class="swal2-icon swal2-warning">
<div class="swal2-icon-content">!</div>
</div>
<h2 class="swal2-title">Room is not running!</h2>
</div>
<div class="swal2-content">
<div>The room you are trying to join is not running.</div>
</div>
`)
}
func (manager *ApiManagerCtx) roomNotReady(w http.ResponseWriter, r *http.Request) {
utils.Swal2Response(w, `
<meta http-equiv="refresh" content="2">
<div class="swal2-header">
<div class="swal2-icon swal2-info">
<div class="swal2-icon-content">i</div>
</div>
<h2 class="swal2-title">Room is not ready, yet!</h2>
</div>
<div class="swal2-content">
<div>Please wait, until this room is ready so you can join. This should happen any second now.</div>
</div>
<div class="swal2-actions">
<div class="swal2-loader"></div>
<button type="button" onclick="location = location" class="swal2-confirm swal2-styled" style="margin-top: 1.25em">Reload</button>
</div>
`)
}
func (manager *ApiManagerCtx) roomReady(w http.ResponseWriter, r *http.Request) {
utils.Swal2Response(w, `
<div class="swal2-header">
<div class="swal2-icon swal2-success swal2-icon-show" style="display: flex;">
<div class="swal2-success-circular-line-left" style="background-color: rgb(47, 49, 54);"></div>
<span class="swal2-success-line-tip"></span> <span class="swal2-success-line-long"></span>
<div class="swal2-success-ring"></div> <div class="swal2-success-fix" style="background-color: rgb(47, 49, 54);"></div>
<div class="swal2-success-circular-line-right" style="background-color: rgb(47, 49, 54);"></div>
</div>
<h2 class="swal2-title">Room is ready!</h2>
</div>
<div class="swal2-content">
<div>Requested room is ready, you can join now.</div>
<div style="padding-top: .5em;">Try to reload page.</div>
</div>
<div class="swal2-actions">
<button type="button" onclick="location = location" class="swal2-confirm swal2-styled">Go to room</button>
</div>
<div class="swal2-content swal2-actions">
<small>If you see this page after refresh, <br /> it can mean misconfiguration on your side.</small>
</div>
`)
proxy.RoomReady(w, r)
}
+79
View File
@@ -0,0 +1,79 @@
package proxy
import (
"net/http"
"github.com/m1k1o/neko-rooms/internal/utils"
)
func RoomNotFound(w http.ResponseWriter, r *http.Request) {
utils.Swal2Response(w, `
<div class="swal2-header">
<div class="swal2-icon swal2-error">
<div class="swal2-icon-content">X</div>
</div>
<h2 class="swal2-title">Room not found!</h2>
</div>
<div class="swal2-content">
<div>The room you are trying to join does not exist.</div>
</div>
`)
}
func RoomNotRunning(w http.ResponseWriter, r *http.Request) {
utils.Swal2Response(w, `
<div class="swal2-header">
<div class="swal2-icon swal2-warning">
<div class="swal2-icon-content">!</div>
</div>
<h2 class="swal2-title">Room is not running!</h2>
</div>
<div class="swal2-content">
<div>The room you are trying to join is not running.</div>
</div>
`)
}
func RoomNotReady(w http.ResponseWriter, r *http.Request) {
utils.Swal2Response(w, `
<meta http-equiv="refresh" content="2">
<div class="swal2-header">
<div class="swal2-icon swal2-info">
<div class="swal2-icon-content">i</div>
</div>
<h2 class="swal2-title">Room is not ready, yet!</h2>
</div>
<div class="swal2-content">
<div>Please wait, until this room is ready so you can join. This should happen any second now.</div>
</div>
<div class="swal2-actions">
<div class="swal2-loader"></div>
<button type="button" onclick="location = location" class="swal2-confirm swal2-styled" style="margin-top: 1.25em">Reload</button>
</div>
`)
}
func RoomReady(w http.ResponseWriter, r *http.Request) {
utils.Swal2Response(w, `
<div class="swal2-header">
<div class="swal2-icon swal2-success swal2-icon-show" style="display: flex;">
<div class="swal2-success-circular-line-left" style="background-color: rgb(47, 49, 54);"></div>
<span class="swal2-success-line-tip"></span> <span class="swal2-success-line-long"></span>
<div class="swal2-success-ring"></div> <div class="swal2-success-fix" style="background-color: rgb(47, 49, 54);"></div>
<div class="swal2-success-circular-line-right" style="background-color: rgb(47, 49, 54);"></div>
</div>
<h2 class="swal2-title">Room is ready!</h2>
</div>
<div class="swal2-content">
<div>Requested room is ready, you can join now.</div>
<div style="padding-top: .5em;">Try to reload page.</div>
</div>
<div class="swal2-actions">
<button type="button" onclick="location = location" class="swal2-confirm swal2-styled">Go to room</button>
</div>
<div class="swal2-content swal2-actions">
<small>If you see this page after refresh, <br /> it can mean misconfiguration on your side.</small>
</div>
`)
}
+18 -7
View File
@@ -27,7 +27,7 @@ type ProxyManagerCtx struct {
prefix string
client *dockerClient.Client
config *config.Room
handlers map[string]http.Handler
handlers map[string]*httputil.ReverseProxy
}
func New(client *dockerClient.Client, config *config.Room) *ProxyManagerCtx {
@@ -43,7 +43,7 @@ func New(client *dockerClient.Client, config *config.Room) *ProxyManagerCtx {
prefix: prefix,
client: client,
config: config,
handlers: map[string]http.Handler{},
handlers: map[string]*httputil.ReverseProxy{},
}
}
@@ -62,6 +62,7 @@ func (p *ProxyManagerCtx) Start() {
filters.Arg("label", fmt.Sprintf("m1k1o.neko_rooms.instance=%s", p.config.InstanceName)),
filters.Arg("event", "create"),
filters.Arg("event", "start"),
//filters.Arg("event", "health_status"),
filters.Arg("event", "stop"),
filters.Arg("event", "destroy"),
),
@@ -124,7 +125,7 @@ func (p *ProxyManagerCtx) Refresh() error {
return err
}
p.handlers = map[string]http.Handler{}
p.handlers = map[string]*httputil.ReverseProxy{}
for _, cont := range containers {
name, port, ok := p.parseLabels(cont.Labels)
@@ -187,7 +188,7 @@ func (p *ProxyManagerCtx) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// if room not found
if !ok {
http.NotFound(w, r)
RoomNotFound(w, r)
return
}
@@ -198,15 +199,25 @@ func (p *ProxyManagerCtx) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
// if room not running
if proxy == nil {
http.Error(w, "room exists but is not running", http.StatusBadGateway)
RoomNotRunning(w, r)
return
}
// handle not ready room
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
if r.URL.Path == "/" {
RoomNotReady(w, r)
}
p.logger.Err(err).Str("room", roomName).Msg("proxying error")
}
// strip prefix from proxy
pathPrefix := path.Join(p.prefix, roomName)
proxy = http.StripPrefix(pathPrefix, proxy)
handler := http.StripPrefix(pathPrefix, proxy)
// handle by proxy
proxy.ServeHTTP(w, r)
handler.ServeHTTP(w, r)
}