Skip to content

Commit

Permalink
refactor: rename app to bus
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Apr 25, 2022
1 parent 273f792 commit 9a5edf0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 39 deletions.
30 changes: 15 additions & 15 deletions core/app/app.go → core/bus/bus.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package bus

import (
"context"
Expand All @@ -8,30 +8,30 @@ import (
"github.com/ItsNotGoodName/reciva-web-remote/core/state"
)

type App struct {
type BusServiceImpl struct {
hubService radio.HubService
radioService radio.RadioService
statePub state.Pub
}

func New(hubService radio.HubService, radioService radio.RadioService, statePub state.Pub) *App {
return &App{
func New(hubService radio.HubService, radioService radio.RadioService, statePub state.Pub) *BusServiceImpl {
return &BusServiceImpl{
hubService: hubService,
radioService: radioService,
statePub: statePub,
}
}

func (a *App) StateGet(ctx context.Context, uuid string) (*state.State, error) {
radio, err := a.hubService.Get(uuid)
func (bs *BusServiceImpl) stateGet(ctx context.Context, uuid string) (*state.State, error) {
radio, err := bs.hubService.Get(uuid)
if err != nil {
return nil, err
}

return a.radioService.GetState(ctx, radio)
return bs.radioService.GetState(ctx, radio)
}

func (a *App) Bus(ctx context.Context, readC <-chan Command, writeC chan<- Command) {
func (bs *BusServiceImpl) Handle(ctx context.Context, readC <-chan Command, writeC chan<- Command) {
stateUUID := ""
stateSub, stateUnsub := make(<-chan state.Message), func() {}
defer stateUnsub()
Expand All @@ -43,32 +43,32 @@ func (a *App) Bus(ctx context.Context, readC <-chan Command, writeC chan<- Comma
case msg := <-stateSub:
// Write state or state partial
if state.IsChangedAll(msg.Changed) {
writeCommand(ctx, writeC, NewStateCommand(&msg.State))
writeCommand(ctx, writeC, newStateCommand(&msg.State))
} else {
writeCommand(ctx, writeC, NewStatePartialCommand(state.GetPartial(&msg.State, msg.Changed)))
writeCommand(ctx, writeC, newStatePartialCommand(state.GetPartial(&msg.State, msg.Changed)))
}
case dto := <-readC:
switch dto.Type {
case TypeStateSubscribe:
stateUUID = fmt.Sprint(dto.Slug)
if stateUUID == "" {
writeCommand(ctx, writeC, NewErrorCommand(fmt.Errorf("invalid uuid")))
writeCommand(ctx, writeC, newErrorCommand(fmt.Errorf("invalid uuid")))
continue
}

// Subscribe to state
stateUnsub()
stateSub, stateUnsub = a.statePub.Subscribe(stateUUID)
stateSub, stateUnsub = bs.statePub.Subscribe(stateUUID)

// Get state
state, err := a.StateGet(ctx, stateUUID)
state, err := bs.stateGet(ctx, stateUUID)
if err != nil {
writeCommand(ctx, writeC, NewErrorCommand(err))
writeCommand(ctx, writeC, newErrorCommand(err))
continue
}

// Write state
writeCommand(ctx, writeC, NewStateCommand(state))
writeCommand(ctx, writeC, newStateCommand(state))
case TypeStateUnsubscribe:
// Unsubscribe from radio
stateUnsub()
Expand Down
26 changes: 17 additions & 9 deletions core/app/dto.go → core/bus/type.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package app
package bus

import (
"context"

"github.com/ItsNotGoodName/reciva-web-remote/core/state"
)

type Command struct {
Type Type `json:"type"`
Slug interface{} `json:"slug"`
}
type (
Type string

Command struct {
Type Type `json:"type"`
Slug interface{} `json:"slug"`
}

type Type string
Service interface {
Handle(ctx context.Context, readC <-chan Command, writeC chan<- Command)
}
)

const (
TypeError = Type("error")
Expand All @@ -19,21 +27,21 @@ const (
TypeStateUnsubscribe = Type("state.unsubscribe")
)

func NewErrorCommand(err error) Command {
func newErrorCommand(err error) Command {
return Command{
Type: TypeError,
Slug: err.Error(),
}
}

func NewStateCommand(state *state.State) Command {
func newStateCommand(state *state.State) Command {
return Command{
Type: TypeState,
Slug: state,
}
}

func NewStatePartialCommand(partial state.Partial) Command {
func newStatePartialCommand(partial state.Partial) Command {
return Command{
Type: TypeStatePartial,
Slug: partial,
Expand Down
2 changes: 1 addition & 1 deletion core/app/util.go → core/bus/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package bus

import "context"

Expand Down
16 changes: 8 additions & 8 deletions left/api/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"time"

"github.com/ItsNotGoodName/reciva-web-remote/core/app"
"github.com/ItsNotGoodName/reciva-web-remote/core/bus"
"github.com/gorilla/websocket"
)

Expand All @@ -22,11 +22,11 @@ func GetWS(upgrader *websocket.Upgrader, handleWS func(*websocket.Conn)) http.Ha
}
}

func HandleWS(application *app.App) func(conn *websocket.Conn) {
func HandleWS(busService bus.Service) func(conn *websocket.Conn) {
return func(conn *websocket.Conn) {
ctx, cancel := context.WithCancel(context.Background())

application.Bus(
busService.Handle(
ctx,
wsHandleRead(ctx, cancel, conn),
wsHandleWrite(ctx, cancel, conn),
Expand All @@ -48,8 +48,8 @@ const (
wsMaxMessageSize = 512
)

func wsHandleWrite(ctx context.Context, cancel context.CancelFunc, conn *websocket.Conn) chan<- app.Command {
writeC := make(chan app.Command)
func wsHandleWrite(ctx context.Context, cancel context.CancelFunc, conn *websocket.Conn) chan<- bus.Command {
writeC := make(chan bus.Command)
go func() {
ticker := time.NewTicker(wsPingPeriod)
defer func() {
Expand Down Expand Up @@ -85,8 +85,8 @@ func wsHandleWrite(ctx context.Context, cancel context.CancelFunc, conn *websock
return writeC
}

func wsHandleRead(ctx context.Context, cancel context.CancelFunc, conn *websocket.Conn) <-chan app.Command {
readC := make(chan app.Command)
func wsHandleRead(ctx context.Context, cancel context.CancelFunc, conn *websocket.Conn) <-chan bus.Command {
readC := make(chan bus.Command)
go func() {
defer cancel()

Expand All @@ -96,7 +96,7 @@ func wsHandleRead(ctx context.Context, cancel context.CancelFunc, conn *websocke

for {
// Read msg or end on error
var msg app.Command
var msg bus.Command
err := conn.ReadJSON(&msg)
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
Expand Down
6 changes: 3 additions & 3 deletions left/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"time"

"github.com/ItsNotGoodName/reciva-web-remote/core/app"
"github.com/ItsNotGoodName/reciva-web-remote/core/bus"
"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
"github.com/ItsNotGoodName/reciva-web-remote/core/radio"
"github.com/ItsNotGoodName/reciva-web-remote/left/api"
Expand All @@ -26,7 +26,7 @@ type Router struct {
r chi.Router
}

func New(port string, p presenter.Presenter, fs fs.FS, hub radio.HubService, radioService radio.RadioService, application *app.App, presetStore preset.PresetStore) *Router {
func New(port string, p presenter.Presenter, fs fs.FS, hub radio.HubService, radioService radio.RadioService, busService bus.Service, presetStore preset.PresetStore) *Router {
r := newRouter()
upgrader := newUpgrader()

Expand All @@ -42,7 +42,7 @@ func New(port string, p presenter.Presenter, fs fs.FS, hub radio.HubService, rad

// API routes
r.Route("/api", func(r chi.Router) {
r.Get("/ws", api.GetWS(upgrader, api.HandleWS(application)))
r.Get("/ws", api.GetWS(upgrader, api.HandleWS(busService)))

r.Get("/radios", p(api.GetRadios(hub, radioService)))
r.Post("/radios", p(api.PostRadios(hub)))
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/ItsNotGoodName/go-upnpsub"
"github.com/ItsNotGoodName/reciva-web-remote/config"
"github.com/ItsNotGoodName/reciva-web-remote/core/app"
"github.com/ItsNotGoodName/reciva-web-remote/core/background"
"github.com/ItsNotGoodName/reciva-web-remote/core/bus"
"github.com/ItsNotGoodName/reciva-web-remote/core/middleware"
"github.com/ItsNotGoodName/reciva-web-remote/core/pubsub"
"github.com/ItsNotGoodName/reciva-web-remote/core/radio"
Expand Down Expand Up @@ -61,10 +61,10 @@ func main() {
backgrounds = append(backgrounds, createService)
hubService := radio.NewHubService(createService)
backgrounds = append(backgrounds, hubService)
application := app.New(hubService, radioService, statePub)
busService := bus.New(hubService, radioService, statePub)

// Left
router := router.New(cfg.PortStr, presenter.New(json.Render), web.FS(), hubService, radioService, application, middlewareAndPresetStore)
router := router.New(cfg.PortStr, presenter.New(json.Render), web.FS(), hubService, radioService, busService, middlewareAndPresetStore)
backgrounds = append(backgrounds, router)

// Run backgrounds
Expand Down

0 comments on commit 9a5edf0

Please sign in to comment.