@@ -78,30 +78,9 @@ if (process.env.AUTO_PROMOTE) {
78
78
const q = faunadb . query ;
79
79
const client = new faunadb . Client ( { secret : PROD ? process . env . FAUNA_KEY : process . env . FAUNA_DEV_KEY } ) ;
80
80
81
- app . enable ( 'trust proxy' ) ; // trust heroku and cloudflare
82
- app . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
83
- app . use ( bodyParser . json ( ) ) ;
84
-
85
-
86
- let cachedIndexHtml = '' ;
87
-
88
- const updateIndexHtmlCache = async ( ) => {
89
- try {
90
- const res = await axios . get ( process . env . INDEX_HTML_URL ) ;
91
- cachedIndexHtml = res . data ;
92
- } catch ( err ) {
93
- console . log ( 'Failed to get index.html from cdn.' ) ;
94
- console . error ( err ) ;
95
- process . exit ( 1 ) ;
96
- }
97
- } ;
98
-
99
- updateIndexHtmlCache ( ) ;
100
-
101
81
let database = null ;
102
82
let stringifiedDB = '' ;
103
83
104
-
105
84
const updateDatabaseCache = ( ) => {
106
85
stringifiedDB = JSON . stringify ( database ) ;
107
86
} ;
@@ -171,29 +150,37 @@ if (process.env.AUTO_PROMOTE) {
171
150
// });
172
151
173
152
// heroku free tier goes to sleep after 30 minutes of network inactivity
174
- app . get ( '/api/ ping' , ( req , res ) => {
153
+ app . get ( '/ping' , ( req , res ) => {
175
154
res . sendStatus ( 200 ) ;
176
155
} ) ;
177
156
178
157
setInterval ( ( ) => {
179
- axios . get ( 'https://coderush.xyz/api /ping' ) . catch ( ( err ) => console . error ( `Ping Error: ${ err } ` ) ) ;
158
+ axios . get ( 'https://api. coderush.xyz/ping' ) . catch ( ( err ) => console . error ( `Ping Error: ${ err } ` ) ) ;
180
159
} , 1000 * 60 * 10 ) ;
181
160
} else {
161
+ // DEV
162
+
163
+ app . use ( ( req , res , next ) => {
164
+ res . header ( 'Access-Control-Allow-Origin' , '*' ) ;
165
+ res . header ( 'Access-Control-Allow-Methods' , 'GET, POST, PUT, DELETE, PATCH, OPTIONS' ) ;
166
+ res . header ( 'Access-Control-Allow-Headers' , 'X-Requested-With, content-type, Authorization' ) ;
167
+
168
+ next ( ) ;
169
+ } ) ;
170
+
182
171
app . use ( ( req , res , next ) => {
183
172
if ( ! req . path . includes ( 'code/' ) && ( req . path . slice ( - 2 ) === 'js' || req . path . slice ( - 3 ) === 'css' ) ) {
184
173
res . header ( 'content-encoding' , 'gzip' ) ;
174
+ console . log ( 'gzip' ) ;
185
175
}
176
+ console . log ( 'not gzip' ) ;
186
177
next ( ) ;
187
178
} ) ;
188
179
}
189
180
190
- app . post ( process . env . INDEX_HTML_UPDATE_URL , ( req , res ) => {
191
- if ( updateIndexHtmlCache ( ) ) {
192
- res . sendStatus ( 201 ) ;
193
- } else {
194
- res . sendStatus ( 400 ) ;
195
- }
196
- } ) ;
181
+ app . enable ( 'trust proxy' ) ; // trust heroku and cloudflare
182
+ app . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
183
+ app . use ( bodyParser . json ( ) ) ;
197
184
198
185
app . post ( process . env . DATABASE_UPDATE_URL , ( req , res ) => {
199
186
if ( fetchDatabase ( ) ) {
@@ -204,24 +191,13 @@ if (process.env.AUTO_PROMOTE) {
204
191
}
205
192
} ) ;
206
193
207
- // send cached index.html when possible
208
- app . use ( ( req , res , next ) => {
209
- const match = req . originalUrl . match ( / \. \w + $ / ) ;
210
- const ext = match ? match [ 0 ] [ 0 ] : '' ;
211
- if ( ( req . method === 'GET' || req . method === 'HEAD' ) && ( ext === '' || ext === 'html' ) ) {
212
- res . send ( cachedIndexHtml ) ;
213
- } else {
214
- next ( ) ;
215
- }
216
- } ) ;
217
-
218
- app . get ( '/database.json' , ( req , res ) => {
194
+ app . get ( '/data' , ( req , res ) => {
219
195
res . setHeader ( 'Content-Type' , 'application/json' ) ;
220
196
res . send ( stringifiedDB ) ;
221
197
} ) ;
222
198
223
199
224
- app . post ( '/api/ upload' , ( req , res ) => {
200
+ app . post ( '/upload' , ( req , res ) => {
225
201
if ( typeof req . body . code === 'string' && req . body . code . length > 20 ) {
226
202
axios ( {
227
203
url : 'https://api.github.com/repos/encap/coderush/dispatches' ,
@@ -250,7 +226,7 @@ if (process.env.AUTO_PROMOTE) {
250
226
}
251
227
} ) ;
252
228
253
- app . post ( '/api/ stats' , async ( req , res ) => {
229
+ app . post ( '/stats' , async ( req , res ) => {
254
230
const { main, misc } = req . body ;
255
231
256
232
try {
0 commit comments