Skip to content

Commit

Permalink
feat(ds-host): add AppUrlDataEvents
Browse files Browse the repository at this point in the history
- add AppUrlDataEvents to events
- create mock AppUrlDataEvents using Generic interfaces
- appmodel sends AppUrlDataEvent when appropriate
  • Loading branch information
teleclimber committed Nov 30, 2023
1 parent 6e6f0dc commit e0e2498
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmd/ds-host/ds-host.go
Expand Up @@ -155,6 +155,7 @@ func main() {
appspaceFilesEvents := &events.AppspaceFilesEvents{}
appspaceStatusEvents := &events.AppspaceStatusEvents{}
migrationJobEvents := &events.MigrationJobEvents{}
appUrlDataEvents := &events.AppUrlDataEvents{}

// models
settingsModel := &settingsmodel.SettingsModel{
Expand Down Expand Up @@ -186,7 +187,8 @@ func main() {
Config: runtimeConfig}

appModel := &appmodel.AppModel{
DB: db}
DB: db,
AppUrlDataEvents: appUrlDataEvents}
appModel.PrepareStatements()

appspaceFilesModel := &appspacefilesmodel.AppspaceFilesModel{
Expand Down
24 changes: 24 additions & 0 deletions cmd/ds-host/events/events.go
Expand Up @@ -210,3 +210,27 @@ func (e *AppspaceRouteHitEvents) removeSubscriber(ch chan<- *domain.AppspaceRout
}
}
}

// AppUrlDataEvents sends AppURLData
type AppUrlDataEvents struct {
ownerSubs eventIDSubs[domain.UserID, domain.AppURLData]
appSubs eventIDSubs[domain.AppID, domain.AppURLData]
}

func (e *AppUrlDataEvents) SubscribeOwner(ownerID domain.UserID) <-chan domain.AppURLData {
return e.appSubs.subscribe(domain.AppID(ownerID))
}

func (e *AppUrlDataEvents) SubscribeApp(appID domain.AppID) <-chan domain.AppURLData {
return e.appSubs.subscribe(appID)
}

func (e *AppUrlDataEvents) Unsubscribe(ch <-chan domain.AppURLData) {
e.appSubs.unsubscribe(ch)
e.ownerSubs.unsubscribe(ch)
}

func (e *AppUrlDataEvents) Send(ownerID domain.UserID, data domain.AppURLData) {
e.ownerSubs.send(ownerID, data)
e.appSubs.send(data.AppID, data)
}
8 changes: 4 additions & 4 deletions cmd/ds-host/events/genericevents.go
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/teleclimber/DropServer/cmd/ds-host/domain"
)

