github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/get_item-param-conf-livetest.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/smugmug/godynamo/conf" 7 "github.com/smugmug/godynamo/conf_file" 8 conf_iam "github.com/smugmug/godynamo/conf_iam" 9 get "github.com/smugmug/godynamo/endpoints/get_item" 10 keepalive "github.com/smugmug/godynamo/keepalive" 11 "github.com/smugmug/godynamo/types/attributevalue" 12 "log" 13 "net/http" 14 "os" 15 ) 16 17 func main() { 18 19 // this is the same as get-item-livetest except here we demonstrate using a parameterized conf 20 home := os.Getenv("HOME") 21 home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME 22 home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file) 23 if home_conf_err != nil { 24 panic("cannot read conf from " + home_conf_file) 25 } 26 home_conf.ConfLock.RLock() 27 if home_conf.Initialized == false { 28 panic("conf struct has not been initialized") 29 } 30 31 // launch a background poller to keep conns to aws alive 32 if home_conf.Network.DynamoDB.KeepAlive { 33 log.Printf("launching background keepalive") 34 go keepalive.KeepAlive([]string{}) 35 } 36 37 // deal with iam, or not 38 if home_conf.UseIAM { 39 iam_ready_chan := make(chan bool) 40 go conf_iam.GoIAM(iam_ready_chan) 41 _ = <-iam_ready_chan 42 } 43 home_conf.ConfLock.RUnlock() 44 45 get1 := get.NewGetItem() 46 get1.TableName = "test-godynamo-livetest" 47 // make sure this item has actually been inserted previously 48 get1.Key["TheHashKey"] = &attributevalue.AttributeValue{S: "AHashKey264"} 49 get1.Key["TheRangeKey"] = &attributevalue.AttributeValue{N: "264"} 50 body, code, err := get1.EndpointReqWithConf(home_conf) 51 if err != nil || code != http.StatusOK { 52 fmt.Printf("get failed %d %v %s\n", code, err, body) 53 } 54 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 55 56 resp := get.NewResponse() 57 um_err := json.Unmarshal([]byte(body), resp) 58 if um_err != nil { 59 log.Fatal(um_err) 60 } 61 j, jerr := json.Marshal(resp) 62 if jerr != nil { 63 log.Fatal(jerr) 64 } 65 fmt.Printf("RESP:%s\n", string(j)) 66 67 // Try converting the Response to a ResponseItemJSON 68 c, cerr := resp.ToResponseItemJSON() 69 if cerr != nil { 70 log.Fatal(cerr) 71 } 72 jc, jcerr := json.Marshal(c) 73 if jcerr != nil { 74 log.Fatal(jcerr) 75 } 76 fmt.Printf("JSON:%s\n", string(jc)) 77 }