More progress on event and gofmt

master
Caj Larsson 3 years ago
parent c609ca7b9a
commit 3e6855d429

@ -1,7 +1,7 @@
package dataswamp
import (
// "caj-larsson/bog/dataswamp/namespace"
// "caj-larsson/bog/dataswamp/namespace"
)
type AdminService struct {}
type AdminService struct{}

@ -2,8 +2,8 @@ package namespace
import (
"caj-larsson/bog/util"
"time"
"fmt"
"time"
)
type Clock interface {
@ -19,9 +19,8 @@ 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 {
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)

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

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

@ -43,7 +43,7 @@ func (fr *FileReference) Clean(strict bool) (*FileReference, error) {
return nil, ErrUnacceptablePath
}
n := FileReference {
n := FileReference{
c,
fr.UserAgent,
}

@ -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()
}

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

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

@ -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())
}

Loading…
Cancel
Save