diff --git a/dataswamp/admin_service.go b/dataswamp/admin_service.go index 1d04adf..6e5bb22 100644 --- a/dataswamp/admin_service.go +++ b/dataswamp/admin_service.go @@ -1,7 +1,7 @@ package dataswamp import ( - // "caj-larsson/bog/dataswamp/namespace" +// "caj-larsson/bog/dataswamp/namespace" ) -type AdminService struct {} +type AdminService struct{} diff --git a/dataswamp/namespace/service.go b/dataswamp/namespace/service.go index 0e319fc..18c1e2b 100644 --- a/dataswamp/namespace/service.go +++ b/dataswamp/namespace/service.go @@ -2,8 +2,8 @@ package namespace import ( "caj-larsson/bog/util" - "time" "fmt" + "time" ) type Clock interface { @@ -11,17 +11,16 @@ type Clock interface { } type NamespaceService struct { - repo Repository - outboxes []func(util.Event) - logger util.Logger - clock Clock - default_ttl time.Duration + repo Repository + outboxes []func(util.Event) + logger util.Logger + clock Clock + default_ttl time.Duration default_quota_bytes int64 } - func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, default_ttl time.Duration, default_quota_bytes int64) *NamespaceService { - return &NamespaceService { + return &NamespaceService{ repo, nil, logger, @@ -31,7 +30,6 @@ func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, defau } } - func (s *NamespaceService) GetOrCreateNs(name string) *Namespace { ns, err := s.repo.GetByName(name) @@ -60,8 +58,8 @@ func (s *NamespaceService) GetOrCreateNs(name string) *Namespace { return ns } - -func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func(ev util.Event)) { reg("FileUsed", s.handleFileUsed) +func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func(ev util.Event)) { + reg("FileUsed", s.handleFileUsed) s.outboxes = append(s.outboxes, outbox) reg("FileUsed", s.handleFileUsed) @@ -69,8 +67,7 @@ func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func reg("FileRecieved", s.handleFileRecieved) } - -func (s *NamespaceService) All() ([]Namespace) { +func (s *NamespaceService) All() []Namespace { nss, err := s.repo.All() if err != nil { panic(err) @@ -79,22 +76,22 @@ func (s *NamespaceService) All() ([]Namespace) { } func (s *NamespaceService) handleFileUsed(payload interface{}) { - var payload_s = payload.(struct{ + var payload_s = payload.(struct { Name string Size int64 }) - fmt.Printf("file used %v\n", payload_s ) + fmt.Printf("file used %v\n", payload_s) ns := s.GetOrCreateNs(payload_s.Name) ns.FileQuota = ns.FileQuota.Add(payload_s.Size) s.repo.Update(ns.ID, *ns) } func (s *NamespaceService) handleFileDeleted(payload interface{}) { - var payload_s = payload.(struct{ + var payload_s = payload.(struct { Name string Size int64 }) - fmt.Printf("file deleted %v\n", payload_s ) + fmt.Printf("file deleted %v\n", payload_s) ns := s.GetOrCreateNs(payload_s.Name) ns.FileQuota = ns.FileQuota.Add(-payload_s.Size) fmt.Printf("file usage %v\n", ns.FileQuota) @@ -102,11 +99,11 @@ func (s *NamespaceService) handleFileDeleted(payload interface{}) { } func (s *NamespaceService) handleFileRecieved(payload interface{}) { - var payload_s = payload.(struct{ + var payload_s = payload.(struct { Name string Size int64 }) - fmt.Printf("file recieved %v\n", payload_s ) + fmt.Printf("file recieved %v\n", payload_s) ns := s.GetOrCreateNs(payload_s.Name) ns.FileQuota = ns.FileQuota.Add(payload_s.Size) fmt.Printf("file usage %v\n", ns.FileQuota) diff --git a/dataswamp/namespace/valueobjects.go b/dataswamp/namespace/valueobjects.go index 279c181..8fccf0f 100644 --- a/dataswamp/namespace/valueobjects.go +++ b/dataswamp/namespace/valueobjects.go @@ -14,14 +14,14 @@ func (f FileSizeQuota) Remaining() int64 { } func (f FileSizeQuota) Add(size int64) FileSizeQuota { - return FileSizeQuota { + return FileSizeQuota{ f.AllowanceKB, f.CurrentUsage + size, } } func (f FileSizeQuota) Remove(size int64) FileSizeQuota { - return FileSizeQuota { + return FileSizeQuota{ f.AllowanceKB, f.CurrentUsage - size, } diff --git a/dataswamp/swamp_service.go b/dataswamp/swamp_service.go index 8af2f06..56d37c6 100644 --- a/dataswamp/swamp_service.go +++ b/dataswamp/swamp_service.go @@ -12,13 +12,12 @@ import ( ) type SwampFileService struct { - ns_svc namespace.NamespaceService - swamp_file_repo swampfile.Repository - logger util.Logger - eventBus util.EventBus + ns_svc namespace.NamespaceService + swamp_file_repo swampfile.Repository + logger util.Logger + eventBus util.EventBus } - func NewSwampFileService( ns_svc namespace.NamespaceService, swamp_file_repo swampfile.Repository, @@ -29,12 +28,10 @@ func NewSwampFileService( return &s } - func (s SwampFileService) NamespaceStats() []namespace.Namespace { return s.ns_svc.All() } - func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, size int64) error { ns := s.ns_svc.GetOrCreateNs(ref.UserAgent) @@ -55,9 +52,10 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s return err } - s.eventBus.Handle(*util.NewEvent("FileUsed", struct { + s.eventBus.Handle(*util.NewEvent("FileUsed", struct { Name string - Size int64}{ + Size int64 + }{ ns.Name, f.Size(), })) @@ -65,7 +63,6 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s // TODO: rewrite this into an interruptable loop that emits downloaded events written, err := io.CopyN(f, src, size) - if written < size { s.swamp_file_repo.Delete(r.Path, strconv.FormatInt(ns.ID, 10)) // return swampfile.ErrContentSizeExaggerated @@ -81,9 +78,10 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s } f.Close() - s.eventBus.Handle(*util.NewEvent("FileRecieved", struct { + s.eventBus.Handle(*util.NewEvent("FileRecieved", struct { Name string - Size int64}{ + Size int64 + }{ ns.Name, written, })) @@ -125,7 +123,8 @@ func (s SwampFileService) CleanUpExpiredFiles() error { for _, df := range dfs { s.eventBus.Handle(*util.NewEvent("FileDeleted", struct { Name string - Size int64}{ + Size int64 + }{ ns.Name, df.Size, })) diff --git a/dataswamp/swamp_service_test.go b/dataswamp/swamp_service_test.go index 05d4f99..a1c81ca 100644 --- a/dataswamp/swamp_service_test.go +++ b/dataswamp/swamp_service_test.go @@ -2,16 +2,16 @@ package dataswamp import ( "bytes" - "fmt" + "caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/swampfile" + m_namespace "caj-larsson/bog/infrastructure/memory/namespace" + m_swampfile "caj-larsson/bog/infrastructure/memory/swampfile" + "caj-larsson/bog/infrastructure/system_time" + "fmt" "github.com/matryer/is" "github.com/spf13/afero" "testing" "time" - "caj-larsson/bog/dataswamp/namespace" - m_namespace "caj-larsson/bog/infrastructure/memory/namespace" - m_swampfile "caj-larsson/bog/infrastructure/memory/swampfile" - "caj-larsson/bog/infrastructure/system_time" ) type TestLogger struct{} @@ -28,7 +28,7 @@ func NewTestSwampFileService() SwampFileService { file_repo := m_swampfile.NewRepository() ns_repo := m_namespace.NewRepository() - logger := TestLogger {} + logger := TestLogger{} ns_svc := namespace.NewNamespaceService( ns_repo, logger, @@ -182,6 +182,6 @@ func TestCleanUpExpired(t *testing.T) { ns, err := ns_repo.GetByName("ns1") fmt.Printf("file final usage %v\n", ns.FileQuota) is.NoErr(err) - fmt.Printf("file\n" ) + fmt.Printf("file\n") is.Equal(ns.FileQuota.CurrentUsage, int64(len("My bog"))) } diff --git a/dataswamp/swampfile/valueobjects.go b/dataswamp/swampfile/valueobjects.go index c3f00f5..6d10dfd 100644 --- a/dataswamp/swampfile/valueobjects.go +++ b/dataswamp/swampfile/valueobjects.go @@ -43,7 +43,7 @@ func (fr *FileReference) Clean(strict bool) (*FileReference, error) { return nil, ErrUnacceptablePath } - n := FileReference { + n := FileReference{ c, fr.UserAgent, } diff --git a/infrastructure/system_time/clock.go b/infrastructure/system_time/clock.go index 2b76ccc..1c414d5 100644 --- a/infrastructure/system_time/clock.go +++ b/infrastructure/system_time/clock.go @@ -2,8 +2,8 @@ package system_time import "time" -type Clock struct {} +type Clock struct{} -func (c Clock) Now() time.Time{ +func (c Clock) Now() time.Time { return time.Now() } diff --git a/server/bog.go b/server/bog.go index 2ac7306..8443318 100644 --- a/server/bog.go +++ b/server/bog.go @@ -1,13 +1,13 @@ package server import ( - "caj-larsson/bog/util" "caj-larsson/bog/dataswamp" "caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/swampfile" fs_swampfile "caj-larsson/bog/infrastructure/fs/swampfile" sql_namespace "caj-larsson/bog/infrastructure/sqlite/namespace" "caj-larsson/bog/infrastructure/system_time" + "caj-larsson/bog/util" "net/http" "strconv" "text/template" @@ -158,6 +158,6 @@ func New(config *Configuration) *Bog { func (b *Bog) Run() { b.logger.Info("Starting bog on address: %s", b.address) go b.cleanNamespaces() - go func(){ http.ListenAndServe(b.adminAddress, b.adminRouter) }() + go func() { http.ListenAndServe(b.adminAddress, b.adminRouter) }() http.ListenAndServe(b.address, b.router) } diff --git a/server/bog_test.go b/server/bog_test.go index 2045b40..0a99d1d 100644 --- a/server/bog_test.go +++ b/server/bog_test.go @@ -4,8 +4,10 @@ import ( "testing" // "fmt" "caj-larsson/bog/dataswamp" - "caj-larsson/bog/infrastructure/memory/namespace" + "caj-larsson/bog/dataswamp/namespace" + ns "caj-larsson/bog/infrastructure/memory/namespace" "caj-larsson/bog/infrastructure/memory/swampfile" + "caj-larsson/bog/infrastructure/system_time" "github.com/matryer/is" "net/http" "net/http/httptest" @@ -22,17 +24,25 @@ func (t TestLogger) Warn(format string, a ...interface{}) {} func TestApplication(t *testing.T) { is := is.New(t) logger := TestLogger{} + nsRepo := ns.NewRepository() + + ns_svc := namespace.NewNamespaceService( + nsRepo, + logger, + system_time.Clock{}, + time.Hour, + 1000, + ) + file_service := dataswamp.NewSwampFileService( - namespace.NewRepository(), + *ns_svc, swampfile.NewRepository(), - 1000, - time.Hour, logger, ) bog := Bog{ router: new(http.ServeMux), - file_service: file_service, + file_service: *file_service, address: "fake", logger: logger, } diff --git a/util/event.go b/util/event.go index a59a1cd..7b23730 100644 --- a/util/event.go +++ b/util/event.go @@ -2,10 +2,10 @@ package util type Event struct { eventName string - payload interface {} + payload interface{} } -func NewEvent(eventName string, payload interface{}) *Event{ +func NewEvent(eventName string, payload interface{}) *Event { return &Event{ eventName, payload, @@ -26,15 +26,15 @@ type EventBus struct { handlers map[string][]EventHandler } -func NewEventBus() *EventBus{ - return &EventBus{ make(map[string][]EventHandler) } +func NewEventBus() *EventBus { + return &EventBus{make(map[string][]EventHandler)} } func (eb *EventBus) Register(eventName string, handler EventHandler) { eb.handlers[eventName] = append(eb.handlers[eventName], handler) } -func (eb *EventBus) Handle(e Event){ +func (eb *EventBus) Handle(e Event) { for _, handler := range eb.handlers[e.EventName()] { handler(e.Payload()) }