Spaces:
Sleeping
Sleeping
shank commited on
Commit ·
781de2c
1
Parent(s): 99b8f64
fix: add error handling to all routes, always return JSON
Browse files
app.py
CHANGED
|
@@ -26,10 +26,18 @@ CORS(app)
|
|
| 26 |
|
| 27 |
# MongoDB setup
|
| 28 |
MONGO_URI = os.getenv("MONGO_URI")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Load ResNet50 model
|
| 35 |
model = models.resnet50()
|
|
@@ -117,47 +125,59 @@ def predict():
|
|
| 117 |
|
| 118 |
@app.route("/register", methods=["POST"])
|
| 119 |
def register():
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
"username": username,
|
| 134 |
-
"password": hashed_pw,
|
| 135 |
-
"action": "register",
|
| 136 |
-
"timestamp": datetime.now(timezone.utc)
|
| 137 |
-
})
|
| 138 |
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
@app.route("/login", methods=["POST"])
|
| 142 |
def login():
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
return jsonify({"error": "Invalid username or password"}), 401
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
"timestamp": datetime.now(timezone.utc)
|
| 158 |
-
})
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
@app.route("/get-reports", methods=["POST"])
|
| 163 |
def get_reports():
|
|
|
|
| 26 |
|
| 27 |
# MongoDB setup
|
| 28 |
MONGO_URI = os.getenv("MONGO_URI")
|
| 29 |
+
try:
|
| 30 |
+
client = MongoClient(MONGO_URI, serverSelectionTimeoutMS=5000)
|
| 31 |
+
client.server_info() # Force connection check
|
| 32 |
+
db = client["screw_inspections"]
|
| 33 |
+
predictions_collection = db["predictions"]
|
| 34 |
+
users_collection = db["users"]
|
| 35 |
+
print("✅ MongoDB connected")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"❌ MongoDB connection failed: {e}")
|
| 38 |
+
client = None
|
| 39 |
+
predictions_collection = None
|
| 40 |
+
users_collection = None
|
| 41 |
|
| 42 |
# Load ResNet50 model
|
| 43 |
model = models.resnet50()
|
|
|
|
| 125 |
|
| 126 |
@app.route("/register", methods=["POST"])
|
| 127 |
def register():
|
| 128 |
+
try:
|
| 129 |
+
data = request.json
|
| 130 |
+
username = data.get("username")
|
| 131 |
+
password = data.get("password")
|
| 132 |
|
| 133 |
+
if not username or not password:
|
| 134 |
+
return jsonify({"error": "Username and password required"}), 400
|
| 135 |
|
| 136 |
+
if users_collection is None:
|
| 137 |
+
return jsonify({"error": "Database not connected"}), 500
|
| 138 |
|
| 139 |
+
if users_collection.find_one({"username": username}):
|
| 140 |
+
return jsonify({"error": "User already exists"}), 400
|
| 141 |
|
| 142 |
+
hashed_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
users_collection.insert_one({
|
| 145 |
+
"username": username,
|
| 146 |
+
"password": hashed_pw,
|
| 147 |
+
"action": "register",
|
| 148 |
+
"timestamp": datetime.now(timezone.utc)
|
| 149 |
+
})
|
| 150 |
+
|
| 151 |
+
return jsonify({"message": "User registered successfully"}), 201
|
| 152 |
+
except Exception as e:
|
| 153 |
+
return jsonify({"error": str(e)}), 500
|
| 154 |
|
| 155 |
@app.route("/login", methods=["POST"])
|
| 156 |
def login():
|
| 157 |
+
try:
|
| 158 |
+
data = request.json
|
| 159 |
+
username = data.get("username")
|
| 160 |
+
password = data.get("password")
|
| 161 |
|
| 162 |
+
if not username or not password:
|
| 163 |
+
return jsonify({"error": "Username and password required"}), 400
|
| 164 |
|
| 165 |
+
if users_collection is None:
|
| 166 |
+
return jsonify({"error": "Database not connected"}), 500
|
|
|
|
| 167 |
|
| 168 |
+
user = users_collection.find_one({"username": username})
|
| 169 |
+
if not user or not bcrypt.checkpw(password.encode('utf-8'), user['password']):
|
| 170 |
+
return jsonify({"error": "Invalid username or password"}), 401
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
users_collection.insert_one({
|
| 173 |
+
"username": username,
|
| 174 |
+
"action": "login",
|
| 175 |
+
"timestamp": datetime.now(timezone.utc)
|
| 176 |
+
})
|
| 177 |
+
|
| 178 |
+
return jsonify({"message": "Login successful", "user_email": username}), 200
|
| 179 |
+
except Exception as e:
|
| 180 |
+
return jsonify({"error": str(e)}), 500
|
| 181 |
|
| 182 |
@app.route("/get-reports", methods=["POST"])
|
| 183 |
def get_reports():
|