WSGI Frameworks¶
Sabia que graças ao incrível trabalho do a2wsgi pode integrar qualquer framework WSGI (Flask, Django...)?
Sim, é verdade, agora pode facilmente migrar para Lilya sem ter que reescrever as suas aplicações antigas, na realidade, pode reutilizá-las diretamente dentro de Lilya, até mesmo um Lilya que esteja a correr dentro de outro Lilya, um Lilyaception.
WSGIMiddleware¶
Utilizar este middleware é bastante simples, vamos usar o Flask como exemplo, já que é muito rápido iniciar um serviço Flask em comparação com outros gigantes como Django.
from flask import Flask, request
from lilya.apps import Lilya
from lilya.middleware.wsgi import WSGIMiddleware
from lilya.requests import Request
from lilya.routing import Include, Path
flask_app = Flask(__name__)
@flask_app.route("/")
def flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from your Flask integrated!"
async def home(request: Request):
name = request.path_params["name"]
return {"name": name}
app = Lilya(
routes=[
Path("/home/{name:str}", handler=home),
Include("/flask", WSGIMiddleware(flask_app)),
]
)
from flask import Flask, request
from lilya.apps import Lilya
from lilya.middleware.wsgi import WSGIMiddleware
from lilya.requests import Request
from lilya.routing import Include, Path
flask_app = Flask(__name__)
@flask_app.route("/")
def flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from your Flask integrated!"
async def home(request: Request):
name = request.path_params["name"]
return {"name": name}
app = Lilya(
routes=[
Path("/home/{name:str}", handler=home),
Include(
"/",
routes=[
Include("/flask", WSGIMiddleware(flask_app)),
],
),
]
)
from flask import Flask, request
from lilya.apps import Lilya
from lilya.middleware.wsgi import WSGIMiddleware
from lilya.requests import Request
from lilya.routing import Include, Path
flask_app = Flask(__name__)
second_flask_app = Flask(__name__)
@flask_app.route("/")
def flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from your Flask integrated!"
@second_flask_app.route("/")
def flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from your Flask integrated!"
async def home(request: Request):
name = request.path_params["name"]
return {"name": name}
routes = [
Path("/home/{name:str}", handler=home),
Include(
"/",
routes=[
Include(
"/internal",
routes=[
Include(
routes=[
Include("/flask", WSGIMiddleware(flask_app)),
]
)
],
),
Include(
"/",
routes=[
Include(
"/",
routes=[
Include(
"/external",
routes=[
Include(
"/second/flask",
WSGIMiddleware(second_flask_app),
),
],
)
],
)
],
),
],
),
]
app = Lilya(routes=routes)
from flask import Flask, request
from lilya.apps import Lilya
from lilya.middleware.wsgi import WSGIMiddleware
from lilya.requests import Request
from lilya.routing import Include, Path
flask_app = Flask(__name__)
second_flask_app = Flask(__name__)
@flask_app.route("/")
def flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from your Flask integrated!"
async def home(request: Request):
name = request.path_params["name"]
return {"name": name}
@second_flask_app.route("/")
def second_flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from Flask!"
routes = [
Include(
path="/",
routes=[
Path("/home/{name:str}", handler=home),
Include("/flask", WSGIMiddleware(flask_app)),
Include("/second/flask", WSGIMiddleware(second_flask_app)),
],
)
]
app = Lilya(routes=routes)
from flask import Flask, request
from lilya.apps import Lilya
from lilya.middleware.wsgi import WSGIMiddleware
from lilya.requests import Request
from lilya.routing import Include, Path
flask_app = Flask(__name__)
second_flask_app = Flask(__name__)
@flask_app.route("/")
def flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from your Flask integrated!"
async def home(request: Request):
name = request.path_params["name"]
return {"name": name}
@second_flask_app.route("/")
def second_flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from Flask!"
sub_lilya = Lilya(
routes=[
Path("/home/{name:str}", handler=home),
Include("/flask", WSGIMiddleware(flask_app)),
Include("/second/flask", WSGIMiddleware(second_flask_app)),
]
)
routes = [Include("/sub-lilya", app=sub_lilya)]
app = Lilya(routes=routes)
from flask import Flask, request
from lilya.apps import ChildLilya, Lilya
from lilya.middleware.wsgi import WSGIMiddleware
from lilya.requests import Request
from lilya.routing import Include, Path
flask_app = Flask(__name__)
second_flask_app = Flask(__name__)
@flask_app.route("/")
def flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from your Flask integrated!"
async def home(request: Request):
name = request.path_params["name"]
return {"name": name}
@second_flask_app.route("/")
def second_flask_main():
name = request.args.get("name", "Lilya")
return f"Hello, {name} from Flask!"
child_lilya = ChildLilya(
routes=[
Path("/home/{name:str}", handler=home),
Include("/flask", WSGIMiddleware(flask_app)),
Include("/second/flask", WSGIMiddleware(second_flask_app)),
]
)
routes = [Include("/child-lilya", app=child_lilya)]
app = Lilya(routes=routes)
Já tem uma ideia, as integrações são infinitas!
Verifique¶
Com todos os exemplos anteriores, já pode verificar que as integrações estão a funcionar.
Os caminhos que se referem ao WSGIMiddleware
serão tratados pelo Flask e o resto será tratado pelo Lilya, incluindo Lilya dentro de outro Lilya.
Se executar o endpoint tratado pelo Flask:
/flask
- No roteamento simples./flask
- No roteamento aninhado./internal/flask
e/external/second/flask
- No roteamento complexo./flask
e/second/flask
- A partir de várias aplicações Flask./lilya/flask
e/lilya/second/flask
- A partir de dentro de outro Lilya.
Verá a resposta:
Hello, Lilya from Flask!
Ao aceder a qualquer endpoint do Lilya
:
/home/lilya
- No roteamento simples./home/lilya
- No roteamento complexo./home/lilya
- No roteamento aninhado./home/lilya
- A partir de várias aplicações Flask./lilya/home/lilya
- A partir de dentro de outro Lilya.
{
"name": "lilya"
}