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.
76 lines
1.3 KiB
Go
76 lines
1.3 KiB
Go
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
|
|
fileDataRepo domain.FileDataRepository
|
|
}
|
|
|
|
func New(config *Configuration) *Bog {
|
|
b := new(Bog)
|
|
b.config = config
|
|
log.Print(config)
|
|
|
|
fsBogRepo := new(integration.FileSystemBogRepository)
|
|
fsBogRepo.Root = "/tmp/datta"
|
|
|
|
b.fileDataRepo = *fsBogRepo
|
|
// uaRepo *domain.UserAgentRepository
|
|
|
|
b.mux = http.NewServeMux()
|
|
|
|
b.mux.HandleFunc("/",func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/" {
|
|
fmt.Fprintf(w, "Hi")
|
|
return
|
|
}
|
|
|
|
switch r.Method {
|
|
case "GET":
|
|
bog_file, err := b.fileDataRepo.Open(r.URL.Path, r.Header["User-Agent"][0])
|
|
|
|
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 := b.fileDataRepo.Create(r.URL.Path, r.Header["User-Agent"][0])
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
io.Copy(bog_file, r.Body)
|
|
bog_file.Close()
|
|
}
|
|
return
|
|
})
|
|
|
|
|
|
return b
|
|
}
|
|
|
|
func (b *Bog) Run() {
|
|
http.ListenAndServe(b.config.address(), b.mux)
|
|
}
|
|
|
|
|
|
func CreateFileHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
}
|