/
share.go
68 lines (56 loc) · 1.7 KB
/
share.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
package api
import (
"encoding/json"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/google/uuid"
)
type CachedQueryData struct {
Query string
DatabaseID int64
}
func (a *ScratchDataAPIStruct) CreateQuery(w http.ResponseWriter, r *http.Request) {
var requestBody struct {
Query string `json:"query"`
Duration int `json:"duration"` // Duration in seconds
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid request body"))
return
}
// Validate the query
if requestBody.Query == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Query cannot be empty"))
return
}
destId := a.AuthGetDatabaseID(r.Context())
expires := time.Duration(requestBody.Duration) * time.Second
sharedQueryId, err := a.storageServices.Database.CreateShareQuery(r.Context(), destId, requestBody.Query, expires)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
render.JSON(w, r, render.M{"id": sharedQueryId.String()})
}
func (a *ScratchDataAPIStruct) ShareData(w http.ResponseWriter, r *http.Request) {
queryUUID := chi.URLParam(r, "uuid")
format := chi.URLParam(r, "format")
id, err := uuid.Parse(queryUUID)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
cachedQuery, found := a.storageServices.Database.GetShareQuery(r.Context(), id)
if !found {
http.Error(w, "Query not found", http.StatusNotFound)
return
}
if err := a.executeQueryAndStreamData(r.Context(), w, cachedQuery.Query, cachedQuery.DestinationID, format); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}