Add basic router & Using hypercorn instead of uvicorn & Fix accepting proxy headers from reverse proxy
This commit is contained in:
@ -1,17 +1,15 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from fastapi import FastAPI, Depends
|
import asyncio
|
||||||
from fastapi import Request
|
from fastapi import FastAPI
|
||||||
from starlette.staticfiles import StaticFiles
|
from starlette.staticfiles import StaticFiles
|
||||||
from fastapi.templating import Jinja2Templates
|
|
||||||
from fastapi.responses import PlainTextResponse
|
|
||||||
|
|
||||||
|
|
||||||
from config import CONFIGS # Loads the configuration from .env
|
from config import CONFIGS # Loads the configuration from .env
|
||||||
from middleware import AuthMiddleware # Defines authentication middleware
|
from middleware import AuthMiddleware # Defines authentication middleware
|
||||||
from middleware import AfterRequestMiddleware # Defines after request middleware
|
from middleware import AfterRequestMiddleware # Defines after request middleware
|
||||||
from dependency import get_templates, get_session_manager # Defines dependencies across routers
|
from dependency import get_session_manager # Defines dependencies across routers
|
||||||
from exception_handler import setup_exception_handler # Defines exception handlers
|
from exception_handler import setup_exception_handler # Defines exception handlers
|
||||||
|
|
||||||
# Append directory of cli_api.py to be able to import it
|
# Append directory of cli_api.py to be able to import it
|
||||||
@ -31,7 +29,7 @@ def create_app() -> FastAPI:
|
|||||||
title='Hysteria Webpanel',
|
title='Hysteria Webpanel',
|
||||||
description='Webpanel for Hysteria',
|
description='Webpanel for Hysteria',
|
||||||
version='0.1.0',
|
version='0.1.0',
|
||||||
debug=CONFIGS.DEBUG
|
debug=CONFIGS.DEBUG,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Set up static files
|
# Set up static files
|
||||||
@ -46,6 +44,7 @@ def create_app() -> FastAPI:
|
|||||||
app.add_middleware(AfterRequestMiddleware)
|
app.add_middleware(AfterRequestMiddleware)
|
||||||
|
|
||||||
# Set up Routers
|
# Set up Routers
|
||||||
|
app.include_router(routers.basic.router, prefix='', tags=['basic']) # Add basic router
|
||||||
app.include_router(routers.login.router, prefix='', tags=['authentication']) # Add authentication router
|
app.include_router(routers.login.router, prefix='', tags=['authentication']) # Add authentication router
|
||||||
app.include_router(routers.user.router, prefix='/users', tags=['users']) # Add user router
|
app.include_router(routers.user.router, prefix='/users', tags=['users']) # Add user router
|
||||||
app.include_router(routers.api.v1.api_v1_router, prefix='/api/v1', tags=['v1']) # Add API version 1 router
|
app.include_router(routers.api.v1.api_v1_router, prefix='/api/v1', tags=['v1']) # Add API version 1 router
|
||||||
@ -56,20 +55,17 @@ def create_app() -> FastAPI:
|
|||||||
app: FastAPI = create_app()
|
app: FastAPI = create_app()
|
||||||
|
|
||||||
|
|
||||||
@app.get('/')
|
|
||||||
async def index(request: Request, templates: Jinja2Templates = Depends(get_templates)):
|
|
||||||
return templates.TemplateResponse('index.html', {'request': request})
|
|
||||||
|
|
||||||
|
|
||||||
@app.get('/home')
|
|
||||||
async def home(request: Request):
|
|
||||||
return await index(request)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get('/robots.txt')
|
|
||||||
async def robots_txt(request: Request):
|
|
||||||
return PlainTextResponse('User-agent: *\nDisallow: /')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import uvicorn
|
from hypercorn.config import Config
|
||||||
uvicorn.run(app, host='127.0.0.1', port=8080)
|
from hypercorn.asyncio import serve # type: ignore
|
||||||
|
from hypercorn.middleware import ProxyFixMiddleware
|
||||||
|
|
||||||
|
config = Config()
|
||||||
|
config.debug = CONFIGS.DEBUG
|
||||||
|
config.bind = ['127.0.0.1:8080']
|
||||||
|
config.accesslog = '-'
|
||||||
|
config.errorlog = '-'
|
||||||
|
|
||||||
|
# Fix proxy headers
|
||||||
|
app = ProxyFixMiddleware(app, 'legacy') # type: ignore
|
||||||
|
asyncio.run(serve(app, config)) # type: ignore
|
||||||
|
|||||||
Reference in New Issue
Block a user