-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi-handlers.go
More file actions
418 lines (353 loc) · 13.7 KB
/
api-handlers.go
File metadata and controls
418 lines (353 loc) · 13.7 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
Copyright 2024 Blnk Finance Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package watch
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/google/uuid"
zlog "github.com/rs/zerolog/log"
)
func handleInject(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
var t Transaction
err := json.NewDecoder(r.Body).Decode(&t)
if err != nil {
zlog.Error().Err(err).Msg("Error decoding request body")
http.Error(w, fmt.Sprintf("Error decoding request body: %v", err), http.StatusBadRequest)
return
}
if t.CreatedAt.IsZero() {
t.CreatedAt = time.Now()
}
if t.TransactionID == "" {
t.TransactionID = uuid.New().String()
}
log := zlog.With().Str("transaction_id", t.TransactionID).Logger()
log.Info().Msg("Received inject request")
t, err = inject(t)
if err != nil {
log.Error().Err(err).Msg("Error processing transaction")
http.Error(w, fmt.Sprintf("Error processing transaction: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"transaction_id": t.TransactionID,
"message": "Call GET /transactions/{id} to fetch the processed transaction or set ALERT_WEBHOOK_URL to get webhooks when the transaction is flagged.",
})
}
func handleBlnkWebhook(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
log := zlog.With().Str("webhook_event", "blnkWebhook").Logger()
var payload struct {
Event string `json:"event"`
Data map[string]interface{} `json:"data"`
}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
zlog.Error().Err(err).Msg("Error decoding Blnk webhook payload")
http.Error(w, "Error decoding webhook payload", http.StatusBadRequest)
return
}
zlog.With().Str("webhook_event", payload.Event).Logger()
log.Info().Msg("Received Blnk webhook event")
transactionData := payload.Data
var t Transaction
transactionBytes, err := json.Marshal(transactionData)
if err != nil {
log.Error().Err(err).Msg("Error marshalling transaction data from webhook")
http.Error(w, "Error processing transaction data in webhook", http.StatusInternalServerError)
return
}
log.Info().Msgf("Transaction data: %s", string(transactionBytes))
err = json.Unmarshal(transactionBytes, &t)
if err != nil {
log.Error().Err(err).Msg("Error unmarshalling transaction data from webhook")
http.Error(w, "Error unmarshalling transaction data", http.StatusBadRequest)
return
}
if t.CreatedAt.IsZero() {
t.CreatedAt = time.Now()
}
if t.TransactionID == "" {
t.TransactionID = uuid.New().String()
log.Warn().Msg("Webhook transaction data missing ID, generated a new one.")
} else {
log = log.With().Str("transaction_id", t.TransactionID).Logger()
}
log.Info().Msg("Processing transaction from Blnk webhook")
t, err = inject(t)
if err != nil {
log.Error().Err(err).Msg("Error processing transaction from webhook")
http.Error(w, fmt.Sprintf("Error processing transaction: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Transaction %s from webhook event '%s' processed successfully", t.TransactionID, payload.Event)
log.Info().Msg("Successfully processed transaction from Blnk webhook")
}
func handleInstructions(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
handleListInstructions(w, r)
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
}
func handleInstructionByID(w http.ResponseWriter, r *http.Request) {
pathParts := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
if len(pathParts) < 2 || pathParts[1] == "" {
http.Error(w, "Missing instruction ID", http.StatusBadRequest)
return
}
instructionIDStr := pathParts[1]
instructionID, err := strconv.ParseInt(instructionIDStr, 10, 64)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid instruction ID: %s", instructionIDStr), http.StatusBadRequest)
return
}
switch r.Method {
case http.MethodGet:
handleGetInstruction(w, r, instructionID)
case http.MethodDelete:
handleDeleteInstruction(w, r, instructionID)
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
}
func handleListInstructions(w http.ResponseWriter, r *http.Request) {
log := zlog.With().Str("handler", "ListInstructions").Logger()
log.Info().Msg("Received request to list instructions")
instructions, err := GetAllInstructions()
if err != nil {
log.Error().Err(err).Msg("Error getting all instructions from store")
http.Error(w, "Failed to retrieve instructions", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(instructions); err != nil {
log.Error().Err(err).Msg("Error encoding instructions list response")
}
}
func handleGetInstruction(w http.ResponseWriter, r *http.Request, id int64) {
log := zlog.With().Str("handler", "GetInstruction").Int64("instruction_id", id).Logger()
log.Info().Msg("Received request to get instruction by ID")
instruction, err := GetInstructionByID(id)
if err != nil {
log.Warn().Err(err).Msg("Error getting instruction by ID from store")
if strings.Contains(err.Error(), "not found") {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
http.Error(w, "Failed to retrieve instruction", http.StatusInternalServerError)
}
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(instruction); err != nil {
log.Error().Err(err).Msg("Error encoding instruction response")
}
}
func handleDeleteInstruction(w http.ResponseWriter, r *http.Request, id int64) {
log := zlog.With().Str("handler", "DeleteInstruction").Int64("instruction_id", id).Logger()
log.Info().Msg("Received request to delete instruction")
err := DeleteInstruction(id)
if err != nil {
log.Error().Err(err).Msg("Error deleting instruction from store")
if strings.Contains(err.Error(), "not found") {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
http.Error(w, "Failed to delete instruction", http.StatusInternalServerError)
}
return
}
w.WriteHeader(http.StatusNoContent)
}
func handleTransactionByID(w http.ResponseWriter, r *http.Request) {
pathParts := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
if len(pathParts) < 2 || pathParts[1] == "" {
http.Error(w, "Missing transaction ID", http.StatusBadRequest)
return
}
transactionID := pathParts[1]
switch r.Method {
case http.MethodGet:
handleGetTransaction(w, r, transactionID)
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
}
func handleGetTransaction(w http.ResponseWriter, r *http.Request, transactionID string) {
log := zlog.With().Str("handler", "GetTransaction").Str("transaction_id", transactionID).Logger()
log.Info().Msg("Received request to get transaction by ID")
transaction, err := getTransactionByID(transactionID)
if err != nil {
log.Warn().Err(err).Msg("Error getting transaction by ID")
if strings.Contains(err.Error(), "not found") {
http.Error(w, "Transaction not found", http.StatusNotFound)
} else {
http.Error(w, "Failed to retrieve transaction", http.StatusInternalServerError)
}
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(transaction); err != nil {
log.Error().Err(err).Msg("Error encoding transaction response")
}
}
func handleCompileAndSaveInstruction(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method. Only POST is allowed.", http.StatusMethodNotAllowed)
return
}
log := zlog.With().Str("handler", "CompileAndSaveInstruction").Logger()
log.Info().Msg("Received request to compile and save instruction script")
var requestBody struct {
Script string `json:"script"`
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
log.Warn().Err(err).Msg("Error decoding /compile-and-save-instruction request body")
http.Error(w, "Invalid request body: could not decode JSON", http.StatusBadRequest)
return
}
if strings.TrimSpace(requestBody.Script) == "" {
log.Warn().Msg("Empty script provided in /compile-and-save-instruction request")
http.Error(w, "Missing required field: script cannot be empty", http.StatusBadRequest)
return
}
ruleName, description, compiledRuleJSON, compileErr := CompileWatchScript(requestBody.Script)
if compileErr != nil {
http.Error(w, fmt.Sprintf("Failed to compile script: %v", compileErr), http.StatusInternalServerError)
return
}
instruction, saveErr := CreateInstructionWithPrecompiledDSL(r.Context(), ruleName, requestBody.Script, description, compiledRuleJSON)
if saveErr != nil {
log.Error().Err(saveErr).Str("rule_name", ruleName).Msg("Failed to save compiled instruction")
if strings.Contains(saveErr.Error(), "UNIQUE constraint failed") || strings.Contains(saveErr.Error(), "already exists") {
http.Error(w, fmt.Sprintf("Failed to save instruction: An instruction with name '%s' already exists. %v", ruleName, saveErr), http.StatusConflict)
} else {
http.Error(w, fmt.Sprintf("Failed to save instruction '%s': %v", ruleName, saveErr), http.StatusInternalServerError)
}
return
}
log.Info().Int64("instruction_id", instruction.ID).Str("rule_name", ruleName).Msg("Instruction compiled and saved successfully")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(instruction); err != nil {
log.Error().Err(err).Int64("instruction_id", instruction.ID).Msg("Error encoding successful /compile-and-save-instruction response")
}
}
type GitStatusResponse struct {
Configured bool `json:"configured"`
RepoURL string `json:"repo_url,omitempty"`
Branch string `json:"branch,omitempty"`
LocalPath string `json:"local_path,omitempty"`
CurrentCommit string `json:"current_commit,omitempty"`
RemoteCommit string `json:"remote_commit,omitempty"`
UpToDate bool `json:"up_to_date"`
LastSync string `json:"last_sync,omitempty"`
Error string `json:"error,omitempty"`
}
func handleGitStatus(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
response := GitStatusResponse{
Configured: globalGitManager != nil,
}
if globalGitManager == nil {
response.Error = "Git repository not configured"
} else {
response.RepoURL = globalGitManager.RepoURL
response.Branch = globalGitManager.Branch
response.LocalPath = globalGitManager.LocalPath
if currentCommit, err := globalGitManager.GetCurrentCommit(); err != nil {
response.Error = fmt.Sprintf("Failed to get current commit: %v", err)
} else {
response.CurrentCommit = currentCommit
}
if remoteCommit, err := globalGitManager.GetRemoteCommit(); err != nil {
zlog.Debug().Err(err).Msg("Failed to get remote commit for status")
} else {
response.RemoteCommit = remoteCommit
response.UpToDate = response.CurrentCommit == response.RemoteCommit
}
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
zlog.Error().Err(err).Msg("Error encoding Git status response")
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
type GitSyncResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
BeforeCommit string `json:"before_commit,omitempty"`
AfterCommit string `json:"after_commit,omitempty"`
ScriptsLoaded int `json:"scripts_loaded,omitempty"`
Error string `json:"error,omitempty"`
}
func handleGitSync(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
response := GitSyncResponse{}
if globalGitManager == nil {
response.Success = false
response.Error = "Git repository not configured"
} else {
beforeCommit, _ := globalGitManager.GetCurrentCommit()
response.BeforeCommit = beforeCommit
if err := globalGitManager.CloneOrUpdate(); err != nil {
response.Success = false
response.Error = fmt.Sprintf("Failed to sync repository: %v", err)
zlog.Error().Err(err).Msg("Manual Git sync failed")
} else {
response.Success = true
response.Message = "Repository synced successfully"
afterCommit, _ := globalGitManager.GetCurrentCommit()
response.AfterCommit = afterCommit
if beforeCommit != afterCommit {
go func() {
processExistingScriptsInDir(globalGitManager.LocalPath)
zlog.Info().Msg("Processed scripts after manual Git sync")
}()
response.Message = "Repository synced and scripts reloaded"
}
zlog.Info().
Str("before", beforeCommit).
Str("after", afterCommit).
Msg("Manual Git sync completed")
}
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
zlog.Error().Err(err).Msg("Error encoding Git sync response")
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}