9
9
"net/http"
10
10
"net/url"
11
11
"os"
12
+ "path/filepath"
12
13
"sort"
13
14
14
15
sqlite "github.com/gwenn/gosqlite"
@@ -27,12 +28,15 @@ import (
27
28
// * "dbname" is the name of the database
28
29
func branchesHandler (w http.ResponseWriter , r * http.Request ) {
29
30
// Do auth check, grab request info
30
- _ , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
31
+ loggedInUser , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
31
32
if err != nil {
32
33
jsonErr (w , err .Error (), httpStatus )
33
34
return
34
35
}
35
36
37
+ // Record the api call in our backend database
38
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "branches" , r .Header .Get ("User-Agent" ))
39
+
36
40
// If the database is a live database, we return an error message
37
41
isLive , _ , err := com .CheckDBLive (dbOwner , dbName )
38
42
if err != nil {
@@ -102,6 +106,9 @@ func columnsHandler(w http.ResponseWriter, r *http.Request) {
102
106
return
103
107
}
104
108
109
+ // Record the api call in our backend database
110
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "columns" , r .Header .Get ("User-Agent" ))
111
+
105
112
// Extract the table name
106
113
table , err := com .GetFormTable (r , false )
107
114
if err != nil {
@@ -221,12 +228,15 @@ func columnsHandler(w http.ResponseWriter, r *http.Request) {
221
228
// * "dbname" is the name of the database
222
229
func commitsHandler (w http.ResponseWriter , r * http.Request ) {
223
230
// Do auth check, grab request info
224
- _ , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
231
+ loggedInUser , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
225
232
if err != nil {
226
233
jsonErr (w , err .Error (), httpStatus )
227
234
return
228
235
}
229
236
237
+ // Record the api call in our backend database
238
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "commits" , r .Header .Get ("User-Agent" ))
239
+
230
240
// If the database is a live database, we return an error message
231
241
isLive , _ , err := com .CheckDBLive (dbOwner , dbName )
232
242
if err != nil {
@@ -279,6 +289,13 @@ func databasesHandler(w http.ResponseWriter, r *http.Request) {
279
289
return
280
290
}
281
291
292
+ // Record the api call in our backend database
293
+ operation := "databases"
294
+ if live {
295
+ operation = "LIVE databases"
296
+ }
297
+ com .ApiCallLog (loggedInUser , "" , "" , operation , r .Header .Get ("User-Agent" ))
298
+
282
299
// Retrieve the list of databases in the user account
283
300
var databases []com.DBInfo
284
301
if ! live {
@@ -336,6 +353,9 @@ func deleteHandler(w http.ResponseWriter, r *http.Request) {
336
353
}
337
354
dbOwner := loggedInUser
338
355
356
+ // Record the api call in our backend database
357
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "delete" , r .Header .Get ("User-Agent" ))
358
+
339
359
// Check if the database exists
340
360
exists , err := com .CheckDBPermissions (loggedInUser , dbOwner , dbName , false )
341
361
if err != nil {
@@ -517,6 +537,10 @@ func diffHandler(w http.ResponseWriter, r *http.Request) {
517
537
return
518
538
}
519
539
540
+ // Record the api call in our backend database
541
+ // Note - Lets not bother adding additional api logging fields just for the diff function at this stage
542
+ com .ApiCallLog (loggedInUser , dbOwnerA , dbNameA , "diff" , r .Header .Get ("User-Agent" ))
543
+
520
544
// Check permissions of the first database
521
545
var allowed bool
522
546
allowed , err = com .CheckDBPermissions (loggedInUser , dbOwnerA , dbNameA , false )
@@ -593,6 +617,9 @@ func downloadHandler(w http.ResponseWriter, r *http.Request) {
593
617
return
594
618
}
595
619
620
+ // Record the api call in our backend database
621
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "download" , r .Header .Get ("User-Agent" ))
622
+
596
623
// Return the requested database to the user
597
624
_ , err = com .DownloadDatabase (w , r , dbOwner , dbName , commitID , loggedInUser , "api" )
598
625
if err != nil {
@@ -630,6 +657,9 @@ func executeHandler(w http.ResponseWriter, r *http.Request) {
630
657
return
631
658
}
632
659
660
+ // Record the api call in our backend database
661
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "execute" , r .Header .Get ("User-Agent" ))
662
+
633
663
// Grab the incoming SQLite query
634
664
rawInput := r .FormValue ("sql" )
635
665
var sql string
@@ -709,6 +739,9 @@ func indexesHandler(w http.ResponseWriter, r *http.Request) {
709
739
return
710
740
}
711
741
742
+ // Record the api call in our backend database
743
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "indexes" , r .Header .Get ("User-Agent" ))
744
+
712
745
// Check if the database is a live database, and get the node/queue to send the request to
713
746
isLive , liveNode , err := com .CheckDBLive (dbOwner , dbName )
714
747
if err != nil {
@@ -826,12 +859,15 @@ func indexesHandler(w http.ResponseWriter, r *http.Request) {
826
859
// * "dbname" is the name of the database
827
860
func metadataHandler (w http.ResponseWriter , r * http.Request ) {
828
861
// Do auth check, grab request info
829
- _ , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
862
+ loggedInUser , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
830
863
if err != nil {
831
864
jsonErr (w , err .Error (), httpStatus )
832
865
return
833
866
}
834
867
868
+ // Record the api call in our backend database
869
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "metadata" , r .Header .Get ("User-Agent" ))
870
+
835
871
// If the database is a live database, we return an error message
836
872
isLive , _ , err := com .CheckDBLive (dbOwner , dbName )
837
873
if err != nil {
@@ -886,6 +922,9 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {
886
922
return
887
923
}
888
924
925
+ // Record the api call in our backend database
926
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "query" , r .Header .Get ("User-Agent" ))
927
+
889
928
// Grab the incoming SQLite query
890
929
rawInput := r .FormValue ("sql" )
891
930
query , err := com .CheckUnicode (rawInput )
@@ -958,12 +997,15 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {
958
997
// * "dbname" is the name of the database
959
998
func releasesHandler (w http.ResponseWriter , r * http.Request ) {
960
999
// Do auth check, grab request info
961
- _ , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
1000
+ loggedInUser , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
962
1001
if err != nil {
963
1002
jsonErr (w , err .Error (), httpStatus )
964
1003
return
965
1004
}
966
1005
1006
+ // Record the api call in our backend database
1007
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "releases" , r .Header .Get ("User-Agent" ))
1008
+
967
1009
// If the database is a live database, we return an error message
968
1010
isLive , _ , err := com .CheckDBLive (dbOwner , dbName )
969
1011
if err != nil {
@@ -1033,6 +1075,9 @@ func tablesHandler(w http.ResponseWriter, r *http.Request) {
1033
1075
return
1034
1076
}
1035
1077
1078
+ // Record the api call in our backend database
1079
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "tables" , r .Header .Get ("User-Agent" ))
1080
+
1036
1081
// Check if the database is a live database, and get the node/queue to send the request to
1037
1082
isLive , liveNode , err := com .CheckDBLive (dbOwner , dbName )
1038
1083
if err != nil {
@@ -1108,12 +1153,15 @@ func tablesHandler(w http.ResponseWriter, r *http.Request) {
1108
1153
// * "dbname" is the name of the database
1109
1154
func tagsHandler (w http.ResponseWriter , r * http.Request ) {
1110
1155
// Do auth check, grab request info
1111
- _ , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
1156
+ loggedInUser , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
1112
1157
if err != nil {
1113
1158
jsonErr (w , err .Error (), httpStatus )
1114
1159
return
1115
1160
}
1116
1161
1162
+ // Record the api call in our backend database
1163
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "tags" , r .Header .Get ("User-Agent" ))
1164
+
1117
1165
// If the database is a live database, we return an error message
1118
1166
isLive , _ , err := com .CheckDBLive (dbOwner , dbName )
1119
1167
if err != nil {
@@ -1314,18 +1362,39 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
1314
1362
com .SanitiseLogString (dbOwner ), com .SanitiseLogString (dbName ), numBytes )
1315
1363
1316
1364
// Send a request to the AMQP backend to set up the database there, ready for querying
1317
- err = com .LiveCreateDB (com .AmqpChan , dbOwner , dbName , objectID , com . SetToPrivate )
1365
+ liveNode , err : = com .LiveCreateDB (com .AmqpChan , dbOwner , dbName , objectID )
1318
1366
if err != nil {
1319
1367
log .Println (err )
1320
1368
jsonErr (w , err .Error (), http .StatusInternalServerError )
1321
1369
return
1322
1370
}
1323
1371
1372
+ // Update PG, so it has a record of this database existing and knows the node/queue name for querying it
1373
+ err = com .LiveAddDatabasePG (dbOwner , dbName , objectID , liveNode , com .SetToPrivate )
1374
+ if err != nil {
1375
+ jsonErr (w , err .Error (), http .StatusInternalServerError )
1376
+ return
1377
+ }
1378
+
1379
+ // Enable the watch flag for the uploader for this database
1380
+ err = com .ToggleDBWatch (dbOwner , dbOwner , dbName )
1381
+ if err != nil {
1382
+ jsonErr (w , err .Error (), http .StatusInternalServerError )
1383
+ return
1384
+ }
1385
+
1324
1386
// Upload was successful, so we construct a fake commit ID then return a success message to the user
1325
1387
x = make (map [string ]string )
1326
1388
x ["commit_id" ] = ""
1327
- x ["url" ] = fmt .Sprintf ("/%s" , dbOwner )
1389
+ x ["url" ] = server + filepath .Join ("/" , dbOwner , dbName )
1390
+ }
1391
+
1392
+ // Record the api call in our backend database
1393
+ operation := "upload"
1394
+ if live {
1395
+ operation = "LIVE upload"
1328
1396
}
1397
+ com .ApiCallLog (loggedInUser , loggedInUser , dbName , operation , r .Header .Get ("User-Agent" ))
1329
1398
1330
1399
// Construct the response message
1331
1400
var ok bool
@@ -1366,6 +1435,9 @@ func viewsHandler(w http.ResponseWriter, r *http.Request) {
1366
1435
return
1367
1436
}
1368
1437
1438
+ // Record the api call in our backend database
1439
+ com .ApiCallLog (loggedInUser , loggedInUser , dbName , "views" , r .Header .Get ("User-Agent" ))
1440
+
1369
1441
// Check if the database is a live database, and get the node/queue to send the request to
1370
1442
isLive , liveNode , err := com .CheckDBLive (dbOwner , dbName )
1371
1443
if err != nil {
@@ -1441,12 +1513,15 @@ func viewsHandler(w http.ResponseWriter, r *http.Request) {
1441
1513
// * "dbname" is the name of the database being queried
1442
1514
func webpageHandler (w http.ResponseWriter , r * http.Request ) {
1443
1515
// Authenticate user and collect requested database details
1444
- _ , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
1516
+ loggedInUser , dbOwner , dbName , _ , httpStatus , err := collectInfo (w , r )
1445
1517
if err != nil {
1446
1518
jsonErr (w , err .Error (), httpStatus )
1447
1519
return
1448
1520
}
1449
1521
1522
+ // Record the api call in our backend database
1523
+ com .ApiCallLog (loggedInUser , dbOwner , dbName , "webpage" , r .Header .Get ("User-Agent" ))
1524
+
1450
1525
// Return the database webUI URL to the user
1451
1526
var z com.WebpageResponseContainer
1452
1527
z .WebPage = "https://" + com .Conf .Web .ServerName + "/" + dbOwner + "/" + dbName
0 commit comments