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 } func New(config *Configuration) *Bog { b := new(Bog) b.config = config log.Print(config) fsBogRepo := new(integration.FileSystemBogRepository) fsBogRepo.Root = "/tmp/datta" uaRepo := integration.NewSQLiteUserAgentRepository("sql.db") b.file_service = domain.NewBogFileService(*uaRepo, *fsBogRepo) b.mux = http.NewServeMux() b.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 := b.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 := b.file_service.CreateOrOpenInFile(ref) 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) { }