More progress on event and gofmt

master
Caj Larsson 3 years ago
parent c609ca7b9a
commit 3e6855d429

@ -2,8 +2,8 @@ package namespace
import ( import (
"caj-larsson/bog/util" "caj-larsson/bog/util"
"time"
"fmt" "fmt"
"time"
) )
type Clock interface { type Clock interface {
@ -19,7 +19,6 @@ type NamespaceService struct {
default_quota_bytes int64 default_quota_bytes int64
} }
func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, default_ttl time.Duration, default_quota_bytes int64) *NamespaceService { func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, default_ttl time.Duration, default_quota_bytes int64) *NamespaceService {
return &NamespaceService{ return &NamespaceService{
repo, repo,
@ -31,7 +30,6 @@ func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, defau
} }
} }
func (s *NamespaceService) GetOrCreateNs(name string) *Namespace { func (s *NamespaceService) GetOrCreateNs(name string) *Namespace {
ns, err := s.repo.GetByName(name) ns, err := s.repo.GetByName(name)
@ -60,8 +58,8 @@ func (s *NamespaceService) GetOrCreateNs(name string) *Namespace {
return ns return ns
} }
func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func(ev util.Event)) {
func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func(ev util.Event)) { reg("FileUsed", s.handleFileUsed) reg("FileUsed", s.handleFileUsed)
s.outboxes = append(s.outboxes, outbox) s.outboxes = append(s.outboxes, outbox)
reg("FileUsed", s.handleFileUsed) reg("FileUsed", s.handleFileUsed)
@ -69,8 +67,7 @@ func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func
reg("FileRecieved", s.handleFileRecieved) reg("FileRecieved", s.handleFileRecieved)
} }
func (s *NamespaceService) All() []Namespace {
func (s *NamespaceService) All() ([]Namespace) {
nss, err := s.repo.All() nss, err := s.repo.All()
if err != nil { if err != nil {
panic(err) panic(err)

@ -18,7 +18,6 @@ type SwampFileService struct {
eventBus util.EventBus eventBus util.EventBus
} }
func NewSwampFileService( func NewSwampFileService(
ns_svc namespace.NamespaceService, ns_svc namespace.NamespaceService,
swamp_file_repo swampfile.Repository, swamp_file_repo swampfile.Repository,
@ -29,12 +28,10 @@ func NewSwampFileService(
return &s return &s
} }
func (s SwampFileService) NamespaceStats() []namespace.Namespace { func (s SwampFileService) NamespaceStats() []namespace.Namespace {
return s.ns_svc.All() return s.ns_svc.All()
} }
func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, size int64) error { func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, size int64) error {
ns := s.ns_svc.GetOrCreateNs(ref.UserAgent) ns := s.ns_svc.GetOrCreateNs(ref.UserAgent)
@ -57,7 +54,8 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s
s.eventBus.Handle(*util.NewEvent("FileUsed", struct { s.eventBus.Handle(*util.NewEvent("FileUsed", struct {
Name string Name string
Size int64}{ Size int64
}{
ns.Name, ns.Name,
f.Size(), 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 // TODO: rewrite this into an interruptable loop that emits downloaded events
written, err := io.CopyN(f, src, size) written, err := io.CopyN(f, src, size)
if written < size { if written < size {
s.swamp_file_repo.Delete(r.Path, strconv.FormatInt(ns.ID, 10)) // s.swamp_file_repo.Delete(r.Path, strconv.FormatInt(ns.ID, 10)) //
return swampfile.ErrContentSizeExaggerated return swampfile.ErrContentSizeExaggerated
@ -83,7 +80,8 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s
f.Close() f.Close()
s.eventBus.Handle(*util.NewEvent("FileRecieved", struct { s.eventBus.Handle(*util.NewEvent("FileRecieved", struct {
Name string Name string
Size int64}{ Size int64
}{
ns.Name, ns.Name,
written, written,
})) }))
@ -125,7 +123,8 @@ func (s SwampFileService) CleanUpExpiredFiles() error {
for _, df := range dfs { for _, df := range dfs {
s.eventBus.Handle(*util.NewEvent("FileDeleted", struct { s.eventBus.Handle(*util.NewEvent("FileDeleted", struct {
Name string Name string
Size int64}{ Size int64
}{
ns.Name, ns.Name,
df.Size, df.Size,
})) }))

@ -2,16 +2,16 @@ package dataswamp
import ( import (
"bytes" "bytes"
"fmt" "caj-larsson/bog/dataswamp/namespace"
"caj-larsson/bog/dataswamp/swampfile" "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/matryer/is"
"github.com/spf13/afero" "github.com/spf13/afero"
"testing" "testing"
"time" "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{} type TestLogger struct{}

@ -1,13 +1,13 @@
package server package server
import ( import (
"caj-larsson/bog/util"
"caj-larsson/bog/dataswamp" "caj-larsson/bog/dataswamp"
"caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/namespace"
"caj-larsson/bog/dataswamp/swampfile" "caj-larsson/bog/dataswamp/swampfile"
fs_swampfile "caj-larsson/bog/infrastructure/fs/swampfile" fs_swampfile "caj-larsson/bog/infrastructure/fs/swampfile"
sql_namespace "caj-larsson/bog/infrastructure/sqlite/namespace" sql_namespace "caj-larsson/bog/infrastructure/sqlite/namespace"
"caj-larsson/bog/infrastructure/system_time" "caj-larsson/bog/infrastructure/system_time"
"caj-larsson/bog/util"
"net/http" "net/http"
"strconv" "strconv"
"text/template" "text/template"

@ -4,8 +4,10 @@ import (
"testing" "testing"
// "fmt" // "fmt"
"caj-larsson/bog/dataswamp" "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/memory/swampfile"
"caj-larsson/bog/infrastructure/system_time"
"github.com/matryer/is" "github.com/matryer/is"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -22,17 +24,25 @@ func (t TestLogger) Warn(format string, a ...interface{}) {}
func TestApplication(t *testing.T) { func TestApplication(t *testing.T) {
is := is.New(t) is := is.New(t)
logger := TestLogger{} logger := TestLogger{}
nsRepo := ns.NewRepository()
ns_svc := namespace.NewNamespaceService(
nsRepo,
logger,
system_time.Clock{},
time.Hour,
1000,
)
file_service := dataswamp.NewSwampFileService( file_service := dataswamp.NewSwampFileService(
namespace.NewRepository(), *ns_svc,
swampfile.NewRepository(), swampfile.NewRepository(),
1000,
time.Hour,
logger, logger,
) )
bog := Bog{ bog := Bog{
router: new(http.ServeMux), router: new(http.ServeMux),
file_service: file_service, file_service: *file_service,
address: "fake", address: "fake",
logger: logger, logger: logger,
} }

Loading…
Cancel
Save