This repositorie is the template of flask API. You can clone the template then start your project. The structure is pretty simple. You can use any database you want, you just need to rewrite the db_handling.py
file.
We require python3.6 or higher version and sqlite3. Please install python3, pip3 and sqlite3 before set up the template.9
-
If you want to use the virtual environment (venv) you run those command:
sudo apt install python3-venv
-
Create a new venv in the current directory:
python3 -m venv venv
-
Active venv:
source venv/bin/activate
-
Install python3 package:
pip3 install -r requirements.txt
-
Deactivate the venv:
deactivate
-
Run the project:
Before run the template, making sure you install the API of connecting the database. Otherwise it will throw an error
python3 run.py
We already solve this issue, just learn about that.
The first method is config the app after request.
# Configuring cors requests
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization,session_id')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS,HEAD')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
The second method is using the packageflask_cors
(used in template):
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
.
├── README.md -- The README files
├── api -- Directory which contain all the API files
│ └── auth.py -- The demo file
├── app -- The init directory
│ └── __init__.py
├── run.py -- The deploy file
└── utils -- Directory which contain all the helper functions
├── crypt.py -- decrypt or encrypt functions
├── db_handling.py -- handle the sql query
├── init_db.sql -- init the database file
└── request_handling.py -- handle the request (get args, header and files)