Skip to main content

Flask-based web framework, including orm with connection pool

Project description

FlaskCap是一个基于flask封装的Web框架,集成了orator和DBUtils来提供数据库ORM及连接池的支持,并包含了一些工具类和易于使用的特性。

安装

使用pip安装和更新flaskcap:

pip install flaskcap

示例

flaskcap

  • flaskcap 的使用与flask一致。
from flaskcap import FlaskCap
from flaskcap import jsonify

app = FlaskCap(__name__)

@app.route('/foo')
def foo():
    return 'Hello World'

@app.route('/bar')
def bar():
    return jsonify({'hello': 'world'})

if __name__ == '__main__':
    app.run()
  • 通过 g 获取请求参数
flaskcap 将HTTP请求参数(文件参数除外)封装在 `g.request_payload` 中,包含表单及JSON参数。
from flaskcap import g, jsonify

@app.route('/', methods=['GET', 'POST'])
def index():
    return jsonify(g.request_payload)
  • 日志
flaskcap 提供一个跨平台的可按时间进行日志分割的实现,`TimedRotatingLogging`。
该实现是进程安全的,并且支持日志文件丢失后自动重建。
from flaskcap import TimedRotatingLogging

logger_handler = TimedRotatingLogging('app.log', backupCount=7)
  • 访问日志
flaskcap 提供了请求日志处理的注册方法,并提供了 `elapsed_time` 值记录请求处理时间
from flaskcap import current_app

app.access_logger.addHandler(NaturalTimedRotating('access.log'))

@app.log_access
def access_func(request, response):
    current_app.access_logger.info('Request takes %s seconds' % current_app.elapsed_time)

orm

  • orm的使用与orator一致,请参见orator的使用。
from flaskcap import FlaskCap
from flaskcap.orator import Orator

app = FlaskCap(__name__)

app.config['DATABASE'] = {
    'mysql': {
        'driver': 'mysql',
        'host': 'localhost',
        'database': 'db',
        'user': 'user',
        'password': 'password',
        # 是否开启慢查询日志,默认关闭
        'log_slow_query': True,
        # 慢查询时间阀值(毫秒),默认2000
        'slow_query_time': 3000,
        # 连接池策略,即DBUtils的连接池方案,包含'PersistentDB'和'PooledDB'两种,
        # 对应配置值为'persistent'和'pooled',默认为'pooled'
        'pool_policy': 'persistent',
        # 其它连接池参数,参见DBUtils
        # 'maxcached': 4,
        # 'maxusage': 10,
        # ...
    }
}

db = Orator(app)
# or
db = Orator()
db.init_app(app)

# 查询
users = db.table('users').all()

# 定义Model
class User(db.Model):
    pass

users = User.all()
  • 配置数据库慢查询日志。
import logging

from flaskcap.logging import TimedRotatingLogging

# 定义慢查询日志
slow_query_logger = logging.getLogger('orator.slow_query')
log_handler = TimedRotatingLogging('slow_query.log', backupCount=7)
slow_query_logger.addHandler(log_handler)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
flaskcap-0.1.9.tar.gz (11.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page