type subIDs interface {
type SubscribeIDs interface {
domain.UserID | domain.AppID | domain.AppspaceID
}
type dataTypes interface {
type DataTypes interface {
domain.AppURLData | domain.AppspaceID
}

type eventIDSubs[T subIDs, D dataTypes] struct {
type eventIDSubs[T SubscribeIDs, D DataTypes] struct {
subsMux sync.Mutex
subscribers map[T]*eventSubs[D]
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func (s *eventIDSubs[T, D]) send(subID T, data D) {
}
}

type eventSubs[D dataTypes] struct {
type eventSubs[D DataTypes] struct {
subsMux sync.Mutex
subscribers []chan D
}
Expand Down
27 changes: 26 additions & 1 deletion cmd/ds-host/models/appmodel/appmodel.go
Expand Up @@ -22,7 +22,10 @@ type stmtPreparer interface {
// AppModel represents the model for app
type AppModel struct {
DB *domain.DB
// need config to select db type?

AppUrlDataEvents interface {
Send(ownerID domain.UserID, data domain.AppURLData)
}

stmt struct {
selectID *sqlx.Stmt
Expand Down Expand Up @@ -221,6 +224,8 @@ func (m *AppModel) CreateFromURL(ownerID domain.UserID, url string, auto bool, l
return domain.AppID(0), err
}

m.sendAppURLDataEvent(appID)

return appID, nil
}

Expand Down Expand Up @@ -361,6 +366,7 @@ func (m *AppModel) UpdateAutomatic(appID domain.AppID, auto bool) error {
m.getLogger("UpdateAutomatic(), Preparex()").AppID(appID).Error(err)
return err
}
m.sendAppURLDataEvent(appID)
return nil
}

Expand Down Expand Up @@ -417,6 +423,7 @@ func (m *AppModel) SetLastFetch(appID domain.AppID, lastDt time.Time, lastResult
m.getLogger("SetLastFetch(), setLast()").AppID(appID).Error(err)
return err
}
m.sendAppURLDataEvent(appID)
return nil
}

Expand Down Expand Up @@ -451,6 +458,7 @@ func (m *AppModel) SetListing(appID domain.AppID, listingFetch domain.AppListing
}

tx.Commit()
m.sendAppURLDataEvent(appID)
return nil
}

Expand All @@ -461,6 +469,7 @@ func (m *AppModel) SetNewUrl(appID domain.AppID, url string, dt nulltypes.NullTi
m.getLogger("SetNewUrl(), setNewUrl()").AppID(appID).Error(err)
return err
}
m.sendAppURLDataEvent(appID)
return nil
}

Expand Down Expand Up @@ -506,6 +515,7 @@ func (m *AppModel) UpdateURL(appID domain.AppID, url string, listingFetch domain
}

tx.Commit()
m.sendAppURLDataEvent(appID)
return nil
}

Expand Down Expand Up @@ -556,6 +566,21 @@ func setListing(appID domain.AppID, l domain.AppListingFetch, sp stmtPreparer) e
return err
}

func (m *AppModel) sendAppURLDataEvent(appID domain.AppID) {
if m.AppUrlDataEvents == nil {
return
}
app, err := m.GetFromID(appID)
if err != nil {
return
}
urlData, err := m.GetAppUrlData(appID)
if err != nil {
return
}
m.AppUrlDataEvents.Send(app.OwnerID, urlData)
}

// GetVersion returns the version for the app
func (m *AppModel) GetVersion(appID domain.AppID, version domain.Version) (domain.AppVersion, error) {
var appVersion domain.AppVersion
Expand Down
22 changes: 17 additions & 5 deletions cmd/ds-host/testmocks/events.go
Expand Up @@ -2,18 +2,30 @@ package testmocks

import (
"github.com/teleclimber/DropServer/cmd/ds-host/domain"
"github.com/teleclimber/DropServer/cmd/ds-host/events"
)

//go:generate mockgen -destination=events_mocks.go -package=testmocks github.com/teleclimber/DropServer/cmd/ds-host/testmocks AppspaceFilesEvents,AppspaceStatusEvents
//go:generate mockgen -destination=events_mocks.go -package=testmocks github.com/teleclimber/DropServer/cmd/ds-host/testmocks AppspaceFilesEvents,AppUrlDataEvents,AppspaceStatusEvents

// xxx go:generate mockgen -source=$GOFILE
// ^^ the above fails with an internal error: nil Pkg imports which I have no idea how to fix.

// AppspaceFilesEvents interface for mocking
type GenericEvents[D events.DataTypes] interface {
Subscribe() <-chan D
SubscribeOwner(domain.UserID) <-chan D
SubscribeApp(domain.AppID) <-chan D
// more subs...
Unsubscribe(ch <-chan D)
}

type AppspaceFilesEvents interface {
Send(appspaceID domain.AppspaceID)
Subscribe() <-chan domain.AppspaceID
Unsubscribe(ch <-chan domain.AppspaceID)
Send(domain.AppspaceID)
GenericEvents[domain.AppspaceID]
}

type AppUrlDataEvents interface {
Send(domain.UserID, domain.AppURLData)
GenericEvents[domain.AppURLData]
}

// AppspaceStatusEvents interface for mocking
Expand Down
119 changes: 118 additions & 1 deletion cmd/ds-host/testmocks/events_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e0e2498

Please sign in to comment.