创建 asgi.py:
from litefs import Litefs
app = Litefs()
@app.add_get('/', name='index')
def index_handler(request):
return 'Hello, World!'
application = app.asgi()pip install uvicornuvicorn asgi:applicationuvicorn asgi:application \
--host 0.0.0.0 \
--port 8000 \
--workers 4 \
--log-level info \
--access-log \
--error-logfile error.log \
--timeout-keep-alive 5 \
--timeout-graceful-shutdown 30pip install daphnedaphne asgi:applicationdaphne \
--bind 0.0.0.0:8000 \
--workers 4 \
--access-log /var/log/litefs/access.log \
--error-log /var/log/litefs/error.log \
asgi:applicationpip install hypercornhypercorn asgi:applicationhypercorn \
--bind 0.0.0.0:8000 \
--workers 4 \
--access-logfile /var/log/litefs/access.log \
--error-logfile /var/log/litefs/error.log \
--log-level info \
asgi:applicationupstream app {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 支持 WebSocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install uvicorn
COPY . .
EXPOSE 8000
CMD ["uvicorn", "asgi:application", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"][Unit]
Description=Litefs ASGI Application
After=network.target
[Service]
Type=notify
User=www-data
Group=www-data
WorkingDirectory=/path/to/litefs
Environment="PATH=/path/to/venv/bin"
ExecStart=/path/to/venv/bin/uvicorn \
asgi:application \
--host 127.0.0.1 \
--port 8000 \
--workers 4 \
--access-logfile /var/log/litefs/access.log \
--error-logfile /var/log/litefs/error.log \
--log-level info
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetASGI 模式下,你可以使用异步处理函数:
from litefs import Litefs
import asyncio
app = Litefs()
@app.add_get('/async', name='async_example')
async def async_handler(request):
# 模拟异步操作
await asyncio.sleep(1)
return 'Hello from async handler!'
application = app.asgi()- :doc:
getting-started- 快速开始 - :doc:
routing-guide- 路由系统 - :doc:
configuration- 配置管理 - :doc:
middleware-guide- 中间件 - :doc:
wsgi-deployment- WSGI 部署