Skip to content

Commit a221385

Browse files
committed
fixing
1 parent aff2c3e commit a221385

File tree

13 files changed

+140
-346
lines changed

13 files changed

+140
-346
lines changed

server/api.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@
66
from sanic.request import Request
77
from sanic_jwt import Initialize
88
from sanic_cors import CORS
9-
from dotenv import load_dotenv
109
import socketio
11-
from controllers.assisstant import RunAssistant
12-
from controllers.features import GetFeatures
10+
from config.app_config import AppConfig
11+
from controllers.assisstant import run_assistant
1312
from controllers.login import login_user, validate_user
14-
from db.database import init_db
13+
from database.database import init_db
1514

16-
load_dotenv()
1715

18-
app = Sanic(__name__)
19-
20-
# Load configuration
21-
app.config.SECRET = os.getenv("SOCKET_SECRET")
22-
app.config.JWT_SECRET = os.getenv("JWT_SECRET")
16+
app = Sanic("Atlas", config=AppConfig())
2317

2418
# Initialize CORS
2519
CORS(app, resources={r"/*": {"origins": "*"}})
@@ -31,7 +25,7 @@
3125

3226
# Initialize database
3327
@app.before_server_start
34-
async def attach_db(app, loop):
28+
async def attach_db(_app, _loop):
3529
"""
3630
Initialize the database connection.
3731
"""
@@ -67,7 +61,7 @@ async def login(request: Request):
6761
if request.method == "POST":
6862
data = request.json
6963
email = data.get("email")
70-
return await login_user(email)
64+
return await login_user(email=email)
7165

7266

7367
@app.route("/api/validate", methods=["POST"])
@@ -82,6 +76,17 @@ async def validate(request: Request):
8276
return await validate_user(email=email, token=token)
8377

8478

79+
@app.route("/api/run_assistant", methods=["POST"])
80+
async def run_assistant_endpoint(request: Request):
81+
"""
82+
Handles the POST request for running the assistant.
83+
"""
84+
if request.method == "POST":
85+
file = request.form.get("file")
86+
sid = request.form.get("sid")
87+
return await run_assistant(sid=sid, file=file, sio=sio)
88+
89+
8590
@sio.on("connect", namespace="/home")
8691
async def handle_connect(sid, _environ, _auth):
8792
"""

server/config/app_config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Configuration class for the application.
3+
"""
4+
5+
import os
6+
from sanic.config import Config
7+
from dotenv import load_dotenv
8+
9+
load_dotenv()
10+
11+
12+
class AppConfig(Config):
13+
"""
14+
Configuration class for the application.
15+
16+
Attributes:
17+
BASE_URL (str): The base URL of the application.
18+
SECRET (str): The secret key for the application's socket.
19+
JWT_SECRET (str): The secret key for JSON Web Tokens (JWT).
20+
JWT_ALGORITHM (str): The algorithm used for JWT encryption.
21+
JWT_EXP_DELTA_SECONDS (int): The expiration time for JWT tokens in seconds.
22+
"""
23+
24+
BASE_URL = os.getenv("BASE_URL")
25+
SECRET = os.getenv("SOCKET_SECRET")
26+
JWT_SECRET = os.getenv("JWT_SECRET")
27+
JWT_ALGORITHM = "HS256"
28+
JWT_EXP_DELTA_SECONDS = 3600

server/controllers/assisstant.py

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
"""
44

55
import os
6-
import json
7-
from flask import jsonify, make_response, request
8-
from flask_restful import Resource
6+
from sanic import json as json_response
7+
import socketio
98

109

1110
from assistant import AssistantException, call_asssistant_api
@@ -17,68 +16,41 @@
1716
os.makedirs(UPLOAD_DIRECTORY)
1817

1918

20-
class RunAssistant(Resource):
19+
def run_assistant(sid, file, sio: socketio.AsyncServer):
2120
"""
22-
Represents a resource for handling file uploads.
23-
Methods:
24-
- post: Handles the POST request for file uploads.
21+
Run the assistant with the uploaded file
2522
"""
26-
27-
def __init__(self, **kwargs):
28-
self.socketio = kwargs.get("socketio")
29-
30-
def post(self):
31-
"""
32-
Handles the POST request for file uploads.
33-
Returns:
34-
- JSON response containing the status of the upload:
35-
- If successful, returns {"message": "File successfully uploaded", "path": file_path}.
36-
- If there is an error, returns {"error": "No file part"} or {"error": "No selected file"}.
37-
"""
38-
sid = request.form.get("sid")
39-
40-
if "file" not in request.files:
41-
return jsonify({"error": "No file part"})
42-
file = request.files["file"]
43-
if file.filename == "":
44-
return jsonify({"error": "No selected file"})
45-
if file:
46-
file_path = os.path.join(UPLOAD_DIRECTORY, file.filename)
47-
file.save(file_path)
48-
try:
49-
result = call_asssistant_api(file_path, sid, self.socketio)
50-
51-
# print("result : ", json.dumps(result))
52-
self.socketio.emit(
53-
"status",
54-
{"status": "Fetching all features...", "progress": 0},
55-
to=sid,
56-
)
57-
58-
response_data = {
59-
"message": "File successfully uploaded",
60-
"file_name": file.filename,
61-
"experiments": result["experiments"],
62-
}
63-
64-
# Delete the uploaded
65-
if os.path.isfile(file_path):
66-
os.remove(file_path)
67-
print("File removed from local storage successfully")
68-
else:
69-
# If it fails, inform the user.
70-
print(f"Error: {file_path} file not found")
71-
72-
response = make_response(jsonify(response_data))
73-
response.status_code = 200
74-
return response
75-
except AssistantException as e:
76-
response_data = {"error": str(e)}
77-
response = make_response(jsonify(response_data))
78-
response.status_code = 500
79-
return response
80-
else:
81-
response_data = {"error": "File upload failed"}
82-
response = make_response(jsonify(response_data))
83-
response.status_code = 400
84-
return response
23+
if file:
24+
file_path = os.path.join(UPLOAD_DIRECTORY, file.filename)
25+
file.save(file_path)
26+
try:
27+
result = call_asssistant_api(file_path, sid, sio)
28+
29+
# print("result : ", json.dumps(result))
30+
sio.emit(
31+
"status",
32+
{"status": "Fetching all features...", "progress": 0},
33+
to=sid,
34+
)
35+
36+
response_data = {
37+
"message": "File successfully uploaded",
38+
"file_name": file.filename,
39+
"experiments": result["experiments"],
40+
}
41+
42+
# Delete the uploaded
43+
if os.path.isfile(file_path):
44+
os.remove(file_path)
45+
print("File removed from local storage successfully")
46+
else:
47+
# If it fails, inform the user.
48+
print(f"Error: {file_path} file not found")
49+
50+
return json_response(body=response_data, status=200)
51+
except AssistantException as e:
52+
response_data = {"error": str(e)}
53+
return json_response(body=response_data, status=500)
54+
else:
55+
response_data = {"error": "File upload failed"}
56+
return json_response(body=response_data, status=400)

server/controllers/features.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)