An open API service indexing awesome lists of open source software.

https://github.com/guduchango/sanic-python-example

sanic framework, migration, seed, docker-compose
https://github.com/guduchango/sanic-python-example

docker docker-compose mysql python sanic-framework

Last synced: 2 months ago
JSON representation

sanic framework, migration, seed, docker-compose

Awesome Lists containing this project

README

          

# πŸš€ Sanic CRUD API Example

A simple CRUD API built with [Sanic](https://sanic.dev/) for practicing the basics of building high-performance web APIs in Python. This project includes a modular structure with controllers, models, database seeding, and runs easily with Docker.

Ideal for developers looking to explore Sanic’s asynchronous capabilities, request handling, and best practices for structuring a web API.

---

## πŸ“¦ Features

βœ… Fully asynchronous API with Sanic
βœ… CRUD endpoints for a single entity (`Item`)
βœ… Modular structure with controllers and models
βœ… Database migration and seed scripts
βœ… Docker & Docker Compose support for easy setup
βœ… Ready-to-use environment for experimenting with Sanic

---

## πŸ—‚οΈ Project Structure

.
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ controllers/ # Route handlers (controllers)
β”‚ β”œβ”€β”€ models/ # Database models
β”‚ β”œβ”€β”€ seeds/ # Seed scripts for database population
β”‚ └── main.py # Entry point for the Sanic app
β”œβ”€β”€ database/
β”‚ β”œβ”€β”€ migrate.py # Migration script
β”‚ └── seed.py # Seed script
β”œβ”€β”€ Dockerfile # Docker image definition
β”œβ”€β”€ docker-compose.yml # Docker Compose services configuration
β”œβ”€β”€ requirements.txt # Python dependencies
└── README.md # Project documentation

---

## πŸš€ Getting Started

### Clone the repository

```bash
git clone https://github.com/guduchango/sanic-python-example.git
cd sanic-python-example
```

---

## 🐳 Running with Docker

Build the Docker image:

```bash
docker-compose build
```

Start the containers:

```bash
docker-compose up
```

---

## πŸ› οΈ Database Migrations & Seeding

Run database migrations:

```bash
docker-compose run web python database/migrate.py
```

Seed the database with initial data:

```bash
docker-compose run web python database/seed.py
```

---

## πŸ“š API Endpoints

Once the app is running on **http://localhost:8000**, the following CRUD routes are available:

| Method | Route | Description |
|--------|-----------------------|---------------------------|
| GET | `/` | Health check/test route |
| GET | `/items` | List all items |
| GET | `/items/` | Retrieve an item by ID |
| POST | `/items` | Create a new item |
| PUT | `/items/` | Update an existing item |
| DELETE | `/items/` | Delete an existing item |

---

## βœ… Example Requests with curl

**List all items**
```bash
curl -X GET http://localhost:8000/items
```

**Get a specific item**
```bash
curl -X GET http://localhost:8000/items/1
```

**Create a new item**
```bash
curl -X POST http://localhost:8000/items -H "Content-Type: application/json" -d '{"name": "New Item", "description": "A new item added to the database"}'
```

**Update an item**
```bash
curl -X PUT http://localhost:8000/items/1 -H "Content-Type: application/json" -d '{"name": "Updated Item", "description": "Updated description"}'
```

**Delete an item**
```bash
curl -X DELETE http://localhost:8000/items/1
```

---

## πŸ”— Access the API

Once running, your API will be available at:

```
http://localhost:8000
```

---

## πŸ“ Prerequisites

- Docker
- Docker Compose

---

## πŸ“„ License

MIT License.

---

## πŸ™Œ Contributing

Contributions are welcome! Feel free to fork the repo and submit a pull request.