Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SINTEF-9012_SINDIT20 /src /sindit /api /api.py
| import fastapi | |
| from sindit.util.environment_and_configuration import ( | |
| ConfigGroups, | |
| get_configuration, | |
| get_environment_variable_bool, | |
| ) | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware | |
| import tomli | |
| from pathlib import Path | |
| description = """This is the API for the SINDIT project. | |
| It provides access to the knowledge graph and the data stored in it.""" | |
| tags_metadata = [ | |
| { | |
| "name": "Knowledge Graph", | |
| "description": "Operations related to the knowledge graph", | |
| }, | |
| { | |
| "name": "Connection", | |
| "description": "Operations related to the connections and properties", | |
| }, | |
| { | |
| "name": "Dataspace", | |
| "description": "Operations related to the dataspace", | |
| }, | |
| { | |
| "name": "Workspace", | |
| "description": "Operations related to the workspace", | |
| }, | |
| { | |
| "name": "Vault", | |
| "description": "Operations related to the secret vault and authenitcation", | |
| }, | |
| { | |
| "name": "Metamodel", | |
| "description": "Operations related to the metamodel (e.g., units, semantics)", | |
| }, | |
| ] | |
| def get_version_from_pyproject(): | |
| """Read version from pyproject.toml file.""" | |
| try: | |
| # Get the path to pyproject.toml | |
| current_file = Path(__file__) | |
| # Check if running in Docker environment | |
| is_docker = get_environment_variable_bool( | |
| "DOCKER_ENV", optional=True, default="False" | |
| ) | |
| if is_docker: | |
| # In Docker: /app/sindit/api/api.py -> /app/pyproject.toml | |
| project_root = current_file.parent.parent.parent | |
| else: | |
| # Local: .../src/sindit/api/api.py -> .../pyproject.toml | |
| project_root = current_file.parent.parent.parent.parent | |
| pyproject_path = project_root / "pyproject.toml" | |
| if pyproject_path.exists(): | |
| with open(pyproject_path, "rb") as f: | |
| pyproject_data = tomli.load(f) | |
| return pyproject_data.get("project", {}).get("version", "unknown") | |
| except Exception as e: | |
| print(f"Failed to read version from pyproject.toml: {e}") | |
| # Fallback to configuration | |
| return get_configuration(ConfigGroups.GENERIC, "sindit_version") | |
| api_version = get_version_from_pyproject() | |
| app = fastapi.FastAPI( | |
| title="SINDIT API", | |
| description=description, | |
| version=api_version, | |
| openapi_tags=tags_metadata, | |
| license_info={"name": "MIT", "url": "https://opensource.org/licenses/MIT"}, | |
| ) | |
| # Add ProxyHeadersMiddleware so FastAPI sees the correct scheme/host behind Traefik | |
| # Otherwise, DELETE get into a https > http > https ping pong where auth header is lost | |
| app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*") | |
| # TODO: This should not be hardcoded | |
| # TODO: For actual deployment, this needs to be set properly | |
| origins = ["*"] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["GET", "POST", "DELETE", "PUT"], | |
| allow_headers=["*"], | |
| ) | |
Xet Storage Details
- Size:
- 3.06 kB
- Xet hash:
- 8669b24d15052bfb2bb29c6bf3e41db01f6b83db9534856f755ac896571ddfb3
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.