forked from tailscale/golink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
convex.go
215 lines (196 loc) · 5.36 KB
/
convex.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package golink
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/fs"
"net/http"
"time"
)
type LinkDocument struct {
Id string `json:"normalizedId"`
Short string `json:"short"`
Long string `json:"long"`
Created float64 `json:"created"`
LastEdit float64 `json:"lastEdit"`
Owner string `json:"owner"`
}
type StatsMap = map[string]interface{}
type ConvexDB struct {
url string
token string
}
type UdfExecution struct {
Path string `json:"path"`
Args map[string]interface{} `json:"args"`
Format string `json:"format"`
}
type ConvexResponse struct {
Status string `json:"status"`
Value json.RawMessage `json:"value"`
ErrorMessage string `json:"errorMessage"`
}
func NewConvexDB(url string, token string) *ConvexDB {
return &ConvexDB{url: url, token: token}
}
func (c *ConvexDB) mutation(args *UdfExecution) error {
args.Args["token"] = c.token
url := fmt.Sprintf("%s/api/mutation", c.url)
encodedArgs, err := json.Marshal(args)
if err != nil {
return err
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(encodedArgs))
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("unexpected status code from Convex: %d", resp.StatusCode)
}
defer resp.Body.Close()
var convexResponse ConvexResponse
err = json.NewDecoder(resp.Body).Decode(&convexResponse)
if err != nil {
return err
}
if convexResponse.Status == "success" {
return nil
}
if convexResponse.Status == "error" {
return fmt.Errorf("error from Convex: %s", convexResponse.ErrorMessage)
}
return fmt.Errorf("unexpected response from Convex: %s", resp.Body)
}
func (c *ConvexDB) query(args *UdfExecution) (json.RawMessage, error) {
args.Args["token"] = c.token
url := fmt.Sprintf("%s/api/query", c.url)
encodedArgs, err := json.Marshal(args)
if err != nil {
return nil, err
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(encodedArgs))
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("unexpected status code from Convex: %d: %s", resp.StatusCode, body)
}
defer resp.Body.Close()
var convexResponse ConvexResponse
err = json.NewDecoder(resp.Body).Decode(&convexResponse)
if err != nil {
return nil, err
}
if convexResponse.Status == "success" {
return convexResponse.Value, nil
}
if convexResponse.Status == "error" {
return nil, fmt.Errorf("error from Convex: %s", convexResponse.ErrorMessage)
}
return nil, fmt.Errorf("unexpected response from Convex: %s", resp.Body)
}
func (c *ConvexDB) LoadAll() ([]*Link, error) {
args := UdfExecution{"load:loadAll", map[string]interface{}{}, "json"}
resp, err := c.query(&args)
if err != nil {
return nil, err
}
var docs []LinkDocument
decoder := json.NewDecoder(bytes.NewReader(resp))
decoder.UseNumber()
err = decoder.Decode(&docs)
if err != nil {
return nil, err
}
var links []*Link
for _, doc := range docs {
link := Link{
Short: doc.Short,
Long: doc.Long,
Created: time.Unix(int64(doc.Created), 0),
LastEdit: time.Unix(int64(doc.LastEdit), 0),
Owner: doc.Owner,
}
links = append(links, &link)
}
return links, nil
}
func (c *ConvexDB) Load(short string) (*Link, error) {
args := UdfExecution{"load:loadOne", map[string]interface{}{"normalizedId": linkID(short)}, "json"}
resp, err := c.query(&args)
if err != nil {
return nil, err
}
var doc *LinkDocument
decoder := json.NewDecoder(bytes.NewReader(resp))
decoder.UseNumber()
err = decoder.Decode(&doc)
if err != nil {
return nil, err
}
if doc == nil {
err := fs.ErrNotExist
return nil, err
}
link := Link{
Short: doc.Short,
Long: doc.Long,
Created: time.Unix(int64(doc.Created), 0),
LastEdit: time.Unix(int64(doc.LastEdit), 0),
Owner: doc.Owner,
}
return &link, nil
}
func (c *ConvexDB) Save(link *Link) error {
document := LinkDocument{
Id: linkID(link.Short),
Short: link.Short,
Long: link.Long,
Created: float64(link.Created.Unix()),
LastEdit: float64(link.LastEdit.Unix()),
Owner: link.Owner,
}
args := UdfExecution{"store", map[string]interface{}{"link": document}, "json"}
return c.mutation(&args)
}
func (c *ConvexDB) Delete(short string) error {
args := UdfExecution{"deleteLink", map[string]interface{}{"normalizedId": linkID(short)}, "json"}
return c.mutation(&args)
}
func (c *ConvexDB) LoadStats() (ClickStats, error) {
args := UdfExecution{"stats:loadStats", map[string]interface{}{}, "json"}
response, err := c.query(&args)
if err != nil {
return nil, err
}
var stats StatsMap
decoder := json.NewDecoder(bytes.NewReader(response))
decoder.UseNumber()
err = decoder.Decode(&stats)
if err != nil {
return nil, err
}
clicks := make(ClickStats)
for k, v := range stats {
num, err := v.(json.Number).Float64()
if err != nil {
return nil, err
}
clicks[k] = int(num)
}
return clicks, nil
}
func (c *ConvexDB) SaveStats(stats ClickStats) error {
mungedStats := make(map[string]int)
for id, clicks := range stats {
mungedStats[linkID(id)] = clicks
}
args := UdfExecution{"stats:saveStats", map[string]interface{}{"stats": mungedStats}, "json"}
return c.mutation(&args)
}
func (c *ConvexDB) DeleteStats(short string) error {
args := UdfExecution{"stats:deleteStats", map[string]interface{}{"normalizedId": linkID(short)}, "json"}
return c.mutation(&args)
}