-
Notifications
You must be signed in to change notification settings - Fork 4
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
6cbe694
commit 020d735
Showing
12 changed files
with
2,120 additions
and
1,820 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
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
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 |
---|---|---|
|
@@ -11,66 +11,46 @@ | |
from iris.api import agents, maintenance, measurements, status, targets, users | ||
from iris.api.authentication import current_superuser | ||
from iris.api.settings import APISettings | ||
from iris.commons.dependencies import get_settings | ||
|
||
app = FastAPI( | ||
title="🕸️ Iris", | ||
description=""" | ||
Iris is a system to coordinate complex network measurements from multiple vantage points.<br/> | ||
For more information, please visit our website at [iris.dioptra.io](https://iris.dioptra.io) | ||
or browse the source code on [GitHub](https://github.com/dioptra-io/iris). | ||
""", | ||
version=__version__, | ||
openapi_url="/openapi.json", | ||
docs_url="/docs", | ||
redoc_url=None, | ||
contact={"email": "[email protected]"}, | ||
swagger_ui_parameters={ | ||
"displayRequestDuration": True, | ||
"tryItOutEnabled": True, | ||
}, | ||
) | ||
|
||
def make_app(*args, settings: APISettings | None = None) -> FastAPI: | ||
settings = settings or APISettings() | ||
|
||
app.add_middleware(PrometheusMiddleware) | ||
app.add_route("/metrics", handle_metrics) | ||
app = FastAPI( | ||
title="🕸️ Iris", | ||
description=""" | ||
Iris is a system to coordinate complex network measurements from multiple vantage points.<br/> | ||
For more information, please visit our website at [iris.dioptra.io](https://iris.dioptra.io) | ||
or browse the source code on [GitHub](https://github.com/dioptra-io/iris). | ||
""", | ||
version=__version__, | ||
openapi_url="/openapi.json", | ||
docs_url="/docs", | ||
redoc_url=None, | ||
contact={"email": "[email protected]"}, | ||
swagger_ui_parameters={ | ||
"displayRequestDuration": True, | ||
"tryItOutEnabled": True, | ||
}, | ||
) | ||
|
||
app.include_router(users.router) | ||
app.include_router(agents.router, prefix="/agents", tags=["Agents"]) | ||
app.include_router(targets.router, prefix="/targets", tags=["Targets"]) | ||
app.include_router(measurements.router, prefix="/measurements", tags=["Measurements"]) | ||
app.include_router(status.router, prefix="/status", tags=["Status"]) | ||
app.include_router( | ||
maintenance.router, | ||
prefix="/maintenance", | ||
tags=["Maintenance"], | ||
dependencies=[Depends(current_superuser)], | ||
) | ||
app.add_middleware(PrometheusMiddleware) | ||
app.add_route("/metrics", handle_metrics) | ||
|
||
app.include_router(users.router) | ||
app.include_router(agents.router, prefix="/agents", tags=["Agents"]) | ||
app.include_router(targets.router, prefix="/targets", tags=["Targets"]) | ||
app.include_router( | ||
measurements.router, prefix="/measurements", tags=["Measurements"] | ||
) | ||
app.include_router(status.router, prefix="/status", tags=["Status"]) | ||
app.include_router( | ||
maintenance.router, | ||
prefix="/maintenance", | ||
tags=["Maintenance"], | ||
dependencies=[Depends(current_superuser)], | ||
) | ||
|
||
class ReadOnlyMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request, call_next): | ||
if request.method not in ("GET", "HEAD", "OPTIONS"): | ||
return JSONResponse( | ||
dict( | ||
detail="Iris is under maintenance. Write operations are not available." | ||
), | ||
status_code=HTTP_503_SERVICE_UNAVAILABLE, | ||
) | ||
return await call_next(request) | ||
|
||
|
||
@app.exception_handler(botocore.exceptions.ClientError) | ||
def botocore_exception_handler(request, exc): | ||
if exc.response["Error"]["Code"] == "NoSuchKey": | ||
return Response(status_code=HTTP_404_NOT_FOUND) | ||
raise exc | ||
|
||
|
||
@app.on_event("startup") | ||
async def startup_event(): | ||
# Use overridden get_settings when running tests: | ||
settings: APISettings = app.dependency_overrides.get(get_settings, get_settings)() | ||
if settings.API_CORS_ALLOW_ORIGIN: | ||
app.add_middleware( | ||
CORSMiddleware, | ||
|
@@ -79,5 +59,26 @@ async def startup_event(): | |
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
if settings.API_READ_ONLY: | ||
app.add_middleware(ReadOnlyMiddleware) | ||
|
||
@app.exception_handler(botocore.exceptions.ClientError) | ||
def botocore_exception_handler(request, exc): | ||
if exc.response["Error"]["Code"] == "NoSuchKey": | ||
return Response(status_code=HTTP_404_NOT_FOUND) | ||
raise exc | ||
|
||
return app | ||
|
||
|
||
class ReadOnlyMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request, call_next): | ||
if request.method not in ("GET", "HEAD", "OPTIONS"): | ||
return JSONResponse( | ||
dict( | ||
detail="Iris is under maintenance. Write operations are not available." | ||
), | ||
status_code=HTTP_503_SERVICE_UNAVAILABLE, | ||
) | ||
return await call_next(request) |
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
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
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
Oops, something went wrong.