-
-
Save israelb/6fcdfe0bfdff9fc0c48e3d0770962c09 to your computer and use it in GitHub Desktop.
Connect local dynamodb using Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
) | |
type appContext struct { | |
sess *session.Session | |
dbSvc *dynamodb.DynamoDB | |
} | |
func init() { | |
log.SetPrefix("appxxx => ") | |
log.SetFlags(log.Lmicroseconds | log.Lshortfile) | |
valid.SetFieldsRequiredByDefault(true) | |
} | |
func main() { | |
sess, err := session.NewSession(&aws.Config{ | |
Region: aws.String("us-west-2"), | |
Endpoint: aws.String("http://localhost:8000")}) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
dbSvc := dynamodb.New(sess) | |
result, err := dbSvc.ListTables(&dynamodb.ListTablesInput{}) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
log.Println("Tables:") | |
for _, table := range result.TableNames { | |
log.Println(*table) | |
} | |
router := NewRouter(&appContext{sess: sess, dbSvc: dbSvc}) | |
// Start web server | |
log.Println("Starting www...") | |
log.Fatal(http.ListenAndServe(":9001", router)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment