UserAgent created and used for paths

master
Caj Larsson 3 years ago
parent bb541a8193
commit eb40c99220

@ -12,8 +12,7 @@ import (
type Bog struct { type Bog struct {
config *Configuration config *Configuration
mux *http.ServeMux mux *http.ServeMux
// file_service *domain.BogFileService file_service domain.BogFileService
fileDataRepo domain.FileDataRepository
} }
func New(config *Configuration) *Bog { func New(config *Configuration) *Bog {
@ -24,8 +23,9 @@ func New(config *Configuration) *Bog {
fsBogRepo := new(integration.FileSystemBogRepository) fsBogRepo := new(integration.FileSystemBogRepository)
fsBogRepo.Root = "/tmp/datta" fsBogRepo.Root = "/tmp/datta"
b.fileDataRepo = *fsBogRepo uaRepo := integration.NewSQLiteUserAgentRepository("sql.db")
// uaRepo *domain.UserAgentRepository
b.file_service = domain.NewBogFileService(*uaRepo, *fsBogRepo)
b.mux = http.NewServeMux() b.mux = http.NewServeMux()
@ -35,9 +35,11 @@ func New(config *Configuration) *Bog {
return return
} }
ref := domain.FileReference {r.URL.Path, r.Header["User-Agent"][0]}
switch r.Method { switch r.Method {
case "GET": case "GET":
bog_file, err := b.fileDataRepo.Open(r.URL.Path, r.Header["User-Agent"][0]) bog_file, err := b.file_service.OpenOutFile(ref)
if err != nil { if err != nil {
panic(err) panic(err)
@ -48,7 +50,7 @@ func New(config *Configuration) *Bog {
case "POST": case "POST":
fallthrough fallthrough
case "PUT": case "PUT":
bog_file, err := b.fileDataRepo.Create(r.URL.Path, r.Header["User-Agent"][0]) bog_file, err := b.file_service.CreateOrOpenInFile(ref)
if err != nil { if err != nil {
panic(err) panic(err)

@ -1,8 +1,14 @@
package domain package domain
import "errors"
var (
ErrNoUserAgent = errors.New("that useragent does not exist")
)
type UserAgentRepository interface{ type UserAgentRepository interface{
Migrate() error Migrate() error
CreateUserAgent(useragent UserAgent) (*UserAgent, error) Create(useragent UserAgent) (*UserAgent, error)
All() ([]UserAgent, error) All() ([]UserAgent, error)
GetByName(name string) (*UserAgent, error) GetByName(name string) (*UserAgent, error)
Update(id int64, useragent UserAgent) (*UserAgent, error) Update(id int64, useragent UserAgent) (*UserAgent, error)

@ -1,17 +1,42 @@
package domain package domain
//import "io" import (
// "io"
"time"
"strconv"
)
type BogFileService struct { type BogFileService struct {
user_agent_repo UserAgentRepository user_agent_repo UserAgentRepository
file_data_repo FileDataRepository file_data_repo FileDataRepository
} }
func NewBogFileService(user_agent_repo UserAgentRepository, file_data_repo FileDataRepository) BogFileService {
return BogFileService {user_agent_repo, file_data_repo}
}
func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error) { func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error) {
user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent) user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent)
if err == ErrNotExists {
// TODO make this into a factory method?
new_ua := UserAgent {
0, ref.UserAgent, time.Now(), time.Duration(time.Second * 400), FileSizeQuota {10, 10 },
}
user_agent, err = b.user_agent_repo.Create(new_ua)
if err != nil {
panic(err)
}
}
if err != nil {
panic(err)
}
f, err := b.file_data_repo.Create(ref.Path, string(user_agent.ID))
f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10))
if err != nil { if err != nil {
return nil, err return nil, err
@ -22,8 +47,11 @@ func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error)
func (b BogFileService) OpenOutFile(ref FileReference) (BogOutFile, error) { func (b BogFileService) OpenOutFile(ref FileReference) (BogOutFile, error) {
user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent) user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent)
if err == ErrNotExists {
return nil, err
}
f, err := b.file_data_repo.Open(ref.Path, string(user_agent.ID)) f, err := b.file_data_repo.Open(ref.Path, strconv.FormatInt(user_agent.ID, 10))
if err != nil { if err != nil {
return nil, err return nil, err

@ -11,13 +11,19 @@ type SQLiteUserAgentRepository struct {
db *sql.DB db *sql.DB
} }
func NewSQLiteUserAgentRepository(db *sql.DB) *SQLiteUserAgentRepository { func NewSQLiteUserAgentRepository(filename string) *SQLiteUserAgentRepository {
return &SQLiteUserAgentRepository{ db, err := sql.Open("sqlite3", filename)
if err != nil {
panic(err)
}
repo := SQLiteUserAgentRepository{
db: db, db: db,
} }
repo.Migrate()
return &repo
} }
func (r *SQLiteUserAgentRepository) Migrate() error { func (r SQLiteUserAgentRepository) Migrate() error {
query := ` query := `
CREATE TABLE IF NOT EXISTS useragent( CREATE TABLE IF NOT EXISTS useragent(
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -28,12 +34,11 @@ func (r *SQLiteUserAgentRepository) Migrate() error {
quota_usage_kb bigint quota_usage_kb bigint
); );
` `
_, err := r.db.Exec(query) _, err := r.db.Exec(query)
return err return err
} }
func (r *SQLiteUserAgentRepository) Create(useragent domain.UserAgent) (*domain.UserAgent, error) { func (r SQLiteUserAgentRepository) Create(useragent domain.UserAgent) (*domain.UserAgent, error) {
var record, err = fromEntity(useragent) var record, err = fromEntity(useragent)
if err != nil { if err != nil {
@ -62,7 +67,7 @@ func (r *SQLiteUserAgentRepository) Create(useragent domain.UserAgent) (*domain.
return &useragent, nil return &useragent, nil
} }
func (r *SQLiteUserAgentRepository) All() ([]domain.UserAgent, error) { func (r SQLiteUserAgentRepository) All() ([]domain.UserAgent, error) {
rows, err := r.db.Query("SELECT * FROM useragent") rows, err := r.db.Query("SELECT * FROM useragent")
if err != nil { if err != nil {
return nil, err return nil, err
@ -85,8 +90,8 @@ func (r *SQLiteUserAgentRepository) All() ([]domain.UserAgent, error) {
return all, nil return all, nil
} }
func (r *SQLiteUserAgentRepository) GetByName(name string) (*domain.UserAgent, error) { func (r SQLiteUserAgentRepository) GetByName(name string) (*domain.UserAgent, error) {
row := r.db.QueryRow("SELECT id, name, lastseen, allowance_time, allowance_kb FROM useragent WHERE name = ?", name) row := r.db.QueryRow("SELECT id, name, lastseen, allowance_time, quota_kb, quota_usage_kb FROM useragent WHERE name = ?", name)
var record UserAgentDBRecord var record UserAgentDBRecord
if err := row.Scan(&record.ID, &record.Name, &record.LastSeen, &record.AllowanceSeconds, &record.QuotaKB, &record.QuotaUsedKB); err != nil { if err := row.Scan(&record.ID, &record.Name, &record.LastSeen, &record.AllowanceSeconds, &record.QuotaKB, &record.QuotaUsedKB); err != nil {
@ -104,7 +109,7 @@ func (r *SQLiteUserAgentRepository) GetByName(name string) (*domain.UserAgent, e
return useragent, nil return useragent, nil
} }
func (r *SQLiteUserAgentRepository) Update(id int64, updated domain.UserAgent) (*domain.UserAgent, error) { func (r SQLiteUserAgentRepository) Update(id int64, updated domain.UserAgent) (*domain.UserAgent, error) {
if id == 0 { if id == 0 {
return nil, errors.New("invalid updated ID") return nil, errors.New("invalid updated ID")
} }
@ -130,7 +135,7 @@ func (r *SQLiteUserAgentRepository) Update(id int64, updated domain.UserAgent) (
return &updated, nil return &updated, nil
} }
func (r *SQLiteUserAgentRepository) Delete(id int64) error { func (r SQLiteUserAgentRepository) Delete(id int64) error {
res, err := r.db.Exec("DELETE FROM useragent WHERE id = ?", id) res, err := r.db.Exec("DELETE FROM useragent WHERE id = ?", id)
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save