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