-
Notifications
You must be signed in to change notification settings - Fork 10
/
db.go
183 lines (155 loc) · 3.16 KB
/
db.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package ddbsync
import (
"bytes"
"encoding/json"
"fmt"
"github.com/bmizerany/aws4"
"io/ioutil"
"net/http"
"os"
"time"
)
const (
opPutItem = "DynamoDB_20111205.PutItem"
opGetItem = "DynamoDB_20111205.GetItem"
opDeleteItem = "DynamoDB_20111205.DeleteItem"
)
type item struct {
Name string
Created int64
}
type responseError struct {
resp *http.Response
}
type s struct {
S string
}
type n struct {
N int64 `json:",string"`
}
type database struct {
keys *aws4.Keys
s *aws4.Service
}
var db = &database{
keys: &aws4.Keys{
AccessKey: os.Getenv("AWS_ACCESS_KEY"),
SecretKey: os.Getenv("AWS_SECRET_KEY"),
},
s: &aws4.Service{
Name: "dynamodb",
Region: "us-east-1",
},
}
func (db *database) put(name string, created int64) error {
type T struct {
TableName string
Item struct {
Name s
Created n
}
Expected struct {
Name struct {
Exists bool
}
}
}
t := new(T)
t.TableName = "Locks"
t.Item.Name.S = name
t.Item.Created.N = created
t.Expected.Name.Exists = false
resp, err := db.do(opPutItem, t)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("updateMinute error: %d %q", resp.StatusCode, string(b))
}
return nil
}
func (db *database) get(name string) (*item, error) {
type T struct {
TableName string
ConsistentRead bool
Key struct {
HashKeyElement s
}
AttributesToGet []string
}
t := new(T)
t.TableName = "Locks"
t.ConsistentRead = true
t.Key.HashKeyElement.S = name
t.AttributesToGet = []string{"Name", "Created"}
resp, err := db.do(opGetItem, t)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("get error: %d %q", resp.StatusCode, string(b))
}
type R struct {
Item struct {
Name s
Created n
}
}
r := new(R)
if err := json.NewDecoder(resp.Body).Decode(r); err != nil {
return nil, err
}
if r.Item.Name.S == "" {
return nil, nil
}
return &item{r.Item.Name.S, r.Item.Created.N}, nil
}
func (db *database) delete(name string) error {
type T struct {
TableName string
Key struct {
HashKeyElement s
}
}
t := new(T)
t.TableName = "Locks"
t.Key.HashKeyElement.S = name
resp, err := db.do(opDeleteItem, t)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("delete error: %d %q", resp.StatusCode, string(b))
}
return nil
}
func (db *database) do(op string, v interface{}) (*http.Response, error) {
b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(v); err != nil {
panic(err)
}
r, _ := http.NewRequest("POST", "https://dynamodb.us-east-1.amazonaws.com/", b)
r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
r.Header.Set("X-Amz-Target", op)
r.Header.Set("Content-Type", "application/x-amz-json-1.0")
err := db.s.Sign(db.keys, r)
if err != nil {
return nil, err
}
return http.DefaultClient.Do(r)
}