As a little programmer in my programming field, I am currently working as team lead in an entrepreneurial team. The technology stack involves Android, Python, Java and Go, which is also the main technology stack of our team. Github: github.com/hylinux1024 wechat official account: angryCode

Before, I read the source code of Python WEB framework Flask, and got a macro understanding of service startup process, routing principle and template rendering. But with that said, let’s develop an enterprise-class API application using Flask.

As an example, I chose an enterprise application that the team recently developed. This is a dating application, which was originally developed by using Java’s SpringBoot framework, but IN order to avoid unnecessary trouble, I will use Flask for transformation. Of course, I will simplify this case and focus on the technology and tool library involved while maintaining the core business. Restore the complete process of project development to the maximum extent.

0 x00 technology stack

Here we use Python version 3.7, the WEB framework is of course Flask, the database uses MySql, ORM uses SqlAlchemy, Redis is used as cache, and perhaps marshmallow, the serialization tool library.

The development environment is venv, while the deployment environment is nginx+ Gunicorn + Supervisord

So the entire technology stack is

# Development technology stackPython3.7 + venv + Flask + MySql + SqlAlchemy + Redis + marshmallowDeploy the technology stackPython3.7 + venv + nginx + gunicorn + supervisordCopy the code

Of course, the actual development of enterprises also need to write interface documents for the interaction of students at each end. We can use Postman or Taobao’S API document service.

0x01 Project Design

After the technical selection is well done, do not rush to write code first, but to do a good job in the early stage of the project design, according to the business needs to clarify the functional modules, database table structure, interface documents, etc.

Our demand is to make a dating application, so its main functional modules should have

  • Login and registration Use the user’s mobile phone number to log in and register
  • User list After a user logs in, the user can view the recommended users
  • Contact list Users that have been contacted are displayed in the contact list
  • The chat module sends messages to users, including text and voice messages
  • Nearby people View nearby people based on the geographical location where the user logs in
  • Who has seen me

    Check who’s watching me. This could beVIPfunction
  • Personal information includes basic user information, user photo albums and user labels
  • VIP module

    When the user recharge isVIPThen you can unlock some functions, such as viewingWho’s seen my listEtc.

Note that we focus on the front end of the project to avoid long lead timesapiThe development of the background management function is temporarily not considered.

According to these functional modules, we abstract the entities in the project mainly as follows

  • Login authorizationuser_auth
  • Basic User Informationuser_info
  • User’s locationlocation
  • User photo albumuser_album
  • User labeluser_label
  • The labellabel
  • The contactcontacts
  • The messagemessage
  • Visit the footprintvisitor
  • top-upVIPThe goodsproduct

    A monthlyVIP, quarterlyVIPAnd the annualVIPThree kinds of
  • The orderuser_order
  • The userVIPinformationvip_info

These entities correspond to their respective tables in database modeling. To avoid too much code, I will not post the script code for each table here. The SQL table structure is described in the project address below.

0 x02 database

I use the Tencent cloud database here, of course, it is possible to use the local database.

The fields of each table are shown below

Notice I don’t have a foreign key constraint on any of these tables.

0x03 Project framework Building

Using PyCharm as the IDE for the development environment, I created a project called DatingToday, structured as follows

(Venv) ➜ DatingToday tree-l1.├ ── app.py ├── DatingToday.sql ├── passwords.txt ├── ├─ ├─Copy the code

Notice that I have put the database script file in the project root directory. The venv environment has the following dependency libraries installed

(venv) ➜ DatingToday PIP list Package Version -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Click7.0    
Flask                  1.11.  
flask-marshmallow      0.101. 
Flask-SQLAlchemy       2.4. 0  
itsdangerous           1.1. 0  
Jinja2                 2.101. 
MarkupSafe             1.11.  
marshmallow            2.19. 5 
marshmallow-sqlalchemy 0.17. 0 
pip                    10.01. 
setuptools             39.1. 0 
six                    1.12. 0 
SQLAlchemy             1.36.  
Werkzeug               0.15. 5 
Copy the code

You can use the command

(venv) ➜ pip freeze > requirements.txt
Copy the code

Generate the requirements.tx file.

Using the command

(venv) ➜ pip install -r requirements.txt
Copy the code

Restore dependencies in a virtual environment.

0 x04 summary

This is the first article of the development enterprise API application based on the Flask is mainly to the early stage of the project development of preparatory work, including project design, database design, and project structure building, of course, the actual work may also be the first in the API documentation, let the front of classmates can start, but I’m here because it’s already in the document, So the API documentation is omitted. Sharpening the knife is not wrong to cut wood workers, these jobs are necessary.

0x05 Project address

Github.com/hylinux1024…

0x06 Learning Materials

  • palletsprojects.com/p/flask/
  • Realpython.com/flask-conne…