|
1 | 1 | <p align="center"> |
2 | 2 | <img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" alt="FAST API"/> |
3 | 3 | <h2 align="center"> FastAPI Template </h2> |
4 | | -<h4 align="center"> A template for the beginners </h4> |
5 | | - |
| 4 | +<h4 align="center"> A template for beginners </h4> |
6 | 5 |
|
7 | 6 | --- |
8 | 7 | ## About |
9 | | -This is a beginner's template for getting started with FastAPI. |
10 | | -It uses SQLAlchemy as the ORM. |
11 | | - |
12 | | -Contributions are welcome. |
| 8 | +This is a beginner-friendly template for getting started with FastAPI and SQLAlchemy. |
13 | 9 |
|
14 | 10 | ## Features |
| 11 | +- [x] Database connection using SQLAlchemy |
| 12 | +- [x] FastAPI server |
| 13 | +- [x] Unit testing with PyTest |
| 14 | +- [x] Basic CRUD for posts |
15 | 15 |
|
16 | | -- [x] Database Connection Using SQLAlchemy |
17 | | -- [x] FastAPI Server |
18 | | -- [x] Unit Testing with PyTest |
19 | | -- [x] Basic CRUD for Posts |
| 16 | +## Requirements |
| 17 | +- Python 3.10+ |
| 18 | +- `pip` |
| 19 | +- PostgreSQL database |
20 | 20 |
|
21 | | -<br> |
| 21 | +## Setup |
| 22 | +1. Create and activate a virtual environment: |
22 | 23 |
|
23 | | -## Dependencies |
| 24 | +```bash |
| 25 | +python3 -m venv .venv |
| 26 | +source .venv/bin/activate |
| 27 | +``` |
| 28 | + |
| 29 | +2. Install dependencies: |
| 30 | + |
| 31 | +```bash |
| 32 | +pip install -r requirements.txt |
| 33 | +``` |
24 | 34 |
|
25 | | -- Python 3.7+ |
26 | | -- Pip |
27 | | -- Other listed in requirements.txt |
| 35 | +3. Set environment variables: |
28 | 36 |
|
29 | | -## Running |
| 37 | +| Key | Value | |
| 38 | +| --- | --- | |
| 39 | +| `DATABASE_URL` | `postgresql://user:password@host:port/db` | |
30 | 40 |
|
31 | | -- Clone the repo using |
| 41 | +Example (`.env`): |
32 | 42 |
|
33 | | -```bash |
34 | | -git clone https://github.com/mdhishaamakhtar/fastapi-sqlalchemy-postgres-template |
| 43 | +```env |
| 44 | +DATABASE_URL=postgresql://postgres:postgres@localhost:5432/fastapi_template |
35 | 45 | ``` |
36 | 46 |
|
37 | | -- Create a Virtual Environment using |
| 47 | +4. Run the API: |
38 | 48 |
|
39 | 49 | ```bash |
40 | | -sudo pip install virtualenv |
41 | | -virtualenv env |
| 50 | +uvicorn main:app --reload |
42 | 51 | ``` |
43 | 52 |
|
44 | | -- Activate the virtualenv |
| 53 | +## API Docs (Swagger / OpenAPI) |
| 54 | +Once the app is running locally, open: |
| 55 | +- Swagger UI: `http://127.0.0.1:8000/docs` |
| 56 | +- ReDoc: `http://127.0.0.1:8000/redoc` |
| 57 | +- OpenAPI JSON: `http://127.0.0.1:8000/openapi.json` |
45 | 58 |
|
| 59 | +## Running tests |
46 | 60 | ```bash |
47 | | -env\Scripts\activate # for windows |
48 | | -source env/bin/activate # for linux and mac |
| 61 | +pytest |
49 | 62 | ``` |
50 | 63 |
|
51 | | -- Install dependencies |
| 64 | +## Formatting (Black) |
| 65 | +Run formatter: |
52 | 66 |
|
53 | 67 | ```bash |
54 | | -pip install -r requirements.txt |
| 68 | +black . |
55 | 69 | ``` |
56 | 70 |
|
57 | | -- Setting up environment variables |
| 71 | +## Docker (Local) |
| 72 | +1. Create `.env` from example: |
58 | 73 |
|
59 | | -| Key | Value | |
60 | | -| ----------- | ----------- | |
61 | | -| DATABASE_URL | postgresql://user:password@host:port/db| |
| 74 | +```bash |
| 75 | +cp .env.example .env |
| 76 | +``` |
62 | 77 |
|
63 | | -- To run the project |
| 78 | +2. Start API + Postgres with Docker Compose: |
64 | 79 |
|
65 | 80 | ```bash |
66 | | -uvicorn main:app |
| 81 | +docker compose up --build |
67 | 82 | ``` |
68 | 83 |
|
69 | | -## Contributors |
70 | | - |
71 | | -<table> |
72 | | - <tr align="center"> |
73 | | - <td> |
74 | | - Md Hishaam Akhtar |
75 | | - <p align="center"> |
76 | | - <img src = "https://user-images.githubusercontent.com/58990970/103586688-9cde9700-4f0b-11eb-915c-0d8b9a555159.JPG" width="150" height="150" alt="Md Hishaam Akhtar"> |
77 | | - </p> |
78 | | - <p align="center"> |
79 | | - <a href = "https://github.com/mdhishaamakhtar"> |
80 | | - <img src = "https://www.iconninja.com/files/241/825/211/round-collaboration-social-github-code-circle-network-icon.svg" width="36" height = "36" alt="GitHub"/> |
81 | | - </a> |
82 | | - <a href = "https://www.linkedin.com/in/mdhishaamakhtar"> |
83 | | - <img src = "https://www.iconninja.com/files/863/607/751/network-linkedin-social-connection-circular-circle-media-icon.svg" width="36" height="36" alt="LinkedIn"/> |
84 | | - </a> |
85 | | - </p> |
86 | | - </td> |
87 | | - </tr> |
88 | | -</table> |
| 84 | +3. API will be available at: |
| 85 | +- `http://127.0.0.1:8000` |
| 86 | +- Swagger: `http://127.0.0.1:8000/docs` |
| 87 | +- ReDoc: `http://127.0.0.1:8000/redoc` |
| 88 | + |
| 89 | +`DATABASE_URL` is passed to the API container from `.env` through `docker-compose.yml`. |
0 commit comments