forked from aegis/pyserveX
asgi/wsgi mounting implemented
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
"""
|
||||
Example Flask application for PyServe ASGI mounting.
|
||||
|
||||
This demonstrates how to create a Flask application that can be
|
||||
mounted at a specific path in PyServe (via WSGI-to-ASGI adapter).
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
try:
|
||||
from flask import Flask, jsonify, request
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"Flask is not installed. Install with: pip install flask"
|
||||
)
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
users_db = {
|
||||
1: {"id": 1, "name": "Alice", "email": "alice@example.com"},
|
||||
2: {"id": 2, "name": "Bob", "email": "bob@example.com"},
|
||||
}
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def root():
|
||||
return jsonify({"message": "Welcome to Flask mounted in PyServe!"})
|
||||
|
||||
|
||||
@app.route("/health")
|
||||
def health_check():
|
||||
return jsonify({"status": "healthy", "app": "flask"})
|
||||
|
||||
|
||||
@app.route("/users")
|
||||
def list_users():
|
||||
return jsonify({"users": list(users_db.values()), "count": len(users_db)})
|
||||
|
||||
|
||||
@app.route("/users/<int:user_id>")
|
||||
def get_user(user_id: int):
|
||||
if user_id not in users_db:
|
||||
return jsonify({"error": "User not found"}), 404
|
||||
return jsonify(users_db[user_id])
|
||||
|
||||
|
||||
@app.route("/users", methods=["POST"])
|
||||
def create_user():
|
||||
data = request.get_json()
|
||||
if not data or "name" not in data:
|
||||
return jsonify({"error": "Name is required"}), 400
|
||||
|
||||
new_id = max(users_db.keys()) + 1 if users_db else 1
|
||||
users_db[new_id] = {
|
||||
"id": new_id,
|
||||
"name": data["name"],
|
||||
"email": data.get("email", ""),
|
||||
}
|
||||
return jsonify({"message": f"User created with ID {new_id}", "user": users_db[new_id]}), 201
|
||||
|
||||
|
||||
@app.route("/users/<int:user_id>", methods=["PUT"])
|
||||
def update_user(user_id: int):
|
||||
if user_id not in users_db:
|
||||
return jsonify({"error": "User not found"}), 404
|
||||
|
||||
data = request.get_json()
|
||||
if data:
|
||||
if "name" in data:
|
||||
users_db[user_id]["name"] = data["name"]
|
||||
if "email" in data:
|
||||
users_db[user_id]["email"] = data["email"]
|
||||
|
||||
return jsonify({"message": f"User {user_id} updated", "user": users_db[user_id]})
|
||||
|
||||
|
||||
@app.route("/users/<int:user_id>", methods=["DELETE"])
|
||||
def delete_user(user_id: int):
|
||||
if user_id not in users_db:
|
||||
return jsonify({"error": "User not found"}), 404
|
||||
|
||||
del users_db[user_id]
|
||||
return jsonify({"message": f"User {user_id} deleted"})
|
||||
|
||||
|
||||
def create_app(config: Optional[dict] = None) -> Flask:
|
||||
application = Flask(__name__)
|
||||
|
||||
if config:
|
||||
application.config.update(config)
|
||||
|
||||
@application.route("/")
|
||||
def factory_root():
|
||||
return jsonify({
|
||||
"message": "Welcome to Flask (factory) mounted in PyServe!",
|
||||
"config": config or {},
|
||||
})
|
||||
|
||||
@application.route("/health")
|
||||
def factory_health():
|
||||
return jsonify({"status": "healthy", "app": "flask-factory"})
|
||||
|
||||
@application.route("/echo/<message>")
|
||||
def echo(message: str):
|
||||
return jsonify({"echo": message})
|
||||
|
||||
return application
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=8002, debug=True)
|
||||
Reference in New Issue
Block a user