You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package application
|
|
|
|
import (
|
|
"net/http"
|
|
"fmt"
|
|
"io"
|
|
"caj-larsson/bog/domain"
|
|
"caj-larsson/bog/integration"
|
|
)
|
|
|
|
type Bog struct {
|
|
mux *http.ServeMux
|
|
file_service domain.BogFileService
|
|
address string
|
|
}
|
|
|
|
func buildFileDataRepository(config FileConfig) domain.FileDataRepository{
|
|
fsBogRepo := new(integration.FileSystemBogRepository)
|
|
fsBogRepo.Root = config.Path
|
|
return fsBogRepo
|
|
}
|
|
|
|
func buildUserAgentRepository(config DatabaseConfig) *integration.SQLiteUserAgentRepository{
|
|
if config.Backend != "sqlite" {
|
|
panic("Can only handle sqlite")
|
|
}
|
|
return integration.NewSQLiteUserAgentRepository(config.Connection)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
ref := domain.FileReference {r.URL.Path, r.Header["User-Agent"][0]}
|
|
|
|
switch r.Method {
|
|
case "GET":
|
|
bog_file, err := file_service.OpenOutFile(ref)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
http.ServeContent(w, r, bog_file.Path(), bog_file.Modified(), bog_file)
|
|
|
|
case "POST":
|
|
fallthrough
|
|
case "PUT":
|
|
bog_file, err := file_service.CreateOrOpenInFile(ref)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
io.Copy(bog_file, r.Body)
|
|
bog_file.Close()
|
|
}
|
|
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.address, b.mux)
|
|
}
|