|
| 1 | +# rest-api.ir |
| 2 | + |
| 3 | +## Introduction |
| 4 | +This project is a small web service developed primarily for learning and experimentation, and is still evolving. The goal is to provide a simple executable that can launch a local REST API with minimal setup. If you have ideas or suggestions, I'd be happy to receive pull requests. |
| 5 | + |
| 6 | +## Features |
| 7 | +- **Single Binary Execution**: Simply run `cargo run` or execute the output binary to start all Axum routes with default configurations—no additional tools required. The application listens on `APP_HOST:APP_PORT` and displays the configured domain in the output if set. |
| 8 | +- **Automatic Configuration Generation**: If no `.env` file exists, the application automatically creates one with default values including network settings and SQLite database configuration, allowing you to run immediately without hassle. |
| 9 | +- **Ready-to-Use Utility Routes**: A collection of `/api/v1` endpoints including time, IP detection, and country lists with flags, alongside a health check endpoint—suitable for testing or building widgets. |
| 10 | +- **Structured Responses**: All responses follow the `ApiResponse` structure, providing JSON data with predictable error messages. |
| 11 | +- **Request Statistics**: An in-memory counter tracks requests and persists them to the database every 60 seconds, showing usage metrics for each route. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | +- Rust installation (recent stable version recommended) |
| 15 | +- SQLite, used through the SeaORM driver—no separate installation needed unless you require command-line tools |
| 16 | + |
| 17 | +## Quick Start |
| 18 | +1. Clone the repository and navigate into it |
| 19 | +2. For first-time execution, the following command is sufficient to install dependencies and automatically create the database: |
| 20 | + ```bash |
| 21 | + cargo run |
| 22 | + ``` |
| 23 | +3. The service will be available at `http://127.0.0.1:8080` (or values specified in `.env`) |
| 24 | + |
| 25 | +On first run, a `.env` file is created with the following default values, which you can modify as needed: |
| 26 | +```env |
| 27 | +APP_DOMAIN=localhost |
| 28 | +APP_FINAL_DOMAIN=localhost |
| 29 | +APP_HOST=127.0.0.1 |
| 30 | +APP_PORT=80 |
| 31 | +APP_HTTPS=false |
| 32 | +DATABASE_URL=sqlite://stats.db?mode=rwc |
| 33 | +``` |
| 34 | + |
| 35 | +## Configuration |
| 36 | +- `APP_HOST` and `APP_PORT`: Specify the address and port for the service to listen on |
| 37 | +- `APP_DOMAIN`: If present, displayed in startup logs to indicate if the service is behind a proxy or specific domain |
| 38 | +- `DATABASE_URL`: SeaORM connection string; defaults to creating a local SQLite file |
| 39 | + |
| 40 | +## Main Routes |
| 41 | +| Route | Description | Example | |
| 42 | +| --- | --- | --- | |
| 43 | +| `/health` | Service status, uptime, and usage statistics | `GET /health` | |
| 44 | +| `/api/v1/time` | Current UTC time | `GET /api/v1/time` | |
| 45 | +| `/api/v1/time/{region}/{city}` | Local time with custom timezone | `GET /api/v1/time/asia/tehran` | |
| 46 | +| `/api/v1/ip` | Client IP detection from common headers | `GET /api/v1/ip` | |
| 47 | +| `/api/v1/country` | List of country codes and names | `GET /api/v1/country` | |
| 48 | +| `/api/v1/country/full` | Complete country information including currency, language, and flag | `GET /api/v1/country/full` | |
| 49 | +| `/api/v1/flags/{code}` | Retrieve flag SVG file by two-letter country code | `GET /api/v1/flags/ir.svg` | |
| 50 | + |
| 51 | +## Build and Deployment |
| 52 | +- To build a production-ready release: |
| 53 | + ```bash |
| 54 | + cargo build --release |
| 55 | + ``` |
| 56 | + The executable will be located at `target/release/rest-api.ir` and can be run on any Linux server with appropriate environment variables configured. |
| 57 | +- Database migrations are managed through the `migration` subproject. They run via `cargo run` in the main directory and are automatically applied during application startup. |
| 58 | + |
| 59 | +## Contributing |
| 60 | +This project is a work in progress focused on learning and growth. If you encounter bugs, have ideas, or want to propose new routes, please open an issue or submit a pull request. I welcome constructive feedback and suggestions. |
0 commit comments