diff --git a/application/bog.go b/application/bog.go index 76d4b6c..507e94d 100644 --- a/application/bog.go +++ b/application/bog.go @@ -3,33 +3,33 @@ package application import ( "net/http" "fmt" - "log" "io" "caj-larsson/bog/domain" "caj-larsson/bog/integration" ) type Bog struct { - config *Configuration mux *http.ServeMux file_service domain.BogFileService + address string } -func New(config *Configuration) *Bog { - b := new(Bog) - b.config = config - log.Print(config) - +func buildFileDataRepository(config FileConfig) domain.FileDataRepository{ fsBogRepo := new(integration.FileSystemBogRepository) - fsBogRepo.Root = "/tmp/datta" - - uaRepo := integration.NewSQLiteUserAgentRepository("sql.db") - - b.file_service = domain.NewBogFileService(*uaRepo, *fsBogRepo) + fsBogRepo.Root = config.Path + return fsBogRepo +} - b.mux = http.NewServeMux() +func buildUserAgentRepository(config DatabaseConfig) *integration.SQLiteUserAgentRepository{ + if config.Backend != "sqlite" { + panic("Can only handle sqlite") + } + return integration.NewSQLiteUserAgentRepository(config.Connection) +} - b.mux.HandleFunc("/",func(w http.ResponseWriter, r *http.Request) { +func buildHttpMux(file_service domain.BogFileService) *http.ServeMux { + mux := http.NewServeMux() + mux.HandleFunc("/",func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { fmt.Fprintf(w, "Hi") return @@ -39,7 +39,7 @@ func New(config *Configuration) *Bog { switch r.Method { case "GET": - bog_file, err := b.file_service.OpenOutFile(ref) + bog_file, err := file_service.OpenOutFile(ref) if err != nil { panic(err) @@ -50,7 +50,7 @@ func New(config *Configuration) *Bog { case "POST": fallthrough case "PUT": - bog_file, err := b.file_service.CreateOrOpenInFile(ref) + bog_file, err := file_service.CreateOrOpenInFile(ref) if err != nil { panic(err) @@ -61,17 +61,20 @@ func New(config *Configuration) *Bog { } return }) + return mux +} +func New(config *Configuration) *Bog { + b := new(Bog) + b.address = config.bindAddress() + fsBogRepo := buildFileDataRepository(config.File) + uaRepo := buildUserAgentRepository(config.Database) + file_service := domain.NewBogFileService(*uaRepo, fsBogRepo) + b.mux = buildHttpMux(file_service) return b } func (b *Bog) Run() { - http.ListenAndServe(b.config.address(), b.mux) -} - - -func CreateFileHandler(w http.ResponseWriter, r *http.Request) { - - + http.ListenAndServe(b.address, b.mux) } diff --git a/application/configuration.go b/application/configuration.go index a32b3c0..a3b7824 100644 --- a/application/configuration.go +++ b/application/configuration.go @@ -6,16 +6,31 @@ import ( ) -type Configuration struct { +type ServerConfig struct { Port int64 Host string +} + +type FileConfig struct { Path string } -func (c *Configuration) address() string { - return fmt.Sprintf("%s:%d", c.Host, c.Port) +type DatabaseConfig struct { + Backend string + Connection string } +type Configuration struct { + Server ServerConfig + File FileConfig + Database DatabaseConfig +} + +func (c *Configuration) bindAddress() string { + return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port) +} + + func ConfigFromToml(toml_data string) (*Configuration, error) { var config Configuration diff --git a/default.toml b/default.toml index 24dccbb..f59008c 100644 --- a/default.toml +++ b/default.toml @@ -1,2 +1,9 @@ -port = 8002 -host = "127.0.0.1" \ No newline at end of file +[server] + port = 8002 + host = "127.0.0.1" +[file] + path = "/tmp/datta2" + +[database] + backend = "sqlite" + connection = "sql.db" \ No newline at end of file