Skip to content

Commit b557fb9

Browse files
committed
Made it so that deploy is different on server and local
1 parent e152a0d commit b557fb9

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

backend/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,16 @@ def post(self):
289289
api.add_resource(StorageResource, '/storage/<path:path>')
290290
api.add_resource(DeployResource, '/deploy')
291291

292+
# API health check endpoint to distinguish from static file serving
293+
@app.route('/api/status')
294+
def api_status():
295+
"""Health check endpoint to identify backend server"""
296+
return jsonify({
297+
'status': 'ok',
298+
'server': 'python-backend',
299+
'version': '1.0'
300+
})
301+
292302
# Handle the base path for the frontend
293303
@app.route('/blocks/', defaults={'path': ''})
294304
@app.route('/blocks/<path:path>')

src/reactComponents/Menu.tsx

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as commonStorage from '../storage/common_storage';
2525
import * as storageNames from '../storage/names';
2626
import * as storageProject from '../storage/project';
2727
import * as createPythonFiles from '../storage/create_python_files';
28+
import * as serverSideStorage from '../storage/server_side_storage';
2829
import * as I18Next from 'react-i18next';
2930
import {TabType } from '../types/TabType';
3031

@@ -294,17 +295,45 @@ export function Component(props: MenuProps): React.JSX.Element {
294295

295296
try {
296297
const blobUrl = await createPythonFiles.producePythonProjectBlob(props.project, props.storage);
297-
298-
// Create a temporary link to download the file
299-
const link = document.createElement('a');
300-
link.href = blobUrl;
301-
link.download = `${props.project.projectName}.zip`;
302-
document.body.appendChild(link);
303-
link.click();
304-
document.body.removeChild(link);
305298

306-
// Clean up the blob URL
307-
URL.revokeObjectURL(blobUrl);
299+
// Check if the backend server is available
300+
const serverAvailable = await serverSideStorage.isServerAvailable();
301+
console.log('Server available:', serverAvailable);
302+
303+
if (serverAvailable) {
304+
// Send the file to the backend /deploy endpoint
305+
const response = await fetch(blobUrl);
306+
const blob = await response.blob();
307+
308+
const formData = new FormData();
309+
formData.append('file', blob, `${props.project.projectName}.zip`);
310+
311+
const deployResponse = await fetch('/deploy', {
312+
method: 'POST',
313+
body: formData,
314+
});
315+
316+
if (!deployResponse.ok) {
317+
throw new Error('Deploy to server failed');
318+
}
319+
320+
const result = await deployResponse.json();
321+
console.log('Deployment successful:', result);
322+
323+
// Clean up the blob URL
324+
URL.revokeObjectURL(blobUrl);
325+
} else {
326+
// Download the file locally
327+
const link = document.createElement('a');
328+
link.href = blobUrl;
329+
link.download = `${props.project.projectName}.zip`;
330+
document.body.appendChild(link);
331+
link.click();
332+
document.body.removeChild(link);
333+
334+
// Clean up the blob URL
335+
URL.revokeObjectURL(blobUrl);
336+
}
308337
} catch (error) {
309338
console.error('Failed to deploy project:', error);
310339
props.setAlertErrorMessage(t('DEPLOY_FAILED') || 'Failed to deploy project');

src/storage/server_side_storage.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import * as commonStorage from './common_storage';
2424

25-
const API_BASE_URL = 'http://localhost:5001';
25+
const API_BASE_URL = '';
2626

2727
export async function isServerAvailable(): Promise<boolean> {
2828
try {
@@ -31,13 +31,20 @@ export async function isServerAvailable(): Promise<boolean> {
3131
setTimeout(() => reject(new Error('Timeout')), 5000); // 5 second timeout
3232
});
3333

34-
// Race between the fetch and timeout
34+
// Check the specific API status endpoint to distinguish backend from static file server
35+
// Use absolute path without base URL since /api/status is a backend endpoint
3536
const response = await Promise.race([
36-
fetch(`${API_BASE_URL}/`),
37+
fetch(`${API_BASE_URL}/api/status`),
3738
timeoutPromise
3839
]);
3940

40-
return response.ok;
41+
if (!response.ok) {
42+
return false;
43+
}
44+
45+
// Verify it's actually the Python backend by checking the response
46+
const data = await response.json();
47+
return data.server === 'python-backend';
4148
} catch (error) {
4249
// Network error, server not available, or timeout
4350
return false;

0 commit comments

Comments
 (0)