Backend API Service for the real estate management system. This application is part of the Realty platform. It works in conjunction with the Frontend User Interface to provide a complete solution for managing commercial real estate properties.
- RESTful API endpoints for managing properties, buildings, units, leases and tenants
- JWT authentication and authorization
- Role-based access control
- Database migrations with Alembic
- Logging with configurable outputs
- Docker support for development and production
- Comprehensive test suite
- CORS configuration
- Request ID tracking
- Configurable alerts (email, SMS, Slack)
- More...
- Python 3
- FastAPI
- PostgreSQL
- SQLAlchemy
- Alembic
- Redis
- Docker
- Pytest
- More...
- Python 3.13+
- PostgreSQL
- Redis
- Docker (optional)
The frontend UI is a separate application that works in conjunction with the backend API. It is available at: https://github.com/dnstock/realty-frontend
This app uses Poetry for dependency management. Install it first if you don't have it already:
curl -sSL https://install.python-poetry.org | python3 -
# or
pip install poetryClone the repository:
git clone git@github.com:dnstock/realty-backend.git
cd realty-backendInstall dependencies:
poetry installActivate the virtual environment:
poetry shell
# or
source $(poetry env info --path)/bin/activateCreate a PostgreSQL database:
createdb realtyApply the initial database schema:
alembic upgrade head
# or
./scripts/dev_migrateCreate a .env file from the template:
cp envs/.env_template envs/.envEdit the .env file and configure the environment variables.
vim envs/.envStart the development server:
uvicorn app.main:app --reload
# or
./scripts/dev_runThe API will be available at:
http://localhost:8000
Build and run with Docker Compose:
docker-compose up --buildFor production:
docker-compose -f docker-compose.prod.yml up -dCreate a new migration:
alembic revision --autogenerate -m "Description"
# or
./scripts/migrate "Description"Apply migrations:
alembic upgrade head
# or
./scripts/dev_migrateExecute the test suite:
pytestTo run a specific test file:
pytest app/tests/test_example.py
# or
./scripts/test exampleTo run a specific test case:
pytest app/tests/test_example.py::test_example
# or
./scripts/test example test_exampleUsage of the scripts/test script:
./scripts/test <test_file> [test_case]Run the linter:
flake8Format the code:
black .View the log files in the logs directory:
tail -f logs/app.logThe log level can be configured in the .env file:
LOG_LEVEL=debugThe log format can also be configured:
LOG_FORMAT=jsonOther logging configuration options are available in the .env file.
Once running, view and test the API with Swagger:
http://localhost:8000/docs
ReDoc can also be used for a more structured view:
http://localhost:8000/redocThe API uses JWT for authentication. Obtain a token by sending a POST request to the /auth/login endpoint with the following payload:
{
"username": "admin",
"password": "password"
}Use the token in the Authorization header for subsequent requests:
{
"Authorization": "Bearer <token>"
}The API uses role-based access control. The available roles are:
adminmanagerstafftenant
The admin role has full access to all resources. The other roles have specific permissions based on the resource.
The API has CORS enabled by default. The allowed origins can be configured in the .env file:
API_CORS_ORIGINS=["http://localhost:3000"]Each request is assigned a unique ID that is included in the response headers. This can be used for tracking and debugging purposes.
Log messages are also tagged with the request ID for easier correlation.
The API can be configured to send alerts via email, SMS or Slack. The alert settings can be configured in the .env file:
ALERT_EMAIL_ENABLED=true
ALERT_SMS_ENABLED=false
ALERT_SLACK_ENABLED=falseThe scripts directory contains utility scripts for common tasks:
dev_run: Start the development serverdev_migrate: Apply database migrationsmigrate: Create a new database migrationtest: Run specific tests or test filesactivate: Print the virtual environment activation commandclear_cache: Clear the LRU cachegenerate_secret_key: Generate a new secret key (for JWT)
Sample environment variables in configuration template:
# Application
APP_NAME=realty-api
APP_ENV=development
APP_DEBUG=true
# Database
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=realty
# Authentication
JWT_SECRET_KEY=secret
JWT_ALGORITHM=HS256
# Logging
LOG_LEVEL=debug
LOG_FORMAT=jsonSee the envs/.env_template file for all available configuration options.
.
├── alembic/ # Database migrations
│ └── versions/ # Migration scripts
├── app/
│ ├── api/ # API routes and core logic
│ │ └── v1/ # API version 1
│ │ └── endpoints/ # API endpoints
│ ├── controllers/ # Business logic
│ ├── core/ # Core functionality
│ ├── db/ # Database core logic
│ │ └── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic models
│ │ └── utils/ # Schema utilities
│ └── tests/ # Test suite
├── envs/ # Environment settings
├── logs/ # App log files
├── scripts/ # Utility scripts
├── pyproject.toml # Poetry configuration
└── README.md # Project README
Contributions are welcome!
To contribute, please follow these steps:
- Fork the repository
gh repo fork git@github.com:dnstock/realty-backend.git- Create a new branch
git checkout -b feature/my-feature- Make your changes
git add .
git commit -m "Add my feature"- Commit your changes to your fork
git push origin feature/my-feature- Create a pull request
gh pr create- Wait for review and approval
gh pr status- Make any requested changes
git add .
git commit -m "Address review comments"
git push origin feature/my-feature- Merge your changes
gh pr merge- Celebrate your contribution!
🎉For support, please contact the author.
This project is open source and available under the MIT License - see the LICENSE file for details.