Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cmd/feelbeatserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"log/slog"
"net/http"
"os"

Expand All @@ -21,6 +22,7 @@ const (
)

func main() {
slog.SetLogLoggerLevel(slog.LevelDebug)
fblog.ColorizeLogger()

config := koanf.New(".")
Expand All @@ -32,17 +34,15 @@ func main() {

fblog.Info(component.FeelBeatServer, "config loaded", "config", config.All())

port := config.MustInt("websocket.port")
port := config.MustInt("server.port")
path := config.MustString("websocket.path")

hub := ws.NewHub()
go hub.Run()
roomRepo := roomrepository.NewInMemoryRoomRepository(spotify.SpotifyApi{}, ws.NewWSHub)

http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
ws.ServeWebsockets(hub, w, r)
})
ws := ws.NewWSHandler(path, roomRepo)
ws.ServeWebsockets(path, auth.AuthorizeThroughSpotify)

setupAPI(auth.AuthorizeThroughSpotify, roomrepository.NewInMemoryRoomRepository(spotify.SpotifyApi{}))
setupAPI(auth.AuthorizeThroughSpotify, roomRepo)

fblog.Info(component.FeelBeatServer, "Server started", "port", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", port), nil))
Expand Down
4 changes: 3 additions & 1 deletion config_example.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[websocket]
port = 3000
path = "/ws"

[server]
port = 3000
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@ require (
github.com/knadh/koanf/v2 v2.1.2
github.com/lmittmann/tint v1.0.5
github.com/lrstanley/go-ytdlp v0.0.0-20241221063727-6717edbb36dd
github.com/stretchr/testify v1.9.0
)

require (
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ github.com/knadh/koanf/providers/file v1.1.2 h1:aCC36YGOgV5lTtAFz2qkgtWdeQsgfxUk
github.com/knadh/koanf/providers/file v1.1.2/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI=
github.com/knadh/koanf/v2 v2.1.2 h1:I2rtLRqXRy1p01m/utEtpZSSA6dcJbgGVuE27kW2PzQ=
github.com/knadh/koanf/v2 v2.1.2/go.mod h1:Gphfaen0q1Fc1HTgJgSTC4oRX9R2R5ErYMZJy8fLJBo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/lrstanley/go-ytdlp v0.0.0-20241221063727-6717edbb36dd h1:lLajTMgNTs/W4H05uQYnJDRIbIvHk6XXy7DQNFRbvzU=
Expand Down Expand Up @@ -89,8 +85,5 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions internal/infra/api/roomapi/creategame.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"github.com/feelbeatapp/feelbeatserver/internal/infra/api"
"github.com/feelbeatapp/feelbeatserver/internal/infra/auth"
"github.com/feelbeatapp/feelbeatserver/internal/infra/fblog"
"github.com/feelbeatapp/feelbeatserver/internal/lib"
"github.com/feelbeatapp/feelbeatserver/internal/lib/component"
"github.com/feelbeatapp/feelbeatserver/internal/lib/feelbeaterror"
"github.com/feelbeatapp/feelbeatserver/internal/lib/room"
)

type createGameResponse struct {
RoomId string `json:"roomId"`
}

func (r RoomApi) createGameHandler(user auth.User, res http.ResponseWriter, req *http.Request) {
var payload room.RoomSettings
var payload lib.RoomSettings
err := api.ParseBody(req.Body, &payload)
if err != nil {
http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
Expand Down
2 changes: 1 addition & 1 deletion internal/infra/api/roomapi/fetchgames.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (r RoomApi) fetchRoomsHandler(user auth.User, res http.ResponseWriter, req
formatted.Rooms = append(formatted.Rooms, responseRoom{
Id: room.Id(),
Name: room.Name(),
Players: len(room.Players()),
Players: len(room.PlayerProfiles()),
MaxPlayers: room.Settings().MaxPlayers,
ImageUrl: room.ImageUrl(),
})
Expand Down
121 changes: 0 additions & 121 deletions internal/infra/ws/client.go

This file was deleted.

6 changes: 0 additions & 6 deletions internal/infra/ws/message.go

This file was deleted.

40 changes: 27 additions & 13 deletions internal/infra/ws/servewebsockets.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ws

import (
"fmt"
"net/http"
"os"

"github.com/feelbeatapp/feelbeatserver/internal/lib/component"
"github.com/feelbeatapp/feelbeatserver/internal/infra/fblog"
"github.com/feelbeatapp/feelbeatserver/internal/infra/api"
"github.com/feelbeatapp/feelbeatserver/internal/infra/auth"
"github.com/feelbeatapp/feelbeatserver/internal/lib/feelbeaterror"
"github.com/feelbeatapp/feelbeatserver/internal/lib/messages"
"github.com/gorilla/websocket"
)

Expand All @@ -15,20 +17,32 @@ var upgrader = websocket.Upgrader{
},
}

func ServeWebsockets(hub *WSHub, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)

fblog.Info(component.WebSocket, "received new connection", "ip", r.RemoteAddr)
func (w WSHandler) websocketHandler(user auth.User, res http.ResponseWriter, req *http.Request) {
roomId := req.PathValue("id")
room := w.roomRepo.GetRoom(roomId)
if room == nil {
http.Error(res, feelbeaterror.RoomNotFound, feelbeaterror.StatusCode(feelbeaterror.RoomNotFound))
api.LogApiError("User tried to connect to non existing room", nil, user.Profile.Id, req)
return
}
conn, err := upgrader.Upgrade(res, req, nil)
if err != nil {
http.Error(res, feelbeaterror.Default, feelbeaterror.StatusCode(feelbeaterror.Default))
api.LogApiError("Upgrading websocket connection failed", err, user.Profile.Id, req)
return
}

userSocket := messages.NewUserSocket(newSocketClient(conn), user)
err = room.Hub().Register(userSocket)
if err != nil {
fblog.Error(component.WebSocket, "failed to upgrade connection", "ip", r.RemoteAddr)
os.Exit(1)
http.Error(res, feelbeaterror.RoomNotFound, feelbeaterror.StatusCode(feelbeaterror.RoomNotFound))
api.LogApiError("Registering socket failed, hub is closed", err, user.Profile.Id, req)
return
}

client := newClient(conn, hub)
hub.RegisterClient(client)
api.LogApiCall(user.Profile.Id, req)
}

go client.readLoop()
go client.sendLoop()
func (w WSHandler) ServeWebsockets(basePath string, authWrapper auth.AuthWrapper) {
http.HandleFunc(fmt.Sprintf("%s/{id}", basePath), authWrapper(w.websocketHandler))
}
Loading
Loading