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 @@ -6,15 +6,15 @@ import (
"net/http"
"os"

"github.com/feelbeatapp/feelbeatserver/internal/component"
"github.com/feelbeatapp/feelbeatserver/internal/fblog"
"github.com/feelbeatapp/feelbeatserver/internal/networking"
"github.com/feelbeatapp/feelbeatserver/internal/infra/fblog"
"github.com/feelbeatapp/feelbeatserver/internal/infra/ws"
"github.com/feelbeatapp/feelbeatserver/internal/lib/component"
"github.com/knadh/koanf/v2"
)

const (
ENV_PREFIX = "FEELBEAT_"
TOML_PATH = "config.toml"
envPrefix = "FEELBEAT_"
tomlPath = "config.toml"
)

func main() {
Expand All @@ -32,11 +32,11 @@ func main() {
port := config.MustInt("websocket.port")
path := config.MustString("websocket.path")

hub := networking.NewHub()
hub := ws.NewHub()
go hub.Run()

http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
networking.ServeWebsockets(hub, w, r)
ws.ServeWebsockets(hub, w, r)
})

fblog.Info(component.FeelBeatServer, "Server started", "port", port)
Expand Down
10 changes: 5 additions & 5 deletions cmd/feelbeatserver/setupconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"io/fs"
"strings"

"github.com/feelbeatapp/feelbeatserver/internal/component"
"github.com/feelbeatapp/feelbeatserver/internal/fblog"
"github.com/feelbeatapp/feelbeatserver/internal/lib/component"
"github.com/feelbeatapp/feelbeatserver/internal/infra/fblog"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
)

