Persistent sensor storage of coupled data in database via API
Choices:
a. Fastapi is a familiar technology that is more than sufficient for this task
b. Uvicorn is a fast ASGI web server for Python
c. SqlAlchemy
d. Postgresql as Test and Production databases
This project implements a RESTful API using FastAPI to manage project nodes and sensors. The API provides endpoints to create, retrieve, update, and connect nodes and sensors. The project is containerized with Docker for production deployment and uses Pixi for development environment management and dependency resolution.
Pixi – A fast, cross-platform package manager built atop the conda ecosystem. Docker – For containerized deployment. Git – To clone and manage the source code.
If you prefer a containerized setup, you can use Docker and Docker Compose: It is recommended to have python >= 3.11 Build and start the containers:
docker compose -f docker/docker-compose.yml up --build
The API will be available at http://localhost:8000.
To take down:
docker compose -f docker/docker-compose.yml down
- Running the API outside of the container
With your environment set up by running pixi install
in the repository root, you can run the FastAPI application using Pixi. For example:
pixi run uvicorn src.persistent_sensor_storage.main:app --host 0.0.0.0 --port 8000
This command launches Uvicorn to serve your FastAPI app from the Pixi-managed environment. The API will be available at http://localhost:8000, with interactive documentation at http://localhost:8000/docs. You can access the curl commands from /docs
- Running Tests To run the automated tests (located in the tests/ directory), execute:
pixi run pytest
This will run your test suite using the environment that Pixi has created.
persistent_sensor_storage/
├── src/
│ └── persistent_sensor_storage/
│ ├── __init__.py
│ ├── main.py # FastAPI entry point; sets up routes and creates tables
│ ├── models.py # SQLAlchemy ORM models for Nodes & Sensors
│ ├── schemas.py # Pydantic models for request & response validation
│ ├── database.py # Database engine and session setup
│ ├── crud.py # CRUD operations for nodes and sensors
│ ├── dependencies.py # Dependency functions (e.g., DB session)
│ ├── config.py # Configuration settings (e.g., DATABASE_URL)
│ └── routers/
│ ├── __init__.py
│ ├── nodes.py # API endpoints for node resource
│ └── sensors.py # API endpoints for sensor resource
├── tests/
| ├── conftest.py # Tests the configuration of the database
│ ├── test_main.py # Basic API tests (e.g., health check)
│ ├── test_nodes.py # Tests for node endpoints
│ └── test_sensors.py # Tests for sensor endpoints
├── migrations/ # (Optional) Alembic migrations for database schema changes
├── docker/
│ ├── Dockerfile # Dockerfile to containerize the FastAPI app
│ └── docker-compose.yml # Compose file to run the app with a PostgreSQL container
├── .env # Environment variables (e.g., DATABASE_URL)
├── requirements.txt # Python dependencies (for Docker build and manual setup)
├── pyproject.toml # Project manifest for Pixi (generated by `pixi init`)
├── pixi.lock # Lockfile generated by Pixi for reproducible environments
├── README.md # Project overview and instructions (this file)
└── .gitignore # Files to ignore in Git (e.g., __pycache__, .env, pixi.lock)
FastAPI automatically generates interactive documentation:
OpenAPI UI: http://localhost:8000/docs ReDoc: http://localhost:8000/redoc
GPL 3.0