/
stat.handler.go
45 lines (35 loc) · 970 Bytes
/
stat.handler.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
41
42
43
44
45
package handlers
import (
"net/http"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/morr0/webstat/apps/webstat-main/src/models"
"github.com/morr0/webstat/apps/webstat-main/src/utilities"
)
type CollectStatRequest struct {
Url string
}
func CollectStat(request CollectStatRequest, awsSession session.Session) int {
timestamp := time.Now().UnixMicro()
stat := models.StatModel{
Url: request.Url,
CreatedAt: timestamp,
}
client := dynamodb.New(&awsSession)
dynamoObj, err := dynamodbattribute.MarshalMap(stat)
if err != nil {
return http.StatusBadRequest
}
tableName := utilities.GetStatsTableName()
reqInput := &dynamodb.PutItemInput{
Item: dynamoObj,
TableName: &tableName,
}
_, putErr := client.PutItem(reqInput)
if putErr != nil {
return http.StatusInternalServerError
}
return http.StatusOK
}