2023-06-16 13:17:41 -07:00
|
|
|
import argparse
|
2023-06-07 15:40:02 -07:00
|
|
|
import asyncio
|
|
|
|
import sys
|
2023-06-16 13:17:41 -07:00
|
|
|
from pathlib import Path
|
2023-06-07 15:40:02 -07:00
|
|
|
from types import FrameType
|
|
|
|
|
|
|
|
import uvicorn
|
|
|
|
import uvloop
|
2023-06-16 13:17:41 -07:00
|
|
|
from dotenv import load_dotenv
|
2023-06-07 15:40:02 -07:00
|
|
|
|
|
|
|
from crowdtls.logs import logger
|
2023-06-16 13:17:41 -07:00
|
|
|
|
|
|
|
app_fastapi = None
|
|
|
|
app_rocketry = None
|
2023-06-07 15:40:02 -07:00
|
|
|
|
|
|
|
|
|
|
|
class CrowdTLS(uvicorn.Server):
|
|
|
|
def handle_exit(self, sig: int, frame: FrameType) -> None:
|
|
|
|
logger.info("Shutting down CrowdTLS")
|
|
|
|
return super().handle_exit(sig, frame)
|
|
|
|
|
|
|
|
|
2023-06-16 13:17:41 -07:00
|
|
|
async def start_server() -> None:
|
2023-06-07 15:40:02 -07:00
|
|
|
logger.info("Starting CrowdTLS")
|
|
|
|
server = CrowdTLS(config=uvicorn.Config(app=app_fastapi, workers=1, loop="uvloop"))
|
|
|
|
|
|
|
|
fastapi = asyncio.create_task(server.serve())
|
|
|
|
rocket = asyncio.create_task(app_rocketry.serve())
|
|
|
|
|
|
|
|
await asyncio.wait([rocket, fastapi], return_when=asyncio.FIRST_COMPLETED)
|
|
|
|
|
|
|
|
|
2023-06-16 13:17:41 -07:00
|
|
|
def run(env: Path) -> None:
|
|
|
|
global app_rocketry
|
|
|
|
global app_fastapi
|
|
|
|
|
|
|
|
if env.exists():
|
|
|
|
try:
|
|
|
|
load_dotenv(env)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Could not load env file {env}: {e}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
from crowdtls.scheduler import app as app_rocketry
|
|
|
|
from crowdtls.webserver import app as app_fastapi
|
|
|
|
|
2023-06-07 15:40:02 -07:00
|
|
|
if sys.version_info >= (3, 11):
|
|
|
|
with asyncio.Runner(loop_factory=uvloop.new_event_loop) as runner:
|
|
|
|
runner.run(start_server())
|
|
|
|
else:
|
|
|
|
uvloop.install()
|
|
|
|
asyncio.run(start_server())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-06-16 13:17:41 -07:00
|
|
|
parser = argparse.ArgumentParser(description="CrowdTLS Server")
|
|
|
|
parser.add_argument(
|
|
|
|
"--env",
|
|
|
|
type=lambda p: Path(p).absolute(),
|
|
|
|
default=Path(__file__).absolute().parent / ".env",
|
|
|
|
required=False,
|
|
|
|
help="Path to specific env file",
|
|
|
|
)
|
|
|
|
print(Path(__file__).absolute().parent / ".env")
|
|
|
|
args = parser.parse_args()
|
|
|
|
run(args.env)
|