MongoDB backend for the stac-fastapi project built on top of the sfeos core API library.
This project is built on the following technologies: STAC, stac-fastapi, SFEOS core, FastAPI, MongoDB, Python
- Installation
- Development
- Running the API
- Testing
- Sample Data
- Authentication
- Read-Only Databases
- Contributing
- Changelog
To install from PyPI:
pip install stac_fastapi.mongo
To install the classes in your local Python env, run:
pip install -e .[dev]
Install pre-commit.
Prior to commit, run:
pre-commit run --all-files
Build the MongoDB backend:
docker-compose up mongo
docker-compose build app-mongo
Run the MongoDB API on localhost:8084:
docker-compose up app-mongo
To create a new Collection:
curl -X "POST" "http://localhost:8084/collections" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"id": "my_collection"
}'
Note: this "Collections Transaction" behavior is not part of the STAC API, but may be soon.
The collections route handles optional limit
and token
parameters. The links
field that is
returned from the /collections
route contains a next
link with the token that can be used to
get the next page of results.
curl -X "GET" "http://localhost:8084/collections?limit=1&token=example_token"
Run the test suite:
make test
Ingest sample data:
make ingest
Basic authentication is an optional feature. You can enable it by setting the environment variable STAC_FASTAPI_ROUTE_DEPENDENCIES
as a JSON string.
Example:
STAC_FASTAPI_ROUTE_DEPENDENCIES=[{"routes":[{"method":"*","path":"*"}],"dependencies":[{"method":"stac_fastapi.core.models.basic_auth.BasicAuth","kwargs":{"credentials":[{"username":"admin","password":"admin"}]}}]}]
The STAC_FASTAPI_ROUTE_DEPENDENCIES
environment variable allows you to configure different levels of authentication for different routes. The configuration is a JSON array of objects, each with two properties:
routes
: An array of route objects, each withmethod
andpath
propertiesdependencies
: An array of dependency objects, each withmethod
andkwargs
properties
This example configures all routes to require admin authentication:
[
{
"routes": [
{
"method": "*",
"path": "*"
}
],
"dependencies": [
{
"method": "stac_fastapi.core.models.basic_auth.BasicAuth",
"kwargs": {
"credentials": [
{
"username": "admin",
"password": "admin"
}
]
}
}
]
}
]
This example makes specific endpoints public while requiring admin authentication for all others:
[
{
"routes": [
{
"method": "*",
"path": "*"
}
],
"dependencies": [
{
"method": "stac_fastapi.core.models.basic_auth.BasicAuth",
"kwargs": {
"credentials": [
{
"username": "admin",
"password": "admin"
}
]
}
}
]
},
{
"routes": [
{"path": "/", "method": ["GET"]},
{"path": "/conformance", "method": ["GET"]},
{"path": "/collections/{collection_id}/items/{item_id}", "method": ["GET"]},
{"path": "/search", "method": ["GET", "POST"]},
{"path": "/collections", "method": ["GET"]},
{"path": "/collections/{collection_id}", "method": ["GET"]},
{"path": "/collections/{collection_id}/items", "method": ["GET"]},
{"path": "/queryables", "method": ["GET"]},
{"path": "/queryables/collections/{collection_id}/queryables", "method": ["GET"]},
{"path": "/_mgmt/ping", "method": ["GET"]}
],
"dependencies": []
}
]
This example configures admin authentication for all routes, with a separate reader user that can access specific read-only endpoints:
[
{
"routes": [
{
"method": "*",
"path": "*"
}
],
"dependencies": [
{
"method": "stac_fastapi.core.models.basic_auth.BasicAuth",
"kwargs": {
"credentials": [
{
"username": "admin",
"password": "admin"
}
]
}
}
]
},
{
"routes": [
{"path": "/", "method": ["GET"]},
{"path": "/conformance", "method": ["GET"]},
{"path": "/collections/{collection_id}/items/{item_id}", "method": ["GET"]},
{"path": "/search", "method": ["GET", "POST"]},
{"path": "/collections", "method": ["GET"]},
{"path": "/collections/{collection_id}", "method": ["GET"]},
{"path": "/collections/{collection_id}/items", "method": ["GET"]},
{"path": "/queryables", "method": ["GET"]},
{"path": "/queryables/collections/{collection_id}/queryables", "method": ["GET"]},
{"path": "/_mgmt/ping", "method": ["GET"]}
],
"dependencies": [
{
"method": "stac_fastapi.core.models.basic_auth.BasicAuth",
"kwargs": {
"credentials": [
{
"username": "reader",
"password": "reader"
}
]
}
}
]
}
]
If you are using a read-only MongoDB user, the MONGO_CREATE_INDEXES
environment variable should be set to "false" (as a string and not a boolean) to avoid creating indexes in the database. When this environment variable is not set, the default is to create indexes. See GitHub issue #28
Contributions are welcome! Here's how you can help:
- Fork the repository - Create your own fork of the project
- Create a feature branch -
git checkout -b feature/your-feature-name
- Commit your changes - Make sure to write clear, concise commit messages
- Push to your branch -
git push origin feature/your-feature-name
- Open a Pull Request - Describe your changes in detail
- Follow the existing code style and conventions
- Add tests for new features
- Update documentation as needed
- Make sure all tests pass before submitting a PR
If you find a bug or have a feature request, please open an issue on the GitHub repository.
For changes, see the Changelog