-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinker.go
243 lines (196 loc) · 5.69 KB
/
sinker.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/fsnotify/fsnotify"
"github.com/joho/godotenv"
)
var watcher *fsnotify.Watcher
var sinkerApiDeviceId string
// main
func main() {
err := godotenv.Load()
if err != nil {
panic(errors.New("could not load .env file"))
}
sinkerApiDeviceId, err = registerDevice()
if err != nil {
panic(err)
}
// TODO sync before watching
// creates a new file watcher
watcher, _ = fsnotify.NewWatcher()
defer watcher.Close()
go watchPeriodically(os.Getenv("SINKER_BASE_PATH"), 5)
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
handleFsEvent(event)
case err := <-watcher.Errors:
fmt.Println("ERROR", err)
}
}
}()
<-done
}
// watchDir gets run as a walk func, searching for directories to add watchers to
func watchDir(path string, fi os.FileInfo, err error) error {
// since fsnotify can watch all the files in a directory, watchers only need
// to be added to each nested directory
if fi.Mode().IsDir() {
return watcher.Add(path)
}
return nil
}
// watchPeriodically adds sub directories peridically to watch, with the help
// of fsnotify which maintains a directory map rather than slice.
func watchPeriodically(directory string, interval int) {
done := make(chan struct{})
go func() {
done <- struct{}{}
}()
ticker := time.NewTicker(time.Duration(interval) * time.Second)
defer ticker.Stop()
for ; ; <-ticker.C {
<-done
if err := filepath.Walk(directory, watchDir); err != nil {
fmt.Println(err)
}
go func() {
done <- struct{}{}
}()
}
}
// handleFsEvent handles a file system event, uploading a file to S3,
// and updates the state backend
func handleFsEvent(event fsnotify.Event) {
var err error
// Skip CHMOD event as macOS sends 2 for every WRITE event (before and after)
if event.Op.String() == "CHMOD" {
return
}
fmt.Printf("EVENT! %#v\n", event.String())
if event.Op.String() == "CREATE" {
_, err = uploadFile(event.Name)
}
if event.Op.String() == "REMOVE" {
_, err = removeFile(event.Name)
}
if event.Op.String() == "WRITE" {
_, err = uploadFile(event.Name)
}
if err != nil {
fmt.Println("ERROR", err, event.Name)
return
}
_, err = updateState(event)
if err != nil {
fmt.Println("ERROR", err, event.Name)
}
fmt.Println("file updated", event.Name)
}
// updateState updates the state backend
func updateState(event fsnotify.Event) ([]byte, error) {
values := map[string]string{
"path": relativePath(event.Name),
"type": event.Op.String(),
}
jsonValue, _ := json.Marshal(values)
return sinkerApiRequest("POST", os.Getenv("SINKER_API_STORE_EVENT_PATH"), jsonValue)
}
// removeFile removes a file from a given absolute path from the S3 bucket
// specified by the AWS_BUCKET env variable
func removeFile(path string) (*s3.DeleteObjectOutput, error) {
return s3Client().DeleteObject(context.TODO(), &s3.DeleteObjectInput{
Bucket: aws.String(os.Getenv("AWS_BUCKET")),
Key: aws.String(relativePath(path)),
})
}
// uploadFile uploads a file from a given absolute path to the S3 bucket
// specified by the AWS_BUCKET env variable
func uploadFile(path string) (*s3.PutObjectOutput, error) {
file, err := os.Open(path)
if err != nil {
return nil, errors.New("could not open path")
}
return s3Client().PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(os.Getenv("AWS_BUCKET")),
Key: aws.String(relativePath(path)),
Body: file,
})
}
// relativePath returns the relative path of a file from a given aboslute path string
func relativePath(path string) string {
return strings.Trim(strings.Replace(path, os.Getenv("SINKER_BASE_PATH"), "", 1), "/")
}
// s3Client returns a new S3 client
func s3Client() *s3.Client {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic(err)
}
return s3.NewFromConfig(cfg, func(o *s3.Options) {
o.Region = "eu-west-1"
})
}
func registerDevice() (string, error) {
type Device struct {
Uuid string `json:"uuid"`
Name string `json:"name"`
}
type DeviceResponse struct {
Data Device `json:"data"`
}
var response DeviceResponse
values := map[string]string{"name": "Sinker macOS client"}
jsonValue, _ := json.Marshal(values)
body, err := sinkerApiRequest("POST", os.Getenv("SINKER_API_STORE_DEVICE_PATH"), jsonValue)
if err != nil {
return "", err
}
if err := json.Unmarshal(body, &response); err != nil {
return "", err
}
if response.Data.Uuid == "" {
return "", errors.New(fmt.Sprint("Could not register device. ERROR ", string(body)))
}
return string(response.Data.Uuid), nil
}
func sinkerApiRequest(method string, uri string, requestBody []byte) ([]byte, error) {
url := fmt.Sprint(os.Getenv("SINKER_API_BASE_URL"), uri)
fmt.Println("URL:>", url)
req, err := http.NewRequest(method, url, bytes.NewBuffer(requestBody))
if err != nil {
return nil, err
}
req.Header.Set(os.Getenv("SINKER_API_KEY_HEADER_NAME"), os.Getenv("SINKER_API_KEY_HEADER_VALUE"))
req.Header.Set(os.Getenv("SINKER_API_USER_ID_HEADER_NAME"), os.Getenv("SINKER_API_USER_ID_HEADER_VALUE"))
req.Header.Set(os.Getenv("SINKER_API_DEVICE_ID_HEADER_NAME"), sinkerApiDeviceId)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
// fmt.Println("request Headers:", req.Header)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
return body, nil
}