2020-10-02 09:37:50 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
FastAPI app
|
|
|
|
"""
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2021-12-24 04:35:39 +02:00
|
|
|
from fastapi import FastAPI, Request, HTTPException
|
2020-10-02 09:37:50 +03:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from fastapi.responses import JSONResponse
|
2021-05-16 12:07:17 +03:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2021-04-17 12:03:20 +03:00
|
|
|
from uvicorn.main import Server as UvicornServer
|
2020-11-19 06:51:03 +02:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
from gns3server.controller.controller_error import (
|
|
|
|
ControllerError,
|
|
|
|
ControllerNotFoundError,
|
2020-12-02 10:09:08 +02:00
|
|
|
ControllerBadRequestError,
|
2020-10-02 09:37:50 +03:00
|
|
|
ControllerTimeoutError,
|
|
|
|
ControllerForbiddenError,
|
2021-04-13 12:16:50 +03:00
|
|
|
ControllerUnauthorizedError,
|
2022-04-07 11:21:47 +03:00
|
|
|
ComputeConflictError
|
2020-10-02 09:37:50 +03:00
|
|
|
)
|
|
|
|
|
2020-11-19 06:51:03 +02:00
|
|
|
from gns3server.api.routes import controller, index
|
|
|
|
from gns3server.api.routes.compute import compute_api
|
|
|
|
from gns3server.core import tasks
|
2020-10-02 09:37:50 +03:00
|
|
|
from gns3server.version import __version__
|
|
|
|
|
|
|
|
import logging
|
2021-04-13 12:16:50 +03:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2020-11-19 06:51:03 +02:00
|
|
|
|
2020-12-07 08:22:36 +02:00
|
|
|
def get_application() -> FastAPI:
|
|
|
|
|
|
|
|
application = FastAPI(
|
2021-04-13 12:16:50 +03:00
|
|
|
title="GNS3 controller API", description="This page describes the public controller API for GNS3", version="v3"
|
2020-12-07 08:22:36 +02:00
|
|
|
)
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2020-12-07 08:22:36 +02:00
|
|
|
application.add_middleware(
|
2021-04-13 12:16:50 +03:00
|
|
|
CORSMiddleware,
|
2021-12-19 09:40:15 +02:00
|
|
|
allow_origin_regex=r"http(s)?://(localhost|127.0.0.1)(:\d+)?",
|
2021-04-13 12:16:50 +03:00
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
2020-12-07 08:22:36 +02:00
|
|
|
|
|
|
|
application.add_event_handler("startup", tasks.create_startup_handler(application))
|
|
|
|
application.add_event_handler("shutdown", tasks.create_shutdown_handler(application))
|
|
|
|
application.include_router(index.router, tags=["Index"])
|
|
|
|
application.include_router(controller.router, prefix="/v3")
|
2021-10-21 14:08:36 +03:00
|
|
|
application.mount("/v3/compute", compute_api, name="compute")
|
2020-12-07 08:22:36 +02:00
|
|
|
|
|
|
|
return application
|
|
|
|
|
|
|
|
|
|
|
|
app = get_application()
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-04-17 12:03:20 +03:00
|
|
|
# Monkey Patch uvicorn signal handler to detect the application is shutting down
|
|
|
|
app.state.exiting = False
|
|
|
|
unicorn_exit_handler = UvicornServer.handle_exit
|
|
|
|
|
|
|
|
|
|
|
|
def handle_exit(*args, **kwargs):
|
|
|
|
app.state.exiting = True
|
|
|
|
unicorn_exit_handler(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
UvicornServer.handle_exit = handle_exit
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
@app.exception_handler(ControllerError)
|
|
|
|
async def controller_error_handler(request: Request, exc: ControllerError):
|
2020-10-22 08:49:44 +03:00
|
|
|
log.error(f"Controller error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=409,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(ControllerTimeoutError)
|
|
|
|
async def controller_timeout_error_handler(request: Request, exc: ControllerTimeoutError):
|
2020-10-22 08:49:44 +03:00
|
|
|
log.error(f"Controller timeout error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=408,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(ControllerUnauthorizedError)
|
|
|
|
async def controller_unauthorized_error_handler(request: Request, exc: ControllerUnauthorizedError):
|
2020-10-22 08:49:44 +03:00
|
|
|
log.error(f"Controller unauthorized error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=401,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(ControllerForbiddenError)
|
|
|
|
async def controller_forbidden_error_handler(request: Request, exc: ControllerForbiddenError):
|
2020-10-22 08:49:44 +03:00
|
|
|
log.error(f"Controller forbidden error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=403,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(ControllerNotFoundError)
|
|
|
|
async def controller_not_found_error_handler(request: Request, exc: ControllerNotFoundError):
|
2020-10-22 08:49:44 +03:00
|
|
|
log.error(f"Controller not found error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=404,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
@app.exception_handler(ControllerBadRequestError)
|
|
|
|
async def controller_bad_request_error_handler(request: Request, exc: ControllerBadRequestError):
|
|
|
|
log.error(f"Controller bad request error: {exc}")
|
|
|
|
return JSONResponse(
|
|
|
|
status_code=400,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-04-07 11:21:47 +03:00
|
|
|
@app.exception_handler(ComputeConflictError)
|
|
|
|
async def compute_conflict_error_handler(request: Request, exc: ComputeConflictError):
|
|
|
|
log.error(f"Controller received error from compute for request '{exc.url()}': {exc}")
|
|
|
|
return JSONResponse(
|
|
|
|
status_code=409,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-11-02 09:38:25 +02:00
|
|
|
# make sure the content key is "message", not "detail" per default
|
2021-12-24 04:35:39 +02:00
|
|
|
@app.exception_handler(HTTPException)
|
|
|
|
async def http_exception_handler(request: Request, exc: HTTPException):
|
2020-11-02 09:38:25 +02:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=exc.status_code,
|
|
|
|
content={"message": exc.detail},
|
2021-12-24 04:35:39 +02:00
|
|
|
headers=exc.headers
|
2020-11-02 09:38:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-16 12:07:17 +03:00
|
|
|
@app.exception_handler(SQLAlchemyError)
|
|
|
|
async def sqlalchemry_error_handler(request: Request, exc: SQLAlchemyError):
|
|
|
|
log.error(f"Controller database error: {exc}")
|
|
|
|
return JSONResponse(
|
|
|
|
status_code=500,
|
|
|
|
content={"message": "Database error detected, please check logs to find details"},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@app.middleware("http")
|
|
|
|
async def add_extra_headers(request: Request, call_next):
|
|
|
|
start_time = time.time()
|
|
|
|
response = await call_next(request)
|
|
|
|
process_time = time.time() - start_time
|
|
|
|
response.headers["X-Process-Time"] = str(process_time)
|
2021-04-13 12:07:58 +03:00
|
|
|
response.headers["X-GNS3-Server-Version"] = f"{__version__}"
|
2020-10-02 09:37:50 +03:00
|
|
|
return response
|