func setupConfig(config *koanf.Koanf) error {
err := config.Load(file.Provider(TOML_PATH), toml.Parser())
err := config.Load(file.Provider(tomlPath), toml.Parser())

if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
Expand All @@ -26,8 +26,8 @@ func setupConfig(config *koanf.Koanf) error {
fblog.Info(component.Config, "config file loaded")
}

envProvider := env.Provider(ENV_PREFIX, ".", func(key string) string {
envvar := strings.ToLower(strings.TrimPrefix(key, ENV_PREFIX))
envProvider := env.Provider(envPrefix, ".", func(key string) string {
envvar := strings.ToLower(strings.TrimPrefix(key, envPrefix))

return strings.ReplaceAll(envvar, "_", ".")
})
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ go 1.23.1
require github.com/gorilla/websocket v1.5.3

require (
github.com/buger/jsonparser v1.1.1
github.com/knadh/koanf/parsers/toml v0.1.0
github.com/knadh/koanf/providers/env v1.0.0
github.com/knadh/koanf/providers/file v1.1.2
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
Expand All @@ -22,6 +26,8 @@ require (
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
golang.org/x/sys v0.27.0 // indirect
github.com/stretchr/objx v0.5.2 // 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
)
59 changes: 57 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78=
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
Expand All @@ -22,6 +30,8 @@ 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=
github.com/lrstanley/go-ytdlp v0.0.0-20241221063727-6717edbb36dd/go.mod h1:75ujbafjqiJugIGw4K6o52/p8C0m/kt+DrYwgClXYT4=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
Expand All @@ -30,10 +40,55 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
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=
Expand Down
2 changes: 1 addition & 1 deletion internal/fblog/main.go → internal/infra/fblog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"time"

"github.com/feelbeatapp/feelbeatserver/internal/component"
"github.com/feelbeatapp/feelbeatserver/internal/lib/component"
"github.com/lmittmann/tint"
)

Expand Down
6 changes: 3 additions & 3 deletions internal/networking/client.go → internal/infra/ws/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package networking
package ws

import (
"time"

"github.com/feelbeatapp/feelbeatserver/internal/component"
"github.com/feelbeatapp/feelbeatserver/internal/fblog"
"github.com/feelbeatapp/feelbeatserver/internal/lib/component"
"github.com/feelbeatapp/feelbeatserver/internal/infra/fblog"
"github.com/gorilla/websocket"
)

Expand Down
6 changes: 3 additions & 3 deletions internal/networking/hub.go → internal/infra/ws/hub.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package networking
package ws

import (
"github.com/feelbeatapp/feelbeatserver/internal/component"
"github.com/feelbeatapp/feelbeatserver/internal/fblog"
"github.com/feelbeatapp/feelbeatserver/internal/lib/component"
"github.com/feelbeatapp/feelbeatserver/internal/infra/fblog"
)

type BasicHub struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package networking
package ws

type ClientMessage struct {
From HubClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package networking
package ws

import (
"net/http"
"os"

"github.com/feelbeatapp/feelbeatserver/internal/component"
"github.com/feelbeatapp/feelbeatserver/internal/fblog"
"github.com/feelbeatapp/feelbeatserver/internal/lib/component"
"github.com/feelbeatapp/feelbeatserver/internal/infra/fblog"
"github.com/gorilla/websocket"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package networking
package ws

type Hub interface {
RegisterClient(HubClient)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package networking_test
package ws_test

import (
"fmt"
"testing"
"time"

"github.com/feelbeatapp/feelbeatserver/internal/networking"
"github.com/feelbeatapp/feelbeatserver/internal/infra/ws"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,9 +21,7 @@ func newFakeClient() *FakeClient {
}

func (c *FakeClient) Send(payload []byte) {
fmt.Println("Let's go I'm really sending sht")
c.payloads = append(c.payloads, payload)
fmt.Println(c.payloads)
}

func (c *FakeClient) CloseNow() {
Expand All @@ -39,7 +36,7 @@ const testMessage = "hi there"

func TestHubBroadcastsMessages(t *testing.T) {
assert := assert.New(t)
hub := networking.NewHub()
hub := ws.NewHub()

go hub.Run()

Expand All @@ -49,7 +46,7 @@ func TestHubBroadcastsMessages(t *testing.T) {
hub.RegisterClient(clients[i])
}

hub.Broadcast(networking.ClientMessage{
hub.Broadcast(ws.ClientMessage{
From: clients[0],
Payload: []byte(testMessage),
})
Expand Down
4 changes: 4 additions & 0 deletions internal/lib/audioprovider/ytaudioprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package audioprovider

type YTAudioProvider struct {
}
2 changes: 2 additions & 0 deletions internal/component/main.go → internal/lib/component/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const (
WebSocket = "websocket"
Client = "client"
Hub = "hub"

AudioDownloadTask = "audiodownloadtask"
)

type FeelBeatComponent = string
23 changes: 23 additions & 0 deletions internal/lib/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testutils

import (
"errors"
"sync"
"time"
)

// waitTimeout waits for the waitgroup for the specified max timeout.
// Returns true if waiting timed out.
func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) error {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return nil // completed normally
case <-time.After(timeout):
return errors.New("Timeout exceeded") // timed out
}
}
9 changes: 9 additions & 0 deletions internal/lib/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lib

import "time"

type SongDetails struct {
Title string
Artist string
Duration time.Duration
}
47 changes: 47 additions & 0 deletions internal/thirdparty/spotify/spotifyapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package spotify

import (
"fmt"
"io"
"log"
"net/http"
"time"

"github.com/buger/jsonparser"
"github.com/feelbeatapp/feelbeatserver/internal/lib"
)

func FetchSongDetails(spotifyId string, token string) {
url := fmt.Sprintf("https://api.spotify.com/v1/tracks/%s", spotifyId)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
req.Header.Set("Authorization", "Bearer "+token)

res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}

defer res.Body.Close()
bytes, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}

title, err := jsonparser.GetString(bytes, "name")
if err != nil {
log.Fatal(err)

}
durationInMs, err := jsonparser.GetInt(bytes, "duration_ms")
if err != nil {
log.Fatal(err)
}

fmt.Println(lib.SongDetails{
Title: title,
Duration: time.Duration(durationInMs) * time.Millisecond,
})
}
Loading
Loading