|
|
|
@ -9,8 +9,13 @@ import (
|
|
|
|
|
"caj-larsson/bog/integration"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Router interface {
|
|
|
|
|
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
|
|
|
|
|
ServeHTTP(http.ResponseWriter, *http.Request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Bog struct {
|
|
|
|
|
mux *http.ServeMux
|
|
|
|
|
router Router
|
|
|
|
|
file_service domain.BogFileService
|
|
|
|
|
address string
|
|
|
|
|
}
|
|
|
|
@ -28,52 +33,53 @@ func buildUserAgentRepository(config DatabaseConfig) *integration.SQLiteUserAgen
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
func (b *Bog) fileHandler(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]}
|
|
|
|
|
ref := domain.FileReference {r.URL.Path, r.Header["User-Agent"][0]}
|
|
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
|
case "GET":
|
|
|
|
|
bog_file, err := file_service.OpenOutFile(ref)
|
|
|
|
|
switch r.Method {
|
|
|
|
|
case "GET":
|
|
|
|
|
bog_file, err := b.file_service.OpenOutFile(ref)
|
|
|
|
|
|
|
|
|
|
if err == domain.ErrNotExists {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err == domain.ErrNotExists {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.ServeContent(w, r, bog_file.Path(), bog_file.Modified(), bog_file)
|
|
|
|
|
case "POST":
|
|
|
|
|
fallthrough
|
|
|
|
|
case "PUT":
|
|
|
|
|
size_str := r.Header["Content-Length"][0]
|
|
|
|
|
http.ServeContent(w, r, bog_file.Path(), bog_file.Modified(), bog_file)
|
|
|
|
|
case "POST":
|
|
|
|
|
fallthrough
|
|
|
|
|
case "PUT":
|
|
|
|
|
size_str := r.Header["Content-Length"][0]
|
|
|
|
|
|
|
|
|
|
size, err := strconv.ParseInt(size_str, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(422)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
size, err := strconv.ParseInt(size_str, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(422)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = file_service.SaveFile(ref, r.Body, size)
|
|
|
|
|
err = b.file_service.SaveFile(ref, r.Body, size)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
})
|
|
|
|
|
return mux
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Bog) routes() {
|
|
|
|
|
b.router.HandleFunc("/", b.fileHandler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func New(config *Configuration) *Bog {
|
|
|
|
|
b := new(Bog)
|
|
|
|
|
b.address = config.bindAddress()
|
|
|
|
@ -81,13 +87,15 @@ func New(config *Configuration) *Bog {
|
|
|
|
|
fsBogRepo := buildFileDataRepository(config.File)
|
|
|
|
|
uaRepo := buildUserAgentRepository(config.Database)
|
|
|
|
|
|
|
|
|
|
file_service := domain.NewBogFileService(
|
|
|
|
|
b.file_service = domain.NewBogFileService(
|
|
|
|
|
uaRepo, fsBogRepo, config.Quota.ParsedSizeBytes(), config.Quota.ParsedDuration(),
|
|
|
|
|
)
|
|
|
|
|
b.mux = buildHttpMux(file_service)
|
|
|
|
|
|
|
|
|
|
b.router = new(http.ServeMux)
|
|
|
|
|
b.routes()
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Bog) Run() {
|
|
|
|
|
http.ListenAndServe(b.address, b.mux)
|
|
|
|
|
http.ListenAndServe(b.address, b.router)
|
|
|
|
|
}
|
|
|
|
|