From 079c9662ade176ee38d770594dc9735e4dfef034 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 1 Dec 2010 22:34:08 -0800 Subject: [PATCH] go blobserver: return "Received" in JSON Also return text/json mime type, And switch from fmt.Print* to log.Print* --- server/go/blobserver/camlistored.go | 3 ++- server/go/blobserver/enumerate.go | 5 +++-- server/go/blobserver/upload.go | 31 ++++++++++++++++++----------- server/go/http_util/http_util.go | 3 +++ server/go/sigserver/camsigd.go | 2 +- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/server/go/blobserver/camlistored.go b/server/go/blobserver/camlistored.go index e94ecaf02..ab032e6cd 100644 --- a/server/go/blobserver/camlistored.go +++ b/server/go/blobserver/camlistored.go @@ -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, diff --git a/server/go/blobserver/enumerate.go b/server/go/blobserver/enumerate.go index 9904b756f..4043312e4 100644 --- a/server/go/blobserver/enumerate.go +++ b/server/go/blobserver/enumerate.go @@ -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 } diff --git a/server/go/blobserver/upload.go b/server/go/blobserver/upload.go index de2fe3214..9c467da5f 100644 --- a/server/go/blobserver/upload.go +++ b/server/go/blobserver/upload.go @@ -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()) } }() diff --git a/server/go/http_util/http_util.go b/server/go/http_util/http_util.go index e87a62433..bd9174b09 100644 --- a/server/go/http_util/http_util.go +++ b/server/go/http_util/http_util.go @@ -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( diff --git a/server/go/sigserver/camsigd.go b/server/go/sigserver/camsigd.go index be09c6c35..7a4daacb8 100644 --- a/server/go/sigserver/camsigd.go +++ b/server/go/sigserver/camsigd.go @@ -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 {