Skip to content
Reference code of online video course "sanic in practice".
Python Other
  1. Python 98.7%
  2. Other 1.3%
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.devcontainer run application by docker compose Jul 29, 2019
docker-app-deploy deploy app by docker Aug 3, 2019
sanic-web-framework worked with weiguan demo app Aug 21, 2019
sqlalchemy-sql-toolkit add sqlalchemy examples Jul 29, 2019
weiguan
.dockerignore
.gitignore add sqlalchemy examples Jul 29, 2019
Dockerfile run application by docker compose Jul 29, 2019
README.md refactoring with dependency injection and clean architecture Aug 23, 2019
docker-compose.dev.yml deploy app by docker Aug 3, 2019
docker-compose.yml deploy app by docker Aug 3, 2019
pip.conf run application by docker compose Jul 29, 2019
requirements.txt refactoring with dependency injection and clean architecture Aug 23, 2019
sources.list run application by docker compose Jul 29, 2019

README.md

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

  1. Sanic Asynchronous web framework and server
  2. SQLAlchemy SQL toolkit and orm framework
  3. AIOMySQL Asynchronous access mysql
  4. AIORedis Asynchronous access redis
  5. MarshMallow Object serialization
  6. Fire CLI application framework
  7. APScheduler Run interval jobs
  8. 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 deactivate.

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';

Configure application

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.

Create tables

python -u -m weiguan.manage model create-tables

Start server

python -u -m weiguan.app

Start scheduler

python -u -m weiguan.manage schedule run

The api service's endpoint is at http://localhost:8000/.

By docker compose

You need install Docker and Docker Compose at first.

Configure compose

Change the content of docker-compose.yml as needed, especially the host path of mounted volumes.

Start all services

docker-compose up

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 jwcourse.com.

$ 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 http://localhost:8001/.

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

Configure docker-compose.dev.yml and .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.

You can’t perform that action at this time.