Configuration structured up a bit

master
Caj Larsson 3 years ago
parent eb40c99220
commit f0c538551c

@ -3,33 +3,33 @@ package application
import ( import (
"net/http" "net/http"
"fmt" "fmt"
"log"
"io" "io"
"caj-larsson/bog/domain" "caj-larsson/bog/domain"
"caj-larsson/bog/integration" "caj-larsson/bog/integration"
) )
type Bog struct { type Bog struct {
config *Configuration
mux *http.ServeMux mux *http.ServeMux
file_service domain.BogFileService file_service domain.BogFileService
address string
} }
func New(config *Configuration) *Bog { func buildFileDataRepository(config FileConfig) domain.FileDataRepository{
b := new(Bog)
b.config = config
log.Print(config)
fsBogRepo := new(integration.FileSystemBogRepository) fsBogRepo := new(integration.FileSystemBogRepository)
fsBogRepo.Root = "/tmp/datta" fsBogRepo.Root = config.Path
return fsBogRepo
uaRepo := integration.NewSQLiteUserAgentRepository("sql.db") }
b.file_service = domain.NewBogFileService(*uaRepo, *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 == "/" { if r.URL.Path == "/" {
fmt.Fprintf(w, "Hi") fmt.Fprintf(w, "Hi")
return return
@ -39,7 +39,7 @@ func New(config *Configuration) *Bog {
switch r.Method { switch r.Method {
case "GET": case "GET":
bog_file, err := b.file_service.OpenOutFile(ref) bog_file, err := file_service.OpenOutFile(ref)
if err != nil { if err != nil {
panic(err) panic(err)
@ -50,7 +50,7 @@ func New(config *Configuration) *Bog {
case "POST": case "POST":
fallthrough fallthrough
case "PUT": case "PUT":
bog_file, err := b.file_service.CreateOrOpenInFile(ref) bog_file, err := file_service.CreateOrOpenInFile(ref)
if err != nil { if err != nil {
panic(err) panic(err)
@ -61,17 +61,20 @@ func New(config *Configuration) *Bog {
} }
return 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 return b
} }
func (b *Bog) Run() { func (b *Bog) Run() {
http.ListenAndServe(b.config.address(), b.mux) http.ListenAndServe(b.address, b.mux)
}
func CreateFileHandler(w http.ResponseWriter, r *http.Request) {
} }

@ -6,16 +6,31 @@ import (
) )
type Configuration struct { type ServerConfig struct {
Port int64 Port int64
Host string Host string
}
type FileConfig struct {
Path string Path string
} }
func (c *Configuration) address() string { type DatabaseConfig struct {
return fmt.Sprintf("%s:%d", c.Host, c.Port) 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) { func ConfigFromToml(toml_data string) (*Configuration, error) {
var config Configuration var config Configuration

@ -1,2 +1,9 @@
port = 8002 [server]
host = "127.0.0.1" port = 8002
host = "127.0.0.1"
[file]
path = "/tmp/datta2"
[database]
backend = "sqlite"
connection = "sql.db"
Loading…
Cancel
Save