Пример #1
0
func main() {
	conf_file.Read()
	conf.Vals.ConfLock.RLock()
	if conf.Vals.Initialized == false {
		panic("the conf.Vals global conf struct has not been initialized")
	}

	// launch a background poller to keep conns to aws alive
	if conf.Vals.Network.DynamoDB.KeepAlive {
		log.Printf("launching background keepalive")
		go keepalive.KeepAlive([]string{})
	}

	// deal with iam, or not
	if conf.Vals.UseIAM {
		iam_ready_chan := make(chan bool)
		go conf_iam.GoIAM(iam_ready_chan)
		_ = <-iam_ready_chan
	}
	conf.Vals.ConfLock.RUnlock()

	get1 := get.NewGetItem()
	get1.TableName = "test-godynamo-livetest"
	// make sure this item has actually been inserted previously
	get1.Key["TheHashKey"] = &attributevalue.AttributeValue{S: "AHashKey264"}
	get1.Key["TheRangeKey"] = &attributevalue.AttributeValue{N: "264"}
	body, code, err := get1.EndpointReq()
	if err != nil || code != http.StatusOK {
		fmt.Printf("get failed %d %v %s\n", code, err, body)
	}
	fmt.Printf("%v\n%v\n,%v\n", string(body), code, err)

	resp := get.NewResponse()
	um_err := json.Unmarshal([]byte(body), resp)
	if um_err != nil {
		log.Fatal(um_err)
	}
	j, jerr := json.Marshal(resp)
	if jerr != nil {
		log.Fatal(jerr)
	}
	fmt.Printf("RESP:%s\n", string(j))

	// Try converting the Response to a ResponseItemJSON
	c, cerr := resp.ToResponseItemJSON()
	if cerr != nil {
		log.Fatal(cerr)
	}
	jc, jcerr := json.Marshal(c)
	if jcerr != nil {
		log.Fatal(jcerr)
	}
	fmt.Printf("JSON:%s\n", string(jc))
}
Пример #2
0
// BBPD-only endpoint.
// GetItemJSONHandler issues a GetItem request to aws and then transforms the Response into
// a ResponseItemJSON.
func GetItemJSONHandler(w http.ResponseWriter, req *http.Request) {
	if bbpd_runinfo.BBPDAbortIfClosed(w) {
		return
	}
	start := time.Now()
	bodybytes, read_err := ioutil.ReadAll(req.Body)
	req.Body.Close()
	if read_err != nil && read_err != io.EOF {
		e := fmt.Sprintf("get_item_route.GetItemJSONHandler err reading req body: %s", read_err.Error())
		log.Printf(e)
		http.Error(w, e, http.StatusInternalServerError)
		return
	}

	resp_body, code, resp_err := authreq.RetryReqJSON_V4(bodybytes, get.GETITEM_ENDPOINT)

	if resp_err != nil {
		e := fmt.Sprintf("get_item_route.GetItemJSONHandler: resp err calling %s err %s (input json: %s)",
			get.GETITEM_ENDPOINT, resp_err.Error(), string(bodybytes))
		log.Printf(e)
		http.Error(w, e, http.StatusInternalServerError)
		return
	}

	if ep.HttpErr(code) {
		e := fmt.Sprintf("get_item_route.GetItemJSONHandler: http err %d calling %s (input json: %s)",
			code, get.GETITEM_ENDPOINT, string(bodybytes))
		route_response.WriteError(w, code, e, resp_body)
		return
	}

	// translate the Response to a ResponseItemJSON
	resp := get.NewResponse()
	um_err := json.Unmarshal([]byte(resp_body), resp)
	if um_err != nil {
		e := fmt.Sprintf("get_item_route.GetItemJSONHandler:err %s",
			um_err.Error())
		log.Printf(e)
		http.Error(w, e, http.StatusInternalServerError)
		return
	}
	resp_json, rerr := resp.ToResponseItemJSON()
	if rerr != nil {
		e := fmt.Sprintf("get_item_route.GetItemJSONHandler:err %s",
			rerr.Error())
		log.Printf(e)
		http.Error(w, e, http.StatusInternalServerError)
		return
	}
	json_body, jerr := json.Marshal(resp_json)
	if jerr != nil {
		e := fmt.Sprintf("get_item_route.GetItemJSONHandler:err %s",
			jerr.Error())
		log.Printf(e)
		http.Error(w, e, http.StatusInternalServerError)
		return
	}
	mr_err := route_response.MakeRouteResponse(
		w,
		req,
		json_body,
		http.StatusOK,
		start,
		get.ENDPOINT_NAME)
	if mr_err != nil {
		e := fmt.Sprintf("get_item_route.GetItemJSONHandler %s",
			mr_err.Error())
		log.Printf(e)
		http.Error(w, e, http.StatusInternalServerError)
		return
	}
}
func main() {

	// this is the same as get-item-livetest except here we demonstrate using a parameterized conf
	home := os.Getenv("HOME")
	home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME
	home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file)
	if home_conf_err != nil {
		panic("cannot read conf from " + home_conf_file)
	}
	home_conf.ConfLock.RLock()
	if home_conf.Initialized == false {
		panic("conf struct has not been initialized")
	}

	// launch a background poller to keep conns to aws alive
	if home_conf.Network.DynamoDB.KeepAlive {
		log.Printf("launching background keepalive")
		go keepalive.KeepAlive([]string{})
	}

	// deal with iam, or not
	if home_conf.UseIAM {
		iam_ready_chan := make(chan bool)
		go conf_iam.GoIAM(iam_ready_chan)
		_ = <-iam_ready_chan
	}
	home_conf.ConfLock.RUnlock()

	get1 := get.NewGetItem()
	get1.TableName = "test-godynamo-livetest"
	// make sure this item has actually been inserted previously
	get1.Key["TheHashKey"] = &attributevalue.AttributeValue{S: "AHashKey264"}
	get1.Key["TheRangeKey"] = &attributevalue.AttributeValue{N: "264"}
	body, code, err := get1.EndpointReqWithConf(home_conf)
	if err != nil || code != http.StatusOK {
		fmt.Printf("get failed %d %v %s\n", code, err, body)
	}
	fmt.Printf("%v\n%v\n,%v\n", string(body), code, err)

	resp := get.NewResponse()
	um_err := json.Unmarshal([]byte(body), resp)
	if um_err != nil {
		log.Fatal(um_err)
	}
	j, jerr := json.Marshal(resp)
	if jerr != nil {
		log.Fatal(jerr)
	}
	fmt.Printf("RESP:%s\n", string(j))

	// Try converting the Response to a ResponseItemJSON
	c, cerr := resp.ToResponseItemJSON()
	if cerr != nil {
		log.Fatal(cerr)
	}
	jc, jcerr := json.Marshal(c)
	if jcerr != nil {
		log.Fatal(jcerr)
	}
	fmt.Printf("JSON:%s\n", string(jc))
}