Skip to content

Commit a3e76e6

Browse files
committed
add mongo folder
1 parent 6e946f7 commit a3e76e6

33 files changed

+3426
-0
lines changed

stac_fastpi/mongo/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Requirements
2+
3+
The Mongo backend requires **mongodb**.

stac_fastpi/mongo/pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
testpaths = tests
3+
addopts = -sv

stac_fastpi/mongo/setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
version = attr: stac_fastapi.mongo.version.__version__

stac_fastpi/mongo/setup.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""stac_fastapi: mongo module."""
2+
3+
from setuptools import find_namespace_packages, setup
4+
5+
with open("README.md") as f:
6+
desc = f.read()
7+
8+
install_requires = [
9+
"attrs",
10+
"pydantic[dotenv]",
11+
"stac_pydantic==2.0.*",
12+
"stac-fastapi.types",
13+
"stac-fastapi.api",
14+
"stac-fastapi.extensions",
15+
"fastapi-utils",
16+
"pymongo",
17+
]
18+
19+
extra_reqs = {
20+
"dev": [
21+
"pytest",
22+
"pytest-cov",
23+
"pytest-asyncio",
24+
"pre-commit",
25+
"requests",
26+
],
27+
"docs": ["mkdocs", "mkdocs-material", "pdocs"],
28+
"server": ["uvicorn[standard]>=0.12.0,<0.14.0"],
29+
}
30+
31+
32+
setup(
33+
name="stac-fastapi.mongo",
34+
description="An implementation of STAC API based on the FastAPI framework.",
35+
long_description=desc,
36+
long_description_content_type="text/markdown",
37+
python_requires=">=3.8",
38+
classifiers=[
39+
"Intended Audience :: Developers",
40+
"Intended Audience :: Information Technology",
41+
"Intended Audience :: Science/Research",
42+
"Programming Language :: Python :: 3.8",
43+
"License :: OSI Approved :: MIT License",
44+
],
45+
keywords="STAC FastAPI COG",
46+
author=u"Arturo Engineering",
47+
author_email="engineering@arturo.ai",
48+
url="https://github.com/stac-utils/stac-fastapi",
49+
license="MIT",
50+
packages=find_namespace_packages(exclude=["alembic", "tests", "scripts"]),
51+
zip_safe=False,
52+
install_requires=install_requires,
53+
tests_require=extra_reqs["dev"],
54+
extras_require=extra_reqs,
55+
entry_points={"console_scripts": ["stac-fastapi-mongo=stac_fastapi.mongo.app:run"]},
56+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""mongo submodule."""
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""FastAPI application."""
2+
from stac_fastapi.api.app import StacApi
3+
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
4+
from stac_fastapi.extensions.core import (
5+
ContextExtension,
6+
FieldsExtension,
7+
SortExtension,
8+
TokenPaginationExtension,
9+
TransactionExtension,
10+
)
11+
from stac_fastapi.extensions.third_party import BulkTransactionExtension
12+
from stac_fastapi.mongo.config import MongoSettings
13+
from stac_fastapi.mongo.core import CoreCrudClient
14+
from stac_fastapi.mongo.extensions import QueryExtension
15+
from stac_fastapi.mongo.session import Session
16+
from stac_fastapi.mongo.transactions import BulkTransactionsClient, TransactionsClient
17+
18+
settings = MongoSettings()
19+
session = Session.create_from_settings(settings)
20+
21+
extensions = [
22+
TransactionExtension(client=TransactionsClient(session=session), settings=settings),
23+
BulkTransactionExtension(client=BulkTransactionsClient(session=session)),
24+
FieldsExtension(),
25+
QueryExtension(),
26+
SortExtension(),
27+
TokenPaginationExtension(),
28+
ContextExtension(),
29+
]
30+
31+
post_request_model = create_post_request_model(extensions)
32+
33+
api = StacApi(
34+
settings=settings,
35+
extensions=extensions,
36+
client=CoreCrudClient(session=session, post_request_model=post_request_model),
37+
search_get_request_model=create_get_request_model(extensions),
38+
search_post_request_model=post_request_model,
39+
)
40+
app = api.app
41+
42+
43+
def run():
44+
"""Run app from command line using uvicorn if available."""
45+
try:
46+
import uvicorn
47+
48+
uvicorn.run(
49+
"stac_fastapi.mongo.app:app",
50+
host=settings.app_host,
51+
port=settings.app_port,
52+
log_level="info",
53+
reload=settings.reload,
54+
)
55+
except ImportError:
56+
raise RuntimeError("Uvicorn must be installed in order to use command")
57+
58+
59+
if __name__ == "__main__":
60+
run()
61+
62+
63+
def create_handler(app):
64+
"""Create a handler to use with AWS Lambda if mangum available."""
65+
try:
66+
from mangum import Mangum
67+
68+
return Mangum(app)
69+
except ImportError:
70+
return None
71+
72+
73+
handler = create_handler(app)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""API configuration."""
2+
import os
3+
from typing import Set
4+
5+
from pymongo import GEO2D, GEOSPHERE, MongoClient, errors
6+
7+
from stac_fastapi.types.config import ApiSettings
8+
9+
DOMAIN = os.getenv("MONGO_HOST")
10+
PORT = os.getenv("MONGO_PORT")
11+
12+
13+
class MongoSettings(ApiSettings):
14+
"""API settings."""
15+
16+
# Fields which are defined by STAC but not included in the database model
17+
forbidden_fields: Set[str] = {"type"}
18+
19+
# Fields which are item properties but indexed as distinct fields in the database model
20+
indexed_fields: Set[str] = {"datetime"}
21+
22+
@property
23+
def create_client(self):
24+
"""Create mongo client."""
25+
try:
26+
client = MongoClient(
27+
host=[str(DOMAIN) + ":" + str(PORT)],
28+
serverSelectionTimeoutMS=3000,
29+
username=os.getenv("MONGO_USER"),
30+
password=os.getenv("MONGO_PASS"),
31+
)
32+
33+
# create indices - they are only created if they don't already exist
34+
item_table = client.stac.stac_item
35+
item_table.create_index([("bbox", GEOSPHERE), ("properties.datetime", 1)])
36+
item_table.create_index([("geometry", GEOSPHERE)])
37+
item_table.create_index([("properties.datetime", 1)])
38+
item_table.create_index([("properties.created", 1)])
39+
item_table.create_index([("properties.updated", 1)])
40+
item_table.create_index([("bbox", GEOSPHERE)])
41+
item_table.create_index([("bbox", GEO2D)])
42+
43+
except errors.ServerSelectionTimeoutError as err:
44+
client = None
45+
print("pymongo ERROR:", err)
46+
47+
return client

0 commit comments

Comments
 (0)