Sanic in Practice
This project is the reference source code of online video course 叽歪课堂 - Python Sanic 高并发服务开发实战, including Sanic web framework usage examples, SQLAlchemy sql toolkit usage examples, and a lite version of Weiguan app's api server.
The latest code was refacted with dependency injection and clean architecture, so the project structure has changed a lot. You can view the commit history and checkout the previous commit to get the traditional project structure.
Frameworks and libraries used by this project
- Sanic Asynchronous web framework and server
- SQLAlchemy SQL toolkit and orm framework
- AIOMySQL Asynchronous access mysql
- AIORedis Asynchronous access redis
- MarshMallow Object serialization
- Fire CLI application framework
- APScheduler Run interval jobs
- Dependency Injector Dependency injection microframework for Python
How to run
This project need python v3.7+.
By python virtual environment
Prapare python virtual environment
git clone https://github.com/jaggerwang/sanic-in-practice.git && cd sanic-in-practice python3 -m venv venv source venv/bin/activate pip install -r requirements.txt
You can quit the virtual environment by execute command
Prepare mysql and redis service
Install mysql and redis server, and start them. After mysql started, create a database for this project, and an account to access the created database.
CREATE DATABASE `weiguan` DEFAULT CHARACTER SET 'utf8mb4'; GRANT ALL PRIVILEGES ON `weiguan`.* TO 'weiguan'@'%' IDENTIFIED BY 'jwcourse.com';
Change configs in
weiguan/config/base.py as your need, especially mysql, redis and path related configs. You can also change configs by environment variables, you need add
WG_ prefix to each config you want to change.
python -u -m weiguan.manage model create-tables
python -u -m weiguan.app
python -u -m weiguan.manage schedule run
The api service's endpoint is at
By docker compose
Change the content of
docker-compose.yml as needed, especially the host path of mounted volumes.
Start all services
It will start server, scheduler, mysql and redis services. If you need to stop and remove all services, you can execute command
docker-compose down. The server and scheduler services will start failed before creating the project's database, you should run
docker-compose up again after created the database.
Create database and tables
First, login to mysql container and create a database for this project. The password of mysql
root account is
$ docker container exec -it sanic-in-practice_mysql_1 bash $ mysql -u root -p > CREATE DATABASE `weiguan` DEFAULT CHARACTER SET 'utf8mb4'; > GRANT ALL PRIVILEGES ON `weiguan`.* TO 'weiguan'@'%' IDENTIFIED BY 'jwcourse.com';
Second, login to server container and create tables for this project.
docker container exec -it sanic-in-practice_server_1 bash python -u -m weiguan.manage model create-tables
Then you can access all apis at endpoint
Developing in vscode's remote container
This project support developing in VSCode's remote container, you can create your own development environment in just one click. You need install vscode and it's extension "Remote - Containers" at first.
Configure remote container
.devcontainer/devcontainer.json as your need, especially the host path of mount volumes. The file
docker-compose.dev.yml will override some configs of the base file
docker-compose.yml for better developing experience. Such as auto build application image, mount local project folder into container, etc. It also mount host's
~/.ssh folder into container for executing git command in container.
Open project in remote container
Click remote development button at bottom-left corner of vscode, or open command palette, then execute command
Remote-Containers: Reopen Folder in Container. It'll build application image, and start a server container, including it's dependency containers, as this project's development environment.
The workspace folder of the remote project is at
/workspace, which mounted the local project's folder. You can now run and restart the remote project as nomal local project. If you need execute some command, you can login to the server container or use
docker container exec.
The left steps is the same as run application by docker compose, such as create database and tables, access apis, etc.