Configuration structured up a bit

master
Caj Larsson 3 years ago
parent eb40c99220
commit f0c538551c

@ -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)
}

@ -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

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