-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPWA_server.py
53 lines (45 loc) · 1.69 KB
/
PWA_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from flask_compress import Compress
app = Flask(__name__, static_folder="static")
CORS(app)
Compress(app)
# Mock user data
users = {
"user": {"password": "user", "role": "user"},
"admin": {"password": "admin", "role": "admin"}
}
@app.route('/')
def index():
return "Hello, World!"
def authenticate(username, password):
user = users.get(username)
if user and user["password"] == password:
return {"username": username, "role": user["role"]}
return None
@app.route("/login", methods=["POST"])
def login():
data = request.json
user = authenticate(data["username"], data["password"])
if user:
return jsonify({"message": "Login successful", "role": user["role"]}), 200
return jsonify({"message": "Invalid credentials"}), 401
@app.route("/classify", methods=["POST"])
def classify():
auth_header = request.headers.get("Authorization")
if not auth_header:
return jsonify({"message": "Unauthorized"}), 401
# Check role from authorization token (simplified role in header for demonstration)
token_role = auth_header.split(" ")[-1]
if token_role == "user":
return jsonify({"message": "User role: Access granted to basic classification"}), 200
elif token_role == "admin":
return jsonify({"message": "Admin role: Access granted with additional controls"}), 200
else:
return jsonify({"message": "Unauthorized role"}), 403
# Serve static files
@app.route('/<path:filename>')
def static_files(filename):
return send_from_directory(app.static_folder, filename)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5001)