Skip to content
A usage example of python sanic web framework, aiomysql, aioredis, fire, apscheduler etc.
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.
weiguan
.dockerignore
.gitignore
Dockerfile
README.md
docker-compose.yml
requirements.txt

README.md

Weiguan(围观) Server

This project is an open source version of weiguan app's server side code that is available to interested parties for learning and research.

Main technical choice

This project was developed in Python. To support high concurrency, the asynchronous io web framework Sanic is used. Reading and writing the backend services, such as MySQL and Redis, are also asynchronous. It also uses the Google Fire command line application framework to write administrative commands and APScheduler to run asynchronous background jobs.

  1. Sanic Web framework and server
  2. AIOMySQL Access MySQL
  3. AIORedis Access Redis
  4. Fire CLI application framework
  5. APScheduler Run background jobs

Directory Structure

.
├── docker-compose.yml # Docker compose file
├── Dockerfile # Docker image building file
├── README.md
├── requirements.txt # PIP packages
├── venv # Python virtual env
└── weiguan # Python top package
    ├── __init__.py
    ├── app.py # Application entry
    ├── blueprints # Request handlers
    │   ├── __init__.py
    │   ├── account.py
    │   ├── common.py
    │   ├── message.py
    │   └── user.py
    ├── commands # Manage commands
    │   ├── __init__.py
    │   ├── model.py
    │   └── schedule.py
    ├── config # Configuration
    │   ├── __init__.py
    │   ├── base.py
    │   └── log.py
    ├── manage.py # Manage command entry
    ├── models # Tables mainly
    │   ├── __init__.py
    │   ├── common.py
    │   ├── file.py
    │   ├── message.py
    │   ├── post_image.py
    │   ├── post_like.py
    │   ├── post_stat.py
    │   ├── post.py
    │   ├── security.py
    │   ├── user_follow.py
    │   ├── user_stat.py
    │   └── user.py
    ├── services # Business logic
    │   ├── __init__.py
    │   ├── common.py
    │   ├── stat.py
    │   └── user.py
    └── utils # Utility functions
        ├── __init__.py
        ├── datetime.py
        └── string.py

Deployment steps

Install Python and PIP dependencies

The Python version requires 3.7+, and it is recommended to use a virtual environment for deployment to avoid conflicts with other projects.

After cloning the code to the local, go to the project root directory and execute the following command to create the virtual environment and install the project's PIP dependencies.

git clone https://github.com/jaggerwang/jw-weiguan-server.git && cd jw-weiguan-server
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

To exit the virtual environment, execute the deactivate command.

Configuration application

Some configurations depend on the environment, such as the connection address of MySQL and Redis, which needs to be modified to the value of the current environment when starting the service or executing administrative commands. There are two ways to modify it, one is to directly modify the configuration file weiguan/config/base.py, and the other is through environment variables. Recommend the latter, so you don't have to modify the code. The environment variable name corresponding to each configuration item is preceded by the WG_ prefix and all uppercase. The default values of each configuration item and their uses are detailed in the comments in the configuration file.

Start service

Application services and background timing tasks need to be started, along with the MySQL and Redis services they depend on. To simplify deployment, it is recommended to use Docker for deployment. The production environment recommends turning off features such as debugging and hot loading for better performance.

Build application image

Application services and scheduled tasks can share a single image. By default, the application service is started. The scheduled task can be started by changing the start command when starting the container. The Docker image build file is Dockerfile, execute the following command in the project root directory to build the image.

docker build -t weiguan.app/server .

Start all services

Use Docker Compose to start all services at once. Docker automatically creates a network for the project and adds all the containers in the project to the network, so each container of the project can access each other. The network name defaults to the name of the current directory and can be modified with the -p or --project-name options. The Docker Compose configuration file is docker-compose.yml, execute the following command in the project root directory to start all services.

docker-compose -p weiguan up

If you need to stop all services, you can execute docker-compose -p weiguan down.

Data for each container, such as application service logs, MySQL data and logs, is persisted to each subdirectory of the host's /data/weiguan directory via Docker Volume to avoid losing data after the container is recreated. If you want to persist to a different directory, you can modify the volumes attribute of each container in the Docker Compose configuration file.

Create database and tables

Before you make your first API call, you need to create an application database and its tables.

Create database and access account

First enter the MySQL container, use the root account (password jaggerwang) to connect to the database service; then create a database named weiguan, set the character set of the database to utf8mb4; finally create a ordinary account weiguan (password jaggerwang) to access the database.

docker container exec -it weiguan_mysql_1 bash
mysql -u root -p jaggerwang
> CREATE DATABASE `weiguan` CHARACTER SET 'utf8mb4';
> GRANT ALL PRIVILEGES ON `weiguan`.* TO 'weiguan'@% IDENTIFIED BY 'jaggerwang';

Note that the root account's password and the new account's password used here need to be the same as those in the previous Docker Compose configuration file.

Create tables

Go to the application service container and execute the following commands in the project root directory (/app) to create all the tables:

docker container exec -it weiguan_server_1 bash
python -u -m weiguan.manage model create_tables

Exposure service

After the startup, the access port of the application service defaults to 8000, which can also be modified through configuration items. Usually a reverse proxy is added before the application service, such as Nginx or Caddy, The external requests are forwarded to the backend application service. It is recommended to use Caddy, which supports automatic HTTPS, it use free certificates from Let’s Encrypt.

You can’t perform that action at this time.