// serviceHandler handlers post request to "/jobs" func serviceHandler(w http.ResponseWriter, r *http.Request) { pathlist, requestType, err := parseURI(r, "/jobs/") if err != nil || len(pathlist) != 0 { badRequest(w, "Error: incorrectly formatted request") return } if requestType != "post" { badRequest(w, "only supports posts") return } // read json decoder := json.NewDecoder(r.Body) var json_data map[string]interface{} err = decoder.Decode(&json_data) // convert schema to json data var schema_data interface{} json.Unmarshal([]byte(serviceSchema), &schema_data) // validate json schema schema, err := gojsonschema.NewJsonSchemaDocument(schema_data) validationResult := schema.Validate(json_data) if !validationResult.Valid() { badRequest(w, "JSON did not pass validation") return } var addRequest AddRequest addRequest.num1 = int(json_data["num1"].(float64)) addRequest.num2 = int(json_data["num2"].(float64)) if err != nil { badRequest(w, "JSON not formatted properly") return } jobid := randomHex() var empty interface{} jobResults.Results[jobid] = empty addRequest.name = jobid w.Header().Set("Content-Type", "application/json") jsondata, _ := json.Marshal(map[string]string{ "result-callback": "http://" + webAddress + "/jobs/" + jobid, }) fmt.Fprintf(w, string(jsondata)) // non-blocking call to actual service go addService(addRequest) }
func extractBodies(w http.ResponseWriter, json_data map[string]interface{}, schemaData string) (sparse_bodies sparseBodies, err error) { // convert schema to json data var schema_data interface{} json.Unmarshal([]byte(schemaData), &schema_data) // validate json schema schema, err := gojsonschema.NewJsonSchemaDocument(schema_data) validationResult := schema.Validate(json_data) if !validationResult.Valid() { badRequest(w, "JSON did not pass validation") err = fmt.Errorf("JSON did not pass validation") return } // retrieve dvid server dvidserver, err := getDVIDserver(json_data) if err != nil { badRequest(w, "DVID server could not be located on proxy") return } // get data uuid uuid := json_data["uuid"].(string) // base url for all dvid queries baseurl := dvidserver + "/api/node/" + uuid + "/sp2body/sparsevol/" bodyinter_list := json_data["bodies"].([]interface{}) for _, bodyinter := range bodyinter_list { bodyid := int(bodyinter.(float64)) url := baseurl + strconv.Itoa(bodyid) resp, err2 := http.Get(url) if err2 != nil || resp.StatusCode != 200 { badRequest(w, "Body could not be read from "+url) err = fmt.Errorf("Body could not be read") return } defer resp.Body.Close() // not examing initial body data for now var junk uint32 binary.Read(resp.Body, binary.LittleEndian, &junk) binary.Read(resp.Body, binary.LittleEndian, &junk) var numspans uint32 binary.Read(resp.Body, binary.LittleEndian, &numspans) sparse_body := sparseBody{} sparse_body.bodyID = uint32(bodyid) for iter := 0; iter < int(numspans); iter += 1 { var x, y, z, run int32 err = binary.Read(resp.Body, binary.LittleEndian, &x) if err != nil { badRequest(w, "Sparse body encoding incorrect") return } err = binary.Read(resp.Body, binary.LittleEndian, &y) if err != nil { badRequest(w, "Sparse body encoding incorrect") return } err = binary.Read(resp.Body, binary.LittleEndian, &z) if err != nil { badRequest(w, "Sparse body encoding incorrect") return } err = binary.Read(resp.Body, binary.LittleEndian, &run) if err != nil { badRequest(w, "Sparse body encoding incorrect") return } sparse_data := sparseData{x, y, z, run} sparse_body.rle = append(sparse_body.rle, sparse_data) } sparse_bodies = append(sparse_bodies, sparse_body) } return }