Skip to content

Commit 086b082

Browse files
authored
Pr backend serve (#329)
* Add deploy to backend * ignore /backend/deploy * Made it so that deploy is different on server and local * Change order of imports to be correct * break out deploy * split storage into its own file * Change to use logger * add python 3 type hints * Fix comment * Change /api/status to /statusz * Fixed mistake of combining lines * Fix problem introduced in merge
1 parent c297f46 commit 086b082

File tree

7 files changed

+431
-225
lines changed

7 files changed

+431
-225
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
# production
1212
/build
1313
/dist
14+
/backend/deploy
1415

1516
# python stuff
1617
__pycache__/
1718
venv/
1819
*.egg-info
1920

21+
2022
# misc
2123
.DS_Store
2224
.env.local

backend/deploy.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Standard library imports
2+
import os
3+
import shutil
4+
import zipfile
5+
from typing import Dict, List, Tuple, Union
6+
7+
# Third-party imports
8+
from flask import request
9+
from flask_restful import Resource
10+
11+
# Our imports
12+
from main import basedir
13+
14+
class DeployResource(Resource):
15+
def post(self) -> Union[Dict[str, Union[str, int, List[str]]], Tuple[Dict[str, str], int]]:
16+
"""Upload and extract a zip file to the deploy directory"""
17+
if 'file' not in request.files:
18+
return {'error': 'No file provided'}, 400
19+
20+
file = request.files['file']
21+
22+
if file.filename == '':
23+
return {'error': 'No file selected'}, 400
24+
25+
if not file.filename.endswith('.zip'):
26+
return {'error': 'Only zip files are allowed'}, 400
27+
28+
try:
29+
# Create deploy directory if it doesn't exist
30+
deploy_dir = os.path.join(basedir, 'deploy')
31+
32+
# Clear existing deploy directory
33+
if os.path.exists(deploy_dir):
34+
shutil.rmtree(deploy_dir)
35+
os.makedirs(deploy_dir)
36+
37+
# Save the zip file temporarily
38+
temp_zip_path = os.path.join(basedir, 'temp_deploy.zip')
39+
file.save(temp_zip_path)
40+
41+
# Extract the zip file
42+
with zipfile.ZipFile(temp_zip_path, 'r') as zip_ref:
43+
zip_ref.extractall(deploy_dir)
44+
45+
# Remove the temporary zip file
46+
os.remove(temp_zip_path)
47+
48+
# List extracted files
49+
extracted_files = []
50+
for root, dirs, files in os.walk(deploy_dir):
51+
for filename in files:
52+
rel_path = os.path.relpath(os.path.join(root, filename), deploy_dir)
53+
extracted_files.append(rel_path)
54+
55+
return {
56+
'message': 'Deployment successful',
57+
'deploy_directory': deploy_dir,
58+
'files_extracted': len(extracted_files),
59+
'files': extracted_files[:20] # Show first 20 files
60+
}
61+
except zipfile.BadZipFile:
62+
return {'error': 'Invalid zip file'}, 400
63+
except Exception as e:
64+
return {'error': f'Deployment failed: {str(e)}'}, 500

0 commit comments

Comments
 (0)