@@ -22,6 +22,13 @@ const databasePath = databaseFolder + 'main.db'
22
22
23
23
const apiPort = process . env . D3D12INFODB_CUSTOM_PORT ? process . env . D3D12INFODB_CUSTOM_PORT : 50854 ;
24
24
25
+ function GetCurrentTimeAsString ( ) {
26
+ return "" + ( new Date ( ) ) . valueOf ( )
27
+ }
28
+
29
+ let startupTime = GetCurrentTimeAsString ( )
30
+ let databaseModificationTime = startupTime
31
+
25
32
function isObjectAllowedInDB ( inObj ) {
26
33
let isAllowed = true ;
27
34
database_common . submitRequiredProperites . forEach ( p => {
@@ -49,12 +56,20 @@ console.log(`Opened database ${databasePath}`)
49
56
50
57
upgrade . upgradeDatabase ( db )
51
58
59
+ let latestReportID = 0
60
+ let latestReportQuery = db . prepare ( "SELECT ID FROM Submissions ORDER BY ID DESC LIMIT 1" ) . get ( )
61
+ if ( latestReportQuery ) {
62
+ latestReportID = latestReportQuery . ID
63
+ console . log ( `Latest report ID is ${ latestReportID } ` )
64
+ }
65
+
52
66
console . log ( "Database is ready" )
53
67
54
68
api . use ( express . json ( ) )
55
69
api . use ( express . urlencoded ( { extended : true } ) )
56
70
api . use ( cors ( ) )
57
71
api . use ( compression ( ) )
72
+ api . disable ( 'etag' )
58
73
59
74
api . get ( '/' , ( req , res ) => {
60
75
res . send ( 'Server is up' )
@@ -68,10 +83,25 @@ api.get('/get_submission', (req, res) => {
68
83
return
69
84
}
70
85
86
+ if ( isNaN ( req . query . ID ) ) {
87
+ res . status ( 400 )
88
+ res . send ( 'ID is not a number' )
89
+ return
90
+ }
91
+
92
+ let etag = req . query . ID > latestReportID ? "0" : startupTime
93
+ if ( req . headers [ 'if-none-match' ] == etag ) {
94
+ res . status ( 304 )
95
+ res . send ( )
96
+ return
97
+ }
98
+
71
99
try {
72
100
let rows = getSubmissionStatement . all ( [ req . query . ID ] )
73
101
rows = rows . map ( database_common . unpackDatabaseObject )
74
- res . header ( "Cache-Control" , "public, max-age=300" )
102
+
103
+ res . header ( "Cache-Control" , "public, max-age=120" )
104
+ res . header ( "ETag" , etag )
75
105
res . send ( JSON . stringify ( rows ) )
76
106
}
77
107
catch ( e ) {
@@ -86,10 +116,19 @@ api.get('/get_submission', (req, res) => {
86
116
87
117
const getAllSubmissionsStatement = db . prepare ( "SELECT * FROM Submissions" )
88
118
api . get ( '/get_all_submissions' , ( req , res ) => {
119
+ let etag = databaseModificationTime
120
+ if ( req . headers [ 'if-none-match' ] == etag ) {
121
+ res . status ( 304 )
122
+ res . send ( )
123
+ return
124
+ }
125
+
89
126
try {
127
+
90
128
let rows = getAllSubmissionsStatement . all ( )
91
129
rows = rows . map ( database_common . unpackDatabaseObject )
92
- res . header ( "Cache-Control" , "public, max-age=300" )
130
+ res . header ( "Cache-Control" , "public, max-age=120" )
131
+ res . header ( "ETag" , databaseModificationTime )
93
132
res . send ( JSON . stringify ( rows ) )
94
133
}
95
134
catch ( e ) {
@@ -166,6 +205,8 @@ api.post('/post_submission', (req, res) => {
166
205
let info = postSubmissionStatement . run ( parameterList )
167
206
168
207
res . send ( "" + info . lastInsertRowid )
208
+ databaseModificationTime = GetCurrentTimeAsString ( )
209
+ latestReportID = info . lastInsertRowid
169
210
170
211
console . log ( `Inserted submission ID ${ info . lastInsertRowid } - ${ newSubmission [ "DXGI_ADAPTER_DESC3.Description" ] } ` )
171
212
0 commit comments