Add webUI serving to api-server

This commit is contained in:
Matthias 2021-01-10 10:31:05 +01:00
parent 79087ba166
commit 27970b424d
6 changed files with 35 additions and 1 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ user_data/*
user_data/notebooks/* user_data/notebooks/*
freqtrade-plot.html freqtrade-plot.html
freqtrade-profit-plot.html freqtrade-profit-plot.html
freqtrade/rpc/api_server/ui/*
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/

View File

View File

@ -0,0 +1,28 @@
from pathlib import Path
from fastapi import APIRouter
from fastapi.exceptions import HTTPException
from starlette.responses import FileResponse
router_ui = APIRouter()
@router_ui.get('/favicon.ico', include_in_schema=False)
async def favicon():
return FileResponse(Path(__file__).parent / 'ui/favicon.ico')
@router_ui.get('/{rest_of_path:path}', include_in_schema=False)
async def index_html(rest_of_path: str):
"""
Emulate path fallback to index.html.
"""
if rest_of_path.startswith('api') or rest_of_path.startswith('.'):
raise HTTPException(status_code=404, detail="Not Found")
uibase = Path(__file__).parent / 'ui'
if (uibase / rest_of_path).is_file():
return FileResponse(uibase / rest_of_path)
# Fall back to index.html, as indicated by vue router docs
return FileResponse(uibase / 'index.html')

View File

@ -57,12 +57,16 @@ class ApiServer(RPCHandler):
from freqtrade.rpc.api_server.api_auth import http_basic_or_jwt_token, router_login from freqtrade.rpc.api_server.api_auth import http_basic_or_jwt_token, router_login
from freqtrade.rpc.api_server.api_v1 import router as api_v1 from freqtrade.rpc.api_server.api_v1 import router as api_v1
from freqtrade.rpc.api_server.api_v1 import router_public as api_v1_public from freqtrade.rpc.api_server.api_v1 import router_public as api_v1_public
from freqtrade.rpc.api_server.web_ui import router_ui
app.include_router(api_v1_public, prefix="/api/v1") app.include_router(api_v1_public, prefix="/api/v1")
app.include_router(api_v1, prefix="/api/v1", app.include_router(api_v1, prefix="/api/v1",
dependencies=[Depends(http_basic_or_jwt_token)], dependencies=[Depends(http_basic_or_jwt_token)],
) )
app.include_router(router_login, prefix="/api/v1", tags=["auth"]) app.include_router(router_login, prefix="/api/v1", tags=["auth"])
# UI Router MUST be last!
app.include_router(router_ui, prefix='')
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,

View File

@ -31,6 +31,7 @@ sdnotify==0.3.2
fastapi==0.63.0 fastapi==0.63.0
uvicorn==0.13.3 uvicorn==0.13.3
pyjwt==2.0.1 pyjwt==2.0.1
aiofiles==0.6.0
# Support for colorized terminal output # Support for colorized terminal output
colorama==0.4.4 colorama==0.4.4

View File

@ -83,7 +83,7 @@ def assert_response(response, expected_code=200, needs_cors=True):
def test_api_not_found(botclient): def test_api_not_found(botclient):
ftbot, client = botclient ftbot, client = botclient
rc = client_post(client, f"{BASE_URI}/invalid_url") rc = client_get(client, f"{BASE_URI}/invalid_url")
assert_response(rc, 404) assert_response(rc, 404)
assert rc.json() == {"detail": "Not Found"} assert rc.json() == {"detail": "Not Found"}