github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/put_item-more-livetest.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/smugmug/godynamo/conf" 6 "github.com/smugmug/godynamo/conf_file" 7 conf_iam "github.com/smugmug/godynamo/conf_iam" 8 put "github.com/smugmug/godynamo/endpoints/put_item" 9 keepalive "github.com/smugmug/godynamo/keepalive" 10 "github.com/smugmug/godynamo/types/attributevalue" 11 "log" 12 "net/http" 13 "os" 14 "time" 15 ) 16 17 func main() { 18 19 // this is the same as put-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 put1 := put.NewPutItem() 46 put1.TableName = "test-godynamo-livetest" 47 48 k := fmt.Sprintf("hk1000") 49 v := fmt.Sprintf("%v", time.Now().Unix()) 50 put1.Item["TheHashKey"] = &attributevalue.AttributeValue{S: k} 51 put1.Item["TheRangeKey"] = &attributevalue.AttributeValue{N: v} 52 53 i := fmt.Sprintf("%v",1) 54 t := fmt.Sprintf("%v",time.Now().Unix()) 55 put1.Item["UserID"] = &attributevalue.AttributeValue{N:i} 56 put1.Item["Timestamp"] = &attributevalue.AttributeValue{N:t} 57 58 // the Token field is a simple string 59 put1.Item["Token"] = &attributevalue.AttributeValue{S:"a token"} 60 61 // the Location must be created as a "map" 62 location := attributevalue.NewAttributeValue() 63 location.InsertM("Latitude",&attributevalue.AttributeValue{N:"120.01"}) 64 location.InsertM("Longitude",&attributevalue.AttributeValue{N:"50.99"}) 65 put1.Item["Location"] = location 66 67 body, code, err := put1.EndpointReqWithConf(home_conf) 68 if err != nil || code != http.StatusOK { 69 fmt.Printf("put failed %d %v %s\n", code, err, body) 70 } 71 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 72 }