UserAgent created and used for paths

master
Caj Larsson 3 years ago
parent bb541a8193
commit eb40c99220

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

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

@ -1,17 +1,42 @@
package domain
//import "io"
import (
// "io"
"time"
"strconv"
)
type BogFileService struct {
user_agent_repo UserAgentRepository
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) {
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)
}
}
f, err := b.file_data_repo.Create(ref.Path, string(user_agent.ID))
if err != nil {
panic(err)
}
f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10))
if err != nil {
return nil, err
@ -22,8 +47,11 @@ func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error)
func (b BogFileService) OpenOutFile(ref FileReference) (BogOutFile, error) {
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 {
return nil, err

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

Loading…
Cancel
Save