-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
40 lines (29 loc) · 802 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/guregu/dynamo"
)
type MyVisitorCounter struct {
Path string // Hash key, a.k.a. partition key
VisitorCount int
}
func GetAndUpdateCount() (int, error) {
db := dynamo.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})
table := db.Table("MyVisitorCounter")
var result MyVisitorCounter
err := table.Update("Path", "countergolang").
SetExpr("SET VisitorCount = VisitorCount + ?", 1).
Value(&result)
return result.VisitorCount, err
}
func HandleRequest(ctx context.Context) (string, error) {
count, _ := GetAndUpdateCount()
return fmt.Sprintln(count), nil
}
func main() {
lambda.Start(HandleRequest)
}