logger, implements #9

master
Caj Larsson 3 years ago
parent 53531373b9
commit 99bdf4e320

@ -0,0 +1,7 @@
package dataswamp
type Logger interface {
Debug(format string, a ...interface{})
Info(format string, a ...interface{})
Warn(format string, a ...interface{})
}

@ -15,6 +15,7 @@ type SwampFileService struct {
swamp_file_repo swampfile.Repository
default_allowance_bytes int64
default_allowance_duration time.Duration
logger Logger
}
func NewSwampFileService(
@ -22,8 +23,9 @@ func NewSwampFileService(
swamp_file_repo swampfile.Repository,
da_bytes int64,
da_duration time.Duration,
logger Logger,
) SwampFileService {
return SwampFileService{namespace_repo, swamp_file_repo, da_bytes, da_duration}
return SwampFileService{namespace_repo, swamp_file_repo, da_bytes, da_duration, logger}
}
func (s SwampFileService) getOrCreateNs(namespace_in string) *namespace.Namespace {
@ -114,6 +116,7 @@ func (s SwampFileService) OpenOutFile(ref swampfile.FileReference) (swampfile.Sw
}
func (s SwampFileService) CleanUpExpiredFiles() error {
s.logger.Info("Cleaning up expired files")
nss, err := s.namespace_repo.All()
if err != nil {

@ -12,6 +12,12 @@ import (
m_swampfile "caj-larsson/bog/infrastructure/memory/swampfile"
)
type TestLogger struct{}
func (t TestLogger) Debug(format string, a ...interface{}) {}
func (t TestLogger) Info(format string, a ...interface{}) {}
func (t TestLogger) Warn(format string, a ...interface{}) {}
var file_ref1 = swampfile.FileReference{"/path1", "ns1"}
var file_ref2 = swampfile.FileReference{"/path1", "ns2"}
var file_ref3 = swampfile.FileReference{"/path2", "ns1"}
@ -19,7 +25,7 @@ var file_ref3 = swampfile.FileReference{"/path2", "ns1"}
func NewTestSwampFileService() SwampFileService {
file_repo := m_swampfile.NewRepository()
ns_repo := m_namespace.NewRepository()
return NewSwampFileService(ns_repo, file_repo, 1024, time.Hour)
return NewSwampFileService(ns_repo, file_repo, 1024, time.Hour, TestLogger{})
}
func TestFileDontExist(t *testing.T) {
@ -138,7 +144,7 @@ func TestCleanUpExpired(t *testing.T) {
fs := afero.NewMemMapFs()
file_repo := m_swampfile.Repository{fs}
ns_repo := m_namespace.NewRepository()
s := NewSwampFileService(ns_repo, file_repo, 1024, time.Hour)
s := NewSwampFileService(ns_repo, file_repo, 1024, time.Hour, TestLogger{})
fakefile := bytes.NewBufferString("My bog data")
err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len()))

@ -2,7 +2,6 @@ package main
import (
"caj-larsson/bog/server"
"fmt"
"io/ioutil"
)
@ -17,9 +16,6 @@ func main() {
if err != nil {
panic(err)
}
fmt.Printf("Dataswamp running")
bog := server.New(config)
bog.Run()
}

@ -1,15 +1,15 @@
package server
import (
"fmt"
"net/http"
"strconv"
"time"
"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"
"fmt"
"net/http"
"strconv"
"time"
)
type Router interface {
@ -21,6 +21,7 @@ type Bog struct {
router Router
file_service dataswamp.SwampFileService
address string
logger dataswamp.Logger
}
func buildFileDataRepository(config FileConfig) swampfile.Repository {
@ -58,7 +59,7 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
panic(err)
}
b.logger.Info("Serving file '%s' of size %d from '%s'", ref.Path, swamp_file.Size(), ref.UserAgent)
http.ServeContent(w, r, swamp_file.Path(), swamp_file.Modified(), swamp_file)
case "POST":
fallthrough
@ -70,7 +71,7 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(422)
return
}
b.logger.Info("Recieving file '%s' of size %d from '%s'", ref.Path, size, ref.UserAgent)
err = b.file_service.SaveFile(ref, r.Body, size)
if err != nil {
@ -84,7 +85,7 @@ func (b *Bog) routes() {
b.router.HandleFunc("/", b.fileHandler)
}
func (b *Bog) cleanNamespaces(){
func (b *Bog) cleanNamespaces() {
for true {
b.file_service.CleanUpExpiredFiles()
time.Sleep(time.Minute * 10)
@ -98,16 +99,22 @@ func New(config *Configuration) *Bog {
fsSwampRepo := buildFileDataRepository(config.File)
nsRepo := buildNamespaceRepository(config.Database)
logger := ServerLogger{Debug}
b.file_service = dataswamp.NewSwampFileService(
nsRepo, fsSwampRepo, config.Quota.ParsedSizeBytes(), config.Quota.ParsedDuration(),
nsRepo,
fsSwampRepo,
config.Quota.ParsedSizeBytes(),
config.Quota.ParsedDuration(),
logger,
)
b.logger = logger
b.router = new(http.ServeMux)
b.routes()
return b
}
func (b *Bog) Run() {
b.logger.Info("Starting bog on address: %s", b.address)
go b.cleanNamespaces()
http.ListenAndServe(b.address, b.router)
}

@ -20,6 +20,7 @@ func TestApplication(t *testing.T) {
swampfile.NewRepository(),
1000,
time.Hour,
ServerLogger{Debug},
)
bog := Bog{

@ -54,11 +54,16 @@ type DatabaseConfig struct {
Connection string
}
type LoggingConfig struct {
Level string
}
type Configuration struct {
Server ServerConfig
File FileConfig
Database DatabaseConfig
Quota QuotaConfig
Logging LoggingConfig
}
func (c *Configuration) bindAddress() string {

@ -0,0 +1,40 @@
package server
import (
"fmt"
"time"
)
const (
Debug int = 0
Info = 1
Warn = 2
None = 3
)
type ServerLogger struct {
level int
}
func logf(level string, format string, a ...interface{}) {
head := fmt.Sprintf("%s - [%s]: ", time.Now().Format(time.RFC3339), level)
fmt.Printf(head+format+"\n", a...)
}
func (t ServerLogger) Debug(format string, a ...interface{}) {
if t.level <= Debug {
logf("DEBUG", format, a...)
}
}
func (t ServerLogger) Info(format string, a ...interface{}) {
if t.level <= Info {
logf("INFO", format, a...)
}
}
func (t ServerLogger) Warn(format string, a ...interface{}) {
if t.level <= Warn {
logf("WARN", format, a...)
}
}
Loading…
Cancel
Save