-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp_http_handlers.go
331 lines (281 loc) · 9.23 KB
/
app_http_handlers.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
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"text/template"
"time"
"github.com/gin-gonic/gin"
)
// getPromptsHandler handles the GET /api/prompts endpoint
func getPromptsHandler(c *gin.Context) {
templateMutex.RLock()
defer templateMutex.RUnlock()
// Read the templates from files or use default content
titleTemplateContent, err := os.ReadFile("prompts/title_prompt.tmpl")
if err != nil {
titleTemplateContent = []byte(defaultTitleTemplate)
}
tagTemplateContent, err := os.ReadFile("prompts/tag_prompt.tmpl")
if err != nil {
tagTemplateContent = []byte(defaultTagTemplate)
}
c.JSON(http.StatusOK, gin.H{
"title_template": string(titleTemplateContent),
"tag_template": string(tagTemplateContent),
})
}
// updatePromptsHandler handles the POST /api/prompts endpoint
func updatePromptsHandler(c *gin.Context) {
var req struct {
TitleTemplate string `json:"title_template"`
TagTemplate string `json:"tag_template"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request payload"})
return
}
templateMutex.Lock()
defer templateMutex.Unlock()
// Update title template
if req.TitleTemplate != "" {
t, err := template.New("title").Parse(req.TitleTemplate)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid title template: %v", err)})
return
}
titleTemplate = t
err = os.WriteFile("prompts/title_prompt.tmpl", []byte(req.TitleTemplate), 0644)
if err != nil {
log.Errorf("Failed to write title_prompt.tmpl: %v", err)
}
}
// Update tag template
if req.TagTemplate != "" {
t, err := template.New("tag").Parse(req.TagTemplate)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid tag template: %v", err)})
return
}
tagTemplate = t
err = os.WriteFile("prompts/tag_prompt.tmpl", []byte(req.TagTemplate), 0644)
if err != nil {
log.Errorf("Failed to write tag_prompt.tmpl: %v", err)
}
}
c.Status(http.StatusOK)
}
// getAllTagsHandler handles the GET /api/tags endpoint
func (app *App) getAllTagsHandler(c *gin.Context) {
ctx := c.Request.Context()
tags, err := app.Client.GetAllTags(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error fetching tags: %v", err)})
log.Errorf("Error fetching tags: %v", err)
return
}
c.JSON(http.StatusOK, tags)
}
// documentsHandler handles the GET /api/documents endpoint
func (app *App) documentsHandler(c *gin.Context) {
ctx := c.Request.Context()
documents, err := app.Client.GetDocumentsByTags(ctx, []string{manualTag}, 25)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error fetching documents: %v", err)})
log.Errorf("Error fetching documents: %v", err)
return
}
c.JSON(http.StatusOK, documents)
}
// generateSuggestionsHandler handles the POST /api/generate-suggestions endpoint
func (app *App) generateSuggestionsHandler(c *gin.Context) {
ctx := c.Request.Context()
var suggestionRequest GenerateSuggestionsRequest
if err := c.ShouldBindJSON(&suggestionRequest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request payload: %v", err)})
log.Errorf("Invalid request payload: %v", err)
return
}
results, err := app.generateDocumentSuggestions(ctx, suggestionRequest, log.WithContext(ctx))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error processing documents: %v", err)})
log.Errorf("Error processing documents: %v", err)
return
}
c.JSON(http.StatusOK, results)
}
// updateDocumentsHandler handles the PATCH /api/update-documents endpoint
func (app *App) updateDocumentsHandler(c *gin.Context) {
ctx := c.Request.Context()
var documents []DocumentSuggestion
if err := c.ShouldBindJSON(&documents); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request payload: %v", err)})
log.Errorf("Invalid request payload: %v", err)
return
}
err := app.Client.UpdateDocuments(ctx, documents, app.Database, false)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error updating documents: %v", err)})
log.Errorf("Error updating documents: %v", err)
return
}
c.Status(http.StatusOK)
}
func (app *App) submitOCRJobHandler(c *gin.Context) {
documentIDStr := c.Param("id")
documentID, err := strconv.Atoi(documentIDStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid document ID"})
return
}
// Create a new job
jobID := generateJobID() // Implement a function to generate unique job IDs
job := &Job{
ID: jobID,
DocumentID: documentID,
Status: "pending",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
// Add job to store and queue
jobStore.addJob(job)
jobQueue <- job
// Return the job ID to the client
c.JSON(http.StatusAccepted, gin.H{"job_id": jobID})
}
func (app *App) getJobStatusHandler(c *gin.Context) {
jobID := c.Param("job_id")
job, exists := jobStore.getJob(jobID)
if !exists {
c.JSON(http.StatusNotFound, gin.H{"error": "Job not found"})
return
}
response := gin.H{
"job_id": job.ID,
"status": job.Status,
"created_at": job.CreatedAt,
"updated_at": job.UpdatedAt,
"pages_done": job.PagesDone,
}
if job.Status == "completed" {
response["result"] = job.Result
} else if job.Status == "failed" {
response["error"] = job.Result
}
c.JSON(http.StatusOK, response)
}
func (app *App) getAllJobsHandler(c *gin.Context) {
jobs := jobStore.GetAllJobs()
jobList := make([]gin.H, 0, len(jobs))
for _, job := range jobs {
response := gin.H{
"job_id": job.ID,
"status": job.Status,
"created_at": job.CreatedAt,
"updated_at": job.UpdatedAt,
"pages_done": job.PagesDone,
}
if job.Status == "completed" {
response["result"] = job.Result
} else if job.Status == "failed" {
response["error"] = job.Result
}
jobList = append(jobList, response)
}
c.JSON(http.StatusOK, jobList)
}
// getDocumentHandler handles the retrieval of a document by its ID
func (app *App) getDocumentHandler() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
parsedID, err := strconv.Atoi(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid document ID"})
return
}
document, err := app.Client.GetDocument(c, parsedID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
log.Errorf("Error fetching document: %v", err)
return
}
c.JSON(http.StatusOK, document)
}
}
// Section for local-db actions
func (app *App) getModificationHistoryHandler(c *gin.Context) {
modifications, err := GetAllModifications(app.Database)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve modification history"})
log.Errorf("Failed to retrieve modification history: %v", err)
return
}
c.JSON(http.StatusOK, modifications)
}
func (app *App) undoModificationHandler(c *gin.Context) {
id := c.Param("id")
modID, err := strconv.Atoi(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid modification ID"})
log.Errorf("Invalid modification ID: %v", err)
return
}
modification, err := GetModification(app.Database, uint(modID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve modification"})
log.Errorf("Failed to retrieve modification: %v", err)
return
}
if modification.Undone {
c.JSON(http.StatusBadRequest, gin.H{"error": "Modification has already been undone"})
log.Errorf("Modification has already been undone: %v", id)
return
}
// Ok, we're actually doing the update:
ctx := c.Request.Context()
// Make the document suggestions for UpdateDocuments
var suggestion DocumentSuggestion
suggestion.ID = int(modification.DocumentID)
suggestion.OriginalDocument, err = app.Client.GetDocument(ctx, int(modification.DocumentID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve original document"})
log.Errorf("Failed to retrieve original document: %v", err)
return
}
switch modification.ModField {
case "title":
suggestion.SuggestedTitle = modification.PreviousValue
case "tags":
var tags []string
err := json.Unmarshal([]byte(modification.PreviousValue), &tags)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to unmarshal previous tags"})
log.Errorf("Failed to unmarshal previous tags: %v", err)
return
}
suggestion.SuggestedTags = tags
case "content":
suggestion.SuggestedContent = modification.PreviousValue
default:
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid modification field"})
log.Errorf("Invalid modification field: %v", modification.ModField)
return
}
// Update the document
err = app.Client.UpdateDocuments(ctx, []DocumentSuggestion{suggestion}, app.Database, true)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update document"})
log.Errorf("Failed to update document: %v", err)
return
}
// Successful, so set modification as undone
err = SetModificationUndone(app.Database, modification)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to mark modification as undone"})
return
}
// Else all was ok
c.Status(http.StatusOK)
}