forked from Rhilip/PT-help
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
30 lines (21 loc) · 893 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# !/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2020 Rhilip <[email protected]>
import pymysql
from flask import Flask
from flaskext.mysql import MySQL
from flask_cors import CORS
from flask_caching import Cache
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config')
app.config.from_pyfile('config.py')
class Database(MySQL):
def exec(self, sql: str, args=None, r_dict: bool = False, fetch_all: bool = False, ret_row: bool = False):
db = self.get_db()
cursor = db.cursor(pymysql.cursors.DictCursor) if r_dict else db.cursor() # Cursor type
row = cursor.execute(sql, args)
data = cursor.fetchall() if fetch_all else cursor.fetchone() # The lines of return info (one or all)
return (row, data) if ret_row else data
mysql = Database(app=app, autocommit=True)
cache = Cache(app)
CORS(app)