-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sync.go
187 lines (155 loc) · 4.86 KB
/
sync.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
package http
import (
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/SyncYomi/SyncYomi/internal/domain"
"github.com/go-chi/chi/v5"
)
type syncService interface {
Store(ctx context.Context, sync *domain.Sync) (*domain.Sync, error)
Delete(ctx context.Context, id int) error
Update(ctx context.Context, sync *domain.Sync) (*domain.Sync, error)
ListSyncs(ctx context.Context, apiKey string) ([]domain.Sync, error)
GetSyncByApiKey(ctx context.Context, apiKey string) (*domain.Sync, error)
GetSyncByDeviceID(ctx context.Context, deviceID int) (*domain.Sync, error)
SyncData(ctx context.Context, sync *domain.SyncData) (*domain.SyncData, error)
}
type syncHandler struct {
encoder encoder
syncService syncService
}
func newSyncHandler(encoder encoder, syncService syncService) *syncHandler {
return &syncHandler{
encoder: encoder,
syncService: syncService,
}
}
func (h syncHandler) Routes(r chi.Router) {
r.Post("/", h.store)
r.Delete("/{id}", h.delete)
r.Get("/", h.listSyncs)
r.Get("/device/{id}", h.getSyncByDeviceID)
r.Get("/{apiKey}", h.getSyncByApiKey)
r.Post("/data", h.sync)
}
func (h syncHandler) store(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
sync domain.Sync
)
if err := json.NewDecoder(r.Body).Decode(&sync); err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusBadRequest)
return
}
// check/try to store sync in database
store, err := h.syncService.Store(ctx, &sync)
if err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusInternalServerError)
return
}
h.encoder.StatusResponse(ctx, w, store, http.StatusOK)
}
func (h syncHandler) delete(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
syncId = chi.URLParam(r, "id")
)
// check if id is an integer
id, _ := strconv.Atoi(syncId)
// check/try to delete sync from database
err := h.syncService.Delete(ctx, id)
if err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusInternalServerError)
return
}
h.encoder.StatusResponse(ctx, w, "Sync deleted", http.StatusOK)
}
func (h syncHandler) listSyncs(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
apiKey = r.Header.Get("X-API-Token")
)
// check/try to get syncs from database
syncs, err := h.syncService.ListSyncs(ctx, apiKey)
if err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusInternalServerError)
return
}
h.encoder.StatusResponse(ctx, w, syncs, http.StatusOK)
}
func (h syncHandler) getSyncByDeviceID(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
deviceId = chi.URLParam(r, "id")
)
// check if id is an integer
id, _ := strconv.Atoi(deviceId)
// check/try to get sync from database
sync, err := h.syncService.GetSyncByDeviceID(ctx, id)
if err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusInternalServerError)
return
}
h.encoder.StatusResponse(ctx, w, sync, http.StatusOK)
}
func (h syncHandler) getSyncByApiKey(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
apiKey = chi.URLParam(r, "apiKey")
)
// check/try to get sync from database
sync, err := h.syncService.GetSyncByApiKey(ctx, apiKey)
if err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusInternalServerError)
return
}
h.encoder.StatusResponse(ctx, w, sync, http.StatusOK)
}
func (h syncHandler) sync(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
sync = domain.SyncData{}
apiKey = r.Header.Get("X-API-Token")
)
// check if api key is set
if apiKey == "" {
h.encoder.StatusResponse(ctx, w, "No API key set", http.StatusBadRequest)
return
}
sync.Sync = &domain.Sync{
UserApiKey: &domain.APIKey{Key: apiKey},
Device: &domain.Device{UserApiKey: &domain.APIKey{Key: apiKey}},
}
sync.Data = &domain.MangaData{UserApiKey: &domain.APIKey{Key: apiKey}}
sync.Device = &domain.Device{UserApiKey: &domain.APIKey{Key: apiKey}}
// Read data from request body
requestData, err := io.ReadAll(r.Body)
if err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusBadRequest)
return
}
// Check if the Content-Encoding header is set to "gzip" and uncompress the data if necessary
if r.Header.Get("Content-Encoding") == "gzip" {
uncompressedData, err := uncompress(requestData)
if err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusBadRequest)
return
}
requestData = uncompressedData
}
// Decode JSON from uncompressed data
if err := json.Unmarshal(requestData, &sync); err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusBadRequest)
return
}
// Store, check, and try to sync data
syncResult, err := h.syncService.SyncData(ctx, &sync)
if err != nil {
h.encoder.StatusResponse(ctx, w, err.Error(), http.StatusInternalServerError)
return
}
h.encoder.StatusResponse(ctx, w, syncResult, http.StatusOK)
}