go blobserver: return "Received" in JSON

Also return text/json mime type,

And switch from fmt.Print* to log.Print*
This commit is contained in:
Brad Fitzpatrick 2010-12-01 22:34:08 -08:00
parent c323cb2ce5
commit 079c9662ad
5 changed files with 28 additions and 16 deletions

View File

@ -10,6 +10,7 @@ import (
"flag"
"fmt"
"http"
"log"
"os"
)
@ -83,7 +84,7 @@ func main() {
mux.HandleFunc("/camli/", handleCamli)
mux.Handle("/js/", http.FileServer("../../clients/js", "/js/"))
fmt.Printf("Starting to listen on http://%v/\n", *listen)
log.Printf("Starting to listen on http://%v/\n", *listen)
err := http.ListenAndServe(*listen, mux)
if err != nil {
fmt.Fprintf(os.Stderr,

View File

@ -4,6 +4,7 @@ import (
"fmt"
"http"
"os"
"log"
"sort"
"strconv"
"strings"
@ -21,14 +22,14 @@ func readBlobs(ch chan *blobInfo, blobPrefix, diskRoot, after string, remain *ui
dirFullPath := *flagStorageRoot + "/" + diskRoot
dir, err := os.Open(dirFullPath, os.O_RDONLY, 0)
if err != nil {
fmt.Println("Error opening directory: ", err)
log.Println("Error opening directory: ", err)
ch <- &blobInfo{Error: err}
return
}
defer dir.Close()
names, err := dir.Readdirnames(32768)
if err != nil {
fmt.Println("Error reading dirnames: ", err)
log.Println("Error reading dirnames: ", err)
ch <- &blobInfo{Error: err}
return
}

View File

@ -6,6 +6,7 @@ import (
"http"
"io"
"io/ioutil"
"log"
"os"
)
@ -32,37 +33,43 @@ func handleMultiPartUpload(conn http.ResponseWriter, req *http.Request) {
for {
part, err := multipart.NextPart()
if err != nil {
fmt.Println("Error reading multipart section:", err)
log.Println("Error reading multipart section:", err)
break
}
if part == nil {
break
}
formName := part.FormName()
fmt.Printf("New value [%s], part=%v\n", formName, part)
log.Printf("New value [%s], part=%v\n", formName, part)
ref := ParseBlobRef(formName)
if ref == nil {
fmt.Printf("Ignoring form key [%s]\n", formName)
log.Printf("Ignoring form key [%s]\n", formName)
continue
}
blobGot, err := receiveBlob(ref, part)
if err != nil {
fmt.Printf("Error receiving blob %v: %v\n", ref, err)
log.Printf("Error receiving blob %v: %v\n", ref, err)
break
}
fmt.Printf("Received blob %v\n", blobGot)
log.Printf("Received blob %v\n", blobGot)
receivedBlobs = append(receivedBlobs, blobGot)
}
fmt.Println("Done reading multipart body.")
for _, got := range receivedBlobs {
fmt.Printf("Got blob: %v\n", got)
}
// TODO: put the blobs in the JSON here
log.Println("Done reading multipart body.")
ret := commonUploadResponse(req)
received := make([]map[string]interface{}, 0)
for _, got := range receivedBlobs {
log.Printf("Got blob: %v\n", got)
blob := make(map[string]interface{})
blob["blobRef"] = got.blobRef.String()
blob["size"] = got.size
received = append(received, blob)
}
ret["received"] = received
http_util.ReturnJson(conn, ret)
}
@ -96,7 +103,7 @@ func receiveBlob(blobRef *BlobRef, source io.Reader) (blobGot *receivedBlob, err
success := false // set true later
defer func() {
if !success {
fmt.Println("Removing temp file: ", tempFile.Name())
log.Println("Removing temp file: ", tempFile.Name())
os.Remove(tempFile.Name())
}
}()

View File

@ -5,10 +5,12 @@ import (
"http"
"json"
"os"
"log"
)
func BadRequestError(conn http.ResponseWriter, errorMessage string) {
conn.WriteHeader(http.StatusBadRequest)
log.Printf("Bad request: %s", errorMessage)
fmt.Fprintf(conn, "%s\n", errorMessage)
}
@ -18,6 +20,7 @@ func ServerError(conn http.ResponseWriter, err os.Error) {
}
func ReturnJson(conn http.ResponseWriter, data interface{}) {
conn.SetHeader("Content-Type", "text/javascript")
bytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
BadRequestError(conn, fmt.Sprintf(

View File

@ -76,7 +76,7 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handleRoot)
mux.HandleFunc("/camli/sig/", handleCamliSig)
fmt.Printf("Starting to listen on http://%v/\n", *listen)
log.Printf("Starting to listen on http://%v/\n", *listen)
listener, err := net.Listen("tcp", *listen)
if err != nil {