从本片文章开始我们就正式进入我们Presto数据查询项目的开发了,我们首先要基于Flask搭建我们的项目框架,然后逐步的完善我们的代码。
首先我们先来看一下我们的项目模块,如图所示:
其中config.py
是配置模块,manage.py
是管理模块。而app
是我们项目模块。在app
模块中,auth
为用户及权限管理模块,core
为公用的核心代码模块,queryserver
为数据查询模块。而static
和templates
则是静态文件和页面模板。
整体的结构已经非常清晰了,接下来我们就先来看一下我们config模块和manage模块中有哪些内容。我会在代码中尽量详细的加入注释,大家可以先通过代码进行学习。
import os
import uuid
import logging
# 获取文件目录
basedir = os.path.abspath(os.path.dirname(__file__))
# log级别过滤
class InfoFilter(logging.Filter):
def filter(self, record):
"""only use INFO
筛选, 只需要 INFO 级别的log
:param record:
:return:
"""
if logging.INFO <= record.levelno < logging.ERROR:
# 已经是INFO级别了
# 然后利用父类, 返回 1
return super().filter(record)
else:
return 0
# 配置类基类
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or str(uuid.uuid4())
SSL_DISABLE = False
SQLALCHEMY_RECORD_QUERIES = True
LOG_PATH = os.path.join(basedir, 'logs')
LOG_PATH_ERROR = os.path.join(LOG_PATH, 'error.log')
LOG_PATH_INFO = os.path.join(LOG_PATH, 'info.log')
LOG_FILE_MAX_BYTES = 5 * 1024 * 1024
LOG_FILE_BACKUP_COUNT = 5
@staticmethod
def init_app(app):
pass
# 开发模式配置类
class DevConfig(Config):
DEBUG = True
PRESERVE_CONTEXT_ON_EXCEPTION = False
# 是否开启csrf认证
WTF_CSRF_ENABLED = False
# 数据库链接地址
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://imooc:123456@localhost:3306/sqlprotest"
# presto链接地址
PRESTO_URI = "presto://127.0.0.1:7670"
PRESTO_HTTP_URI = "http://127.0.0.1:7670"
# redis相关配置
REDIS_HOST = "localhost"
REDIS_PORT = 6379
SQLALCHEMY_TRACK_MODIFICATIONS = True
@classmethod
def init_app(cls, app):
Config.init_app(app)
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
# 生产模式配置类
class ProConfig(Config):
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:123456@127.0.0.1:3306/sqlpro"
PRESTO_URI = "presto://localhost:8989"
PRESTO_HTTP_URI = "http://localhost:8989"
REDIS_HOST = "localhost"
REDIS_PORT = 6379
@classmethod
def init_app(cls, app):
Config.init_app(app)
import logging
from logging.handlers import RotatingFileHandler
formatter = logging.Formatter(
'%(asctime)s %(levelname)s %(process)d %(thread)d '
'%(pathname)s %(lineno)s %(message)s')
file_handler_info = RotatingFileHandler(filename=cls.LOG_PATH_INFO)
file_handler_info.setFormatter(formatter)
file_handler_info.setLevel(logging.INFO)
info_filter = InfoFilter()
file_handler_info.addFilter(info_filter)
app.logger.addHandler(file_handler_info)
file_handler_error = RotatingFileHandler(filename=cls.LOG_PATH_ERROR)
file_handler_error.setFormatter(formatter)
file_handler_error.setLevel(logging.ERROR)
app.logger.addHandler(file_handler_error)
config = {
'development': DevConfig,
'production': ProConfig,
'default': DevConfig
}
import os
from app import app
# 引入flask第三方扩展
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import db
from app.auth.models import User
# 初始化数据库迁移模块
migrate = Migrate(app, db)
# 初始化命令管理模块
manager = Manager(app)
# 添加数据库迁移命令
manager.add_command('db', MigrateCommand)
# 添加添加用户命令
@manager.command
def adduser(email, username, admin=False):
"""Register a new user."""
from getpass import getpass
password = getpass()
password2 = getpass(prompt='Confirm: ')
if password != password2:
import sys
sys.exit('Error: passwords do not match.')
db.create_all()
user = User(username=username, password=password)
db.session.add(user)
db.session.commit()
print('User {0} was registered successfully.'.format(username))
# 添加系统启动命令
@manager.command
def runserver():
app.run()
if __name__ == '__main__':
manager.run()
在manage.py中我们引入了部分app模块中的类,比如我们定义的User等等。 大家对这里先不要有疑问,我们继续向下看。