forked from Materials-Consortia/optimade-python-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By calling `./run.sh index`, the index meta-database will be run at port 5001. This means one can run both servers on the same machine.
- Loading branch information
Showing
10 changed files
with
187 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,3 +115,4 @@ venv.bak/ | |
|
||
Untitled.ipynb | ||
local_openapi.json | ||
local_index_openapi.json |
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
"_id": { | ||
"$oid": "746573745f73657276657263" | ||
}, | ||
"task_id": "test_server", | ||
"type": "child", | ||
"name": "OPTiMaDe API", | ||
"description": "The [Open Databases Integration for Materials Design (OPTiMaDe) consortium](http://http://www.optimade.org/) aims to make materials databases interoperational by developing a common REST API.", | ||
"base_url": "http://localhost:5000/optimade" | ||
} | ||
] |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from pydantic import ValidationError | ||
from fastapi import FastAPI | ||
from fastapi.exceptions import RequestValidationError | ||
from starlette.exceptions import HTTPException as StarletteHTTPException | ||
|
||
from .config import CONFIG | ||
from .routers import index_info, links | ||
|
||
import optimade.server.exception_handlers as exc_handlers | ||
|
||
|
||
app = FastAPI( | ||
title="OPTiMaDe API - Index meta-database", | ||
description=( | ||
"The [Open Databases Integration for Materials Design (OPTiMaDe) consortium]" | ||
"(http://http://www.optimade.org/) aims to make materials databases interoperational " | ||
"by developing a common REST API.\n" | ||
'This is the "special" index meta-database.' | ||
), | ||
version=CONFIG.version, | ||
docs_url="/index/optimade/extensions/docs", | ||
redoc_url="/index/optimade/extensions/redoc", | ||
openapi_url="/index/optimade/extensions/openapi.json", | ||
) | ||
|
||
|
||
index_links_path = Path(__file__).resolve().parent.joinpath("index_links.json") | ||
if not CONFIG.use_real_mongo and index_links_path.exists(): | ||
import bson.json_util | ||
from .routers.links import links_coll | ||
|
||
print("loading index links...") | ||
with open(index_links_path) as f: | ||
data = json.load(f) | ||
print("inserting index links into collection...") | ||
links_coll.collection.insert_many( | ||
bson.json_util.loads(bson.json_util.dumps(data)) | ||
) | ||
print("done inserting index links...") | ||
|
||
|
||
app.add_exception_handler(StarletteHTTPException, exc_handlers.http_exception_handler) | ||
app.add_exception_handler( | ||
RequestValidationError, exc_handlers.request_validation_exception_handler | ||
) | ||
app.add_exception_handler(ValidationError, exc_handlers.validation_exception_handler) | ||
app.add_exception_handler(Exception, exc_handlers.general_exception_handler) | ||
|
||
|
||
# Create the following prefixes: | ||
# /optimade | ||
# /optimade/vMajor (but only if Major >= 1) | ||
# /optimade/vMajor.Minor | ||
# /optimade/vMajor.Minor.Patch | ||
valid_prefixes = ["/index/optimade"] | ||
version = [int(_) for _ in app.version.split(".")] | ||
while version: | ||
if version[0] or len(version) >= 2: | ||
valid_prefixes.append( | ||
"/index/optimade/v{}".format(".".join([str(_) for _ in version])) | ||
) | ||
version.pop(-1) | ||
|
||
for prefix in valid_prefixes: | ||
app.include_router(index_info.router, prefix=prefix) | ||
app.include_router(links.router, prefix=prefix) | ||
|
||
|
||
def update_schema(app): | ||
"""Update OpenAPI schema in file 'local_index_openapi.json'""" | ||
with open("local_index_openapi.json", "w") as f: | ||
json.dump(app.openapi(), f, indent=2) | ||
|
||
|
||
@app.on_event("startup") | ||
async def startup_event(): | ||
update_schema(app) |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import Union | ||
|
||
from fastapi import APIRouter | ||
from starlette.requests import Request | ||
|
||
from optimade.models import ( | ||
ErrorResponse, | ||
IndexInfoResponse, | ||
IndexInfoAttributes, | ||
IndexInfoResource, | ||
IndexRelationship, | ||
) | ||
|
||
from optimade.server.config import CONFIG | ||
|
||
from .utils import meta_values | ||
|
||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get( | ||
"/info", | ||
response_model=Union[IndexInfoResponse, ErrorResponse], | ||
response_model_skip_defaults=False, | ||
tags=["Info"], | ||
) | ||
def get_info(request: Request): | ||
return IndexInfoResponse( | ||
meta=meta_values(str(request.url), 1, 1, more_data_available=False), | ||
data=IndexInfoResource( | ||
attributes=IndexInfoAttributes( | ||
api_version=f"v{CONFIG.version}", | ||
available_api_versions=[ | ||
{ | ||
"url": f"{CONFIG.provider['index_base_url']}/v{CONFIG.version}/", | ||
"version": f"{CONFIG.version}", | ||
} | ||
], | ||
entry_types_by_format={"json": ["links"]}, | ||
available_endpoints=["info", "links"], | ||
is_index=True, | ||
), | ||
relationships={ | ||
"default": IndexRelationship( | ||
data={"type": "child", "id": CONFIG.default_db} | ||
) | ||
}, | ||
), | ||
) |
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 |
---|---|---|
@@ -1,15 +1,12 @@ | ||
[ | ||
{ | ||
"_id": { | ||
"$oid": "5cfb441f053b174410700d03" | ||
}, | ||
"id": "index", | ||
"last_modified": { | ||
"$date": "2019-11-12T14:24:37.331Z" | ||
}, | ||
"type": "parent", | ||
"name": "Index meta-database", | ||
"description": "Index for example's OPTiMaDe databases", | ||
"base_url": "http://example.com/optimade/index" | ||
} | ||
] | ||
{ | ||
"_id": { | ||
"$oid": "696e646578706172656e7430" | ||
}, | ||
"task_id": "index", | ||
"type": "parent", | ||
"name": "Index meta-database", | ||
"description": "Index for example's OPTiMaDe databases", | ||
"base_url": "http://localhost:5001/index/optimade" | ||
} | ||
] |
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 |
---|---|---|
@@ -1,2 +1,11 @@ | ||
#!/bin/bash | ||
uvicorn optimade.server.main:app --reload --port 5000 | ||
if [ "$1" == "index" ] | ||
then | ||
MAIN="main_index" | ||
PORT=5001 | ||
else | ||
MAIN="main" | ||
PORT=5000 | ||
fi | ||
|
||
uvicorn optimade.server.$MAIN:app --reload --port $PORT |