diff --git a/internal/api/lobby.go b/internal/api/lobby.go index f7a4394..00fff1a 100644 --- a/internal/api/lobby.go +++ b/internal/api/lobby.go @@ -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, ` -
-
-
X
-
-

Room not found!

-
-
-
The room you are trying to join does not exist.
-
- `) -} - -func (manager *ApiManagerCtx) roomNotRunning(w http.ResponseWriter, r *http.Request) { - utils.Swal2Response(w, ` -
-
-
!
-
-

Room is not running!

-
-
-
The room you are trying to join is not running.
-
- `) -} - -func (manager *ApiManagerCtx) roomNotReady(w http.ResponseWriter, r *http.Request) { - utils.Swal2Response(w, ` - - -
-
-
i
-
-

Room is not ready, yet!

-
-
-
Please wait, until this room is ready so you can join. This should happen any second now.
-
-
-
- -
- `) -} - -func (manager *ApiManagerCtx) roomReady(w http.ResponseWriter, r *http.Request) { - utils.Swal2Response(w, ` -
-
-
- -
-
-
-

Room is ready!

-
-
-
Requested room is ready, you can join now.
-
Try to reload page.
-
-
- -
-
- If you see this page after refresh,
it can mean misconfiguration on your side.
-
- `) + proxy.RoomReady(w, r) } diff --git a/internal/proxy/lobby.go b/internal/proxy/lobby.go new file mode 100644 index 0000000..fc81add --- /dev/null +++ b/internal/proxy/lobby.go @@ -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, ` +
+
+
X
+
+

Room not found!

+
+
+
The room you are trying to join does not exist.
+
+ `) +} + +func RoomNotRunning(w http.ResponseWriter, r *http.Request) { + utils.Swal2Response(w, ` +
+
+
!
+
+

Room is not running!

+
+
+
The room you are trying to join is not running.
+
+ `) +} + +func RoomNotReady(w http.ResponseWriter, r *http.Request) { + utils.Swal2Response(w, ` + + +
+
+
i
+
+

Room is not ready, yet!

+
+
+
Please wait, until this room is ready so you can join. This should happen any second now.
+
+
+
+ +
+ `) +} + +func RoomReady(w http.ResponseWriter, r *http.Request) { + utils.Swal2Response(w, ` +
+
+
+ +
+
+
+

Room is ready!

+
+
+
Requested room is ready, you can join now.
+
Try to reload page.
+
+
+ +
+
+ If you see this page after refresh,
it can mean misconfiguration on your side.
+
+ `) +} diff --git a/internal/proxy/manager.go b/internal/proxy/manager.go index 1ec5c2f..13c8ed4 100644 --- a/internal/proxy/manager.go +++ b/internal/proxy/manager.go @@ -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) }