forked from hummingbot/hummingbot
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1766c3c
commit 7521f09
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from json import JSONDecodeError | ||
|
||
import asyncio | ||
import atexit | ||
import logging | ||
import nest_asyncio | ||
import os | ||
import signal | ||
import threading | ||
import uvicorn | ||
from dotmap import DotMap | ||
from fastapi import FastAPI | ||
from pathlib import Path | ||
from starlette.requests import Request | ||
from typing import Any, Dict | ||
from hummingbot.client.hummingbot_application import HummingbotApplication | ||
|
||
nest_asyncio.apply() | ||
root_path = Path(os.path.dirname(__file__)).absolute().as_posix() | ||
debug = True | ||
app = FastAPI(debug=debug, root_path=root_path) | ||
|
||
|
||
@app.post("/run") | ||
async def run(request: Request) -> Dict[str, Any]: | ||
try: | ||
body = DotMap(await request.json()) | ||
except JSONDecodeError: | ||
body = DotMap({}) | ||
|
||
body._dynamic = False | ||
|
||
HummingbotApplication.main_application() | ||
|
||
return body | ||
|
||
|
||
async def start_api(): | ||
signal.signal(signal.SIGTERM, shutdown) | ||
signal.signal(signal.SIGINT, shutdown) | ||
|
||
host = os.environ.get("HOST", "localhost") | ||
port = int(os.environ.get("PORT", "30003")) | ||
|
||
os.environ["ENV"] = "development" | ||
|
||
config = uvicorn.Config( | ||
"app:app", | ||
host=host, | ||
port=port, | ||
log_level=logging.DEBUG, | ||
) | ||
server = uvicorn.Server(config) | ||
await server.serve() | ||
|
||
|
||
async def main(): | ||
await start_api() | ||
|
||
|
||
def after_startup(): | ||
pass | ||
|
||
|
||
async def startup(): | ||
threading.Timer(1, after_startup).start() | ||
|
||
|
||
# noinspection PyUnusedLocal | ||
def shutdown(*args): | ||
pass | ||
|
||
|
||
@atexit.register | ||
def shutdown_helper(): | ||
shutdown() | ||
asyncio.get_event_loop().close() | ||
|
||
|
||
app.add_event_handler("startup", startup) | ||
app.add_event_handler("shutdown", shutdown) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) | ||
finally: | ||
shutdown_helper() |