Skip to content

Commit

Permalink
validate create room
Browse files Browse the repository at this point in the history
  • Loading branch information
skewb1k committed Nov 25, 2024
1 parent 7a53912 commit 6502a74
Show file tree
Hide file tree
Showing 29 changed files with 704 additions and 254 deletions.
3 changes: 0 additions & 3 deletions ROADMAP.md

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func main() {

appConfig := loadAppConfig()

logLevel := slog.Level(0)
logLevel := slog.Level(slog.LevelDebug)
if err := logLevel.UnmarshalText([]byte(strings.ToUpper(appConfig.LogLevel))); err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ services:
- ${SERVER_PORT}:${SERVER_PORT}
restart: unless-stopped
depends_on:
- redis
redis:
condition: service_healthy
networks:
- sharetube

Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.2

require (
github.com/go-chi/chi/v5 v5.1.0
github.com/go-playground/validator/v10 v10.23.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/spf13/pflag v1.0.5
Expand All @@ -14,6 +15,13 @@ require (
require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.23.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

require (
Expand All @@ -31,8 +39,8 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
31 changes: 25 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand All @@ -24,10 +34,15 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down Expand Up @@ -71,15 +86,19 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
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/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
1 change: 1 addition & 0 deletions internal/domain/members.go → internal/_domain/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Member struct {
AvatarURL string `json:"avatar_url"`
IsMuted bool `json:"is_muted"`
IsAdmin bool `json:"is_admin"`
IsOnline bool `json:"is_online"`
Conn *websocket.Conn `json:"-"`
}

Expand Down
11 changes: 11 additions & 0 deletions internal/domain/player.go → internal/_domain/player.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package domain

import "time"

type Player struct {
CurrentVideoURL string `json:"current_video_url"`
IsPlaying bool `json:"is_playing"`
CurrentTime float64 `json:"current_time"`
PlaybackRate float64 `json:"playback_rate"`
UpdatedAt int64 `json:"updated_at"`
}

func (p Player) CalcCurrentTime() float64 {
if p.IsPlaying {
return (float64(time.Now().UnixMilli()-p.UpdatedAt) * p.PlaybackRate) + p.CurrentTime
}

return p.CurrentTime
}

func NewPlayer(initialVideoURL string) *Player {
Expand Down
18 changes: 4 additions & 14 deletions internal/domain/playlist.go → internal/_domain/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,19 @@ type Video struct {
ID int `json:"id"`
URL string `json:"url"`
AddedByID string `json:"added_by"`
WasPlayed bool `json:"was_played"`
}

// todo: implement pagination
type Playlist struct {
list []Video
previousVideo *Video
// currentVideo *Video
list []Video
lastID int
limit int
}

func NewPlaylist(initialVideoURL, addedBy string, limit int) *Playlist {
func NewPlaylist(addedBy string, limit int) *Playlist {
return &Playlist{
list: []Video{
{
ID: 1,
URL: initialVideoURL,
AddedByID: addedBy,
WasPlayed: false,
},
},
lastID: 1,
list: make([]Video, 1),
lastID: 0,
limit: limit,
}
}
Expand Down
16 changes: 9 additions & 7 deletions internal/service/handler.go → internal/_domain/room/handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package service
package room

import (
"encoding/json"
"log/slog"
"time"

"github.com/sharetube/server/internal/domain"
)
Expand Down Expand Up @@ -129,19 +130,20 @@ func (r *Room) handlePlayerUpdated(input *Input) (*domain.Player, error) {
// return nil, ErrEmptyData
// }

var udpatedPlayer struct {
var updatedPlayer struct {
IsPlaying bool `json:"is_playing"`
CurrentTime float64 `json:"current_time"`
PlaybackRate float64 `json:"playback_rate"`
}
if err := json.Unmarshal(input.Data, &udpatedPlayer); err != nil {
if err := json.Unmarshal(input.Data, &updatedPlayer); err != nil {
return nil, err
}
slog.Debug("player updated", "player", udpatedPlayer)
slog.Debug("player updated", "player", updatedPlayer)

r.player.IsPlaying = udpatedPlayer.IsPlaying
r.player.CurrentTime = udpatedPlayer.CurrentTime
r.player.PlaybackRate = udpatedPlayer.PlaybackRate
r.player.IsPlaying = updatedPlayer.IsPlaying
r.player.CurrentTime = updatedPlayer.CurrentTime
r.player.PlaybackRate = updatedPlayer.PlaybackRate
r.player.UpdatedAt = time.Now().UnixMilli()

return r.player, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package room

import (
"fmt"
Expand All @@ -12,9 +12,11 @@ func (r *Room) ReadMessages(conn *websocket.Conn) {
var input Input
if err := conn.ReadJSON(&input); err != nil {
slog.Debug("error reading message", "error", err)
r.sendError(conn, fmt.Errorf("error reading message: %w", err))
r.RemoveMemberByConn(conn)
conn.Close()
return

}
slog.Info("message recieved", "message", input)

Expand Down
10 changes: 7 additions & 3 deletions internal/service/room.go → internal/_domain/room/room.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package room

import (
"encoding/json"
Expand All @@ -17,6 +17,7 @@ type Input struct {
Action string `json:"action"`
Sender *domain.Member `json:"-"`
Data []byte `json:"data"`
// RequestID int `json:"request_id"`
}

func (i *Input) UnmarshalJSON(data []byte) error {
Expand Down Expand Up @@ -47,17 +48,19 @@ type Message struct {
}

type Room struct {
id string
playlist *domain.Playlist
members *domain.Members
player *domain.Player
inputCh chan Input
closeCh chan struct{}
}

func newRoom(creator *domain.Member, initialVideoURL string, membersLimit, playlistLimit int) *Room {
func NewRoom(creator *domain.Member, id, initialVideoURL string, membersLimit, playlistLimit int) *Room {
creator.IsAdmin = true
return &Room{
playlist: domain.NewPlaylist(initialVideoURL, creator.ID, playlistLimit),
id: id,
playlist: domain.NewPlaylist(creator.ID, playlistLimit),
members: domain.NewMembers(creator, membersLimit),
player: domain.NewPlayer(initialVideoURL),
inputCh: make(chan Input),
Expand All @@ -67,6 +70,7 @@ func newRoom(creator *domain.Member, initialVideoURL string, membersLimit, playl

func (r Room) GetState() map[string]any {
return map[string]any{
"id": r.id,
"playlist": r.playlist.AsList(),
"playlist_length": r.playlist.Length(),
"members": r.members.AsList(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package room

import (
"log/slog"
Expand Down
11 changes: 5 additions & 6 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/sharetube/server/internal/controller"
"github.com/sharetube/server/internal/repository/redis"
"github.com/sharetube/server/internal/service"
"github.com/sharetube/server/internal/service/room"
"github.com/sharetube/server/pkg/redisclient"
"golang.org/x/exp/slog"
)
Expand All @@ -29,7 +29,7 @@ type AppConfig struct {
RedisPassword string `json:"-"`
}

// todo: add validation.
// todo: add validation
func (cfg *AppConfig) Validate() error {
if cfg.MembersLimit < 1 {
return fmt.Errorf("members limit must be greater than 0")
Expand All @@ -54,13 +54,12 @@ func Run(ctx context.Context, cfg *AppConfig) error {
}
defer rc.Close()

createRoomSessionRepo := redis.NewCreateRoomSessionRepo(rc, 30*time.Second)
createRoomSessionRepo.Set(ctx, "some-value")
roomService := service.NewRoomService(cfg.UpdatesInterval, cfg.MembersLimit, cfg.PlaylistLimit)
roomRepo := redis.NewRepo(rc)
roomService := room.NewService(roomRepo, cfg.UpdatesInterval, cfg.MembersLimit, cfg.PlaylistLimit)
controller := controller.NewController(roomService)
server := &http.Server{Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), Handler: controller.Mux()}

// graceful shutdown.
// graceful shutdown
serverCtx, serverStopCtx := context.WithCancel(ctx)

sig := make(chan os.Signal, 1)
Expand Down
14 changes: 11 additions & 3 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package controller

import (
"context"
"net/http"

"github.com/gorilla/websocket"
"github.com/sharetube/server/internal/service"
"github.com/sharetube/server/internal/service/room"
"github.com/sharetube/server/pkg/validator"
)

type iRoomService interface {
CreateRoom(context.Context, *room.CreateRoomParams) (room.CreateRoomResponse, error)
}

type Controller struct {
roomService iRoomService
upgrader websocket.Upgrader
roomService service.RoomService
validate *validator.Validator
}

func NewController(roomService service.RoomService) *Controller {
func NewController(roomService iRoomService) *Controller {
return &Controller{
upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
roomService: roomService,
validate: validator.NewValidator(),
}
}
29 changes: 0 additions & 29 deletions internal/controller/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package controller
import (
"fmt"
"net/http"

"github.com/sharetube/server/internal/domain"
)

func (c Controller) getQueryParam(r *http.Request, key string) (string, error) {
Expand All @@ -15,30 +13,3 @@ func (c Controller) getQueryParam(r *http.Request, key string) (string, error) {

return value, nil
}

func (c Controller) getUser(r *http.Request) (*domain.Member, error) {
username, err := c.getQueryParam(r, "username")
if err != nil {
return nil, err
}

color, err := c.getQueryParam(r, "color")
if err != nil {
return nil, err
}

avatarURL, err := c.getQueryParam(r, "avatar-url")
if err != nil {
return nil, err
}

// userID := uuid.NewString()
userID := username

return &domain.Member{
ID: userID,
Username: username,
Color: color,
AvatarURL: avatarURL,
}, nil
}
Loading

0 comments on commit 6502a74

Please sign in to comment.