More progress on event and gofmt

master
Caj Larsson 3 years ago
parent c609ca7b9a
commit 3e6855d429

@ -2,8 +2,8 @@ package namespace
import (
"caj-larsson/bog/util"
"time"
"fmt"
"time"
)
type Clock interface {
@ -19,7 +19,6 @@ type NamespaceService struct {
default_quota_bytes int64
}
func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, default_ttl time.Duration, default_quota_bytes int64) *NamespaceService {
return &NamespaceService{
repo,
@ -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)

@ -18,7 +18,6 @@ type SwampFileService struct {
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)
@ -57,7 +54,8 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s
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
@ -83,7 +80,8 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s
f.Close()
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,
}))

@ -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{}

@ -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"

@ -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,
}

Loading…
Cancel
Save