2021-12-21 21:00:58 +01:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
import uvicorn
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
from src.routes.projects import router as project_router
|
|
|
|
from src.routes.tags import router as tag_router
|
|
|
|
from src.routes.records import router as record_router
|
|
|
|
|
|
|
|
|
|
|
|
description = """
|
|
|
|
Welcome to the Juggl API.
|
|
|
|
|
|
|
|
This API allows you to retrieve your timetracking data.
|
|
|
|
"""
|
|
|
|
|
|
|
|
app = FastAPI(
|
|
|
|
title="Juggl API",
|
|
|
|
description=description,
|
|
|
|
contact={ # Contact information. See https://fastapi.tiangolo.com/tutorial/metadata/
|
|
|
|
"name": "Juggl Management",
|
|
|
|
"url": "https://juggl.giller.dev/contact",
|
|
|
|
"email": "help@juggl.giller.dev",
|
|
|
|
},
|
|
|
|
docs_url="/" # Documentation on start page
|
|
|
|
)
|
|
|
|
"""The main FastAPI instance"""
|
|
|
|
|
|
|
|
# Enable CORS to allow requests from other domains/origins.
|
|
|
|
# See https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=[
|
|
|
|
# Allow **all** foreign requests to access this API.
|
|
|
|
'*'
|
|
|
|
],
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add subroutes
|
|
|
|
app.include_router(project_router)
|
|
|
|
app.include_router(tag_router)
|
|
|
|
app.include_router(record_router)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parse_args()
|
|
|
|
if args.dev:
|
|
|
|
start_dev_server(args.port)
|
|
|
|
else:
|
|
|
|
start_production_server(args.port)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
|
|
"""Parses command line arguments on application startup."""
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Juggl Backend v2",
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter # show default values
|
|
|
|
)
|
|
|
|
parser.add_argument("--dev", default=False, action=argparse.BooleanOptionalAction,
|
|
|
|
help="Start a dev server with auto-reloading")
|
|
|
|
parser.add_argument("--port", type=int, default=8192, help="Port on which the API should run")
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def start_dev_server(port: int):
|
|
|
|
"""Starts a development server with auto-reloading on port."""
|
|
|
|
|
2021-12-21 22:45:28 +01:00
|
|
|
# Host="0.0.0.0" makes the application accessible from other IPs. Necessary when running inside docker
|
2021-12-22 09:06:08 +01:00
|
|
|
uvicorn.run("src.main:app", reload=True, port=port, host="0.0.0.0")
|
2021-12-21 21:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def start_production_server(port: int):
|
|
|
|
"""Starts a production, i.e. performance-optimized server on port."""
|
|
|
|
|
2021-12-21 22:45:28 +01:00
|
|
|
# Host="0.0.0.0" makes the application accessible from other IPs. Necessary when running inside docker
|
2021-12-22 09:06:08 +01:00
|
|
|
uvicorn.run("src.main:app", port=port, host="0.0.0.0")
|
2021-12-21 21:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|