慕慕手记项目日志 项目从开发到部署多环境配置 2025-3-8
慕慕手记项目日志 项目从开发到部署多环境配置 2025-3-8
现在是已经到了课程的第十章了,开始进行配置项目环境了。现在要完成的任务是项目可以正常运行,而且可以自由切换配置,开发/测试。
下面是当前的目录结构图:
现在来解释一下这些目录的作用:
-
app:这是项目的主目录,包含了整个应用的核心代码。
-
config
:配置文件夹,包含项目的配置信息。
- *init*.py:初始化文件,使这个目录成为一个包。
- config.py:配置文件,可能包含数据库连接信息、环境变量等。
- settings.py:设置文件,可能包含一些全局设置。
-
common
:通用模块,可能包含一些公共函数或类。
- *init*.py:初始化文件。
- database.py:数据库相关的操作,如ORM模型定义。
-
controller
:控制器模块,处理HTTP请求并返回响应。
- *init*.py:初始化文件。
- user.py:用户相关的控制器,处理与用户相关的请求。
-
model
:模型模块,定义数据模型。
- *init*.py:初始化文件。
- user.py:用户模型,定义用户相关的数据结构。
-
其中的config软件包下面的config.py文件里面定义了,配置的区别根据不同的情况返回对应的配置
class Config:
db_url = "mysql+pymysql://root:123456@172.27.13.88:3306/mumushouji"
class TestConfig(Config):
if_Echo = True
class ProductionConfig(Config):
if_Echo = False
# 根据环境变量选择不同的配置
config = {
"test": TestConfig,
"prod": ProductionConfig
}
然后settings.py文件内需要传入env
变量的类型如以下就是使用的测试环境配置
env="test"
app.py文件内定义了两个函数,分别是注册蓝图和返回app对象
from flask import Flask
def create_app():
app = Flask(__name__,template_folder="../templates",static_url_path="/",static_folder="../resource")
init_Blueprint(app) # 注册蓝本
return app
def init_Blueprint(app):
from controller.user import user
app.register_blueprint(user)
common软件包下的database.py文件内包含了数据库对象初始化的一些代码,和使用的环境
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from app.settings import env
from app.config.config import config
# \app\database.py
def db_connect():
config_class = config[env]()
engine = create_engine( config_class.db_url, echo=config_class.if_Echo)
session = sessionmaker(engine)
# 保证线程安全
db_session = scoped_session(session)
Base = declarative_base() # 创建一个基类
return db_session, Base, engine
接下来是控制器和模型类,这里定义了蓝图对象
from flask import Blueprint
from model.user import User
user = Blueprint('user', __name__)
@user.route('/')
def get_one():
user = User()
result = user.get_one()
print(result)
return "ok"
模型类里面暂时定义了User表的数据库对象还有db_session
和engine
以及基类Base
,但是我们这里用到的这三个对象都需要从db_connect函数内返回
from sqlalchemy import Table
from common.database import db_connect # 导入db_connect函数
db_session, Base, engine = db_connect()
# 定义表结构
class User(Base):
__table__ = Table('user', Base.metadata,autoload_with=engine)
def get_one(self):
return db_session.query(User).first()
日志的基本配置与应用
在该项目中,我们使用这样的日志配置。日志等级会随着env参数的改变而改变
import logging
from logging.handlers import RotatingFileHandler
from app.config.config import config
from app.settings import env
def set_log():
config_class = config[env]()
logging.basicConfig(level=config_class.LOG_LEVEL,)
file_log_handler = RotatingFileHandler("log/mumunote.log", maxBytes=1024 * 1024 * 300, backupCount=10)
#添加全局日志记录器
formater = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
file_log_handler.setFormatter(formater)
logging.getLogger().addHandler(file_log_handler)
set_log()
config_class = config[env]()
: 根据当前环境获取对应的配置类实例。logging.basicConfig(level=config_class.LOG_LEVEL,)
: 设置全局日志级别。这将影响所有未明确设置级别的日志记录器。file_log_handler = RotatingFileHandler(...)
: 创建一个RotatingFileHandler
实例,指定日志文件名、单个日志文件的最大大小(这里是300MB),以及保留旧日志文件的最大数量(这里是10个)。formater = logging.Formatter(...)
: 定义日志输出格式,包括时间戳、源文件名、行号、日志级别和消息内容。file_log_handler.setFormatter(formater)
: 将上面定义的格式应用于RotatingFileHandler
实例。logging.getLogger().addHandler(file_log_handler)
: 将file_log_handler
添加到根日志记录器中,这意味着所有的日志记录都会通过这个处理器输出到日志文件中,并遵循上述设定的规则
刚刚出现的一些问题
由于视频里面的那些源文件我没有,我只能从已有的项目代码里面拷过来一部分前端的代码进行使用。但是刚刚报错误了,提示少了东西
File "D:\code\mumunote\templates\index.html", line 1, in top-level template code
{% extends 'public/header.html' %}
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\code\mumunote\templates\public\header.html", line 135, in top-level template code
{% block content %}
File "D:\code\mumunote\templates\index.html", line 7, in block 'content'
{% for label_name,label_value in label_types.items() %}
就是这个错误。根据通义千问给我的反馈,我定义了些变量,然后前端就能正常加载了。
label_types = {
'type1': 'value1',
'type2': 'value2',
# 根据实际情况添加更多键值对...
}
当前状态:
附完整代码:
controllter/index.py
from flask import Blueprint, render_template
index = Blueprint('index',__name__)
@index.route('/')
def home():
label_types = {
'type1': 'value1',
'type2': 'value2',
# 根据实际情况添加更多键值对...
}
return render_template("index.html",label_types=label_types)
当前目录结构:
首页数据的填充 (1)
首页数据的填充,需要在在controller模块下面创建index.py,再实现home类中的find_article函数
from flask import Blueprint, render_template, request
from model.article import Article
index = Blueprint('index',__name__)
@index.route('/')
def home():
page = request.args.get("page")
article_type = request.args.get("article_type")
if page is None:
page = 1
if article_type is None:
article_type = 'recommend'
article = Article()
db_result = article.find_article(page,article_type)
label_types = {
'type1': 'value1',
'type2': 'value2',
# 根据实际情况添加更多键值对...
}
return render_template("index.html",label_types=label_types,result=db_result)
其中有一个很大的问题是没有label_types参数,页面就会503,暂时没有排查出来是哪里的问题,我们再来看模型类中的的Article类是怎么写的
from itertools import count
from sqlalchemy import Table
from common.database import db_connect
from app.settings import env
from app.config.config import config
from model.user import User
# model/article.py
db_session, Base, engine = db_connect()
# 定义表结构
class Article(Base):
__table__ = Table('article', Base.metadata,autoload_with=engine)
def find_article(self, page, article_type="recommend"):
# 一页显示多少内容呢,我们默认为一个10,page默认应该是从1开始
if int(page) < 1:
page = 1
count = int(page) * config[env].page_count
# 这就证明是来到了推荐的分类下边
if article_type == "recommend":
result = db_session.query(Article, User.nickname).join(
User, User.user_id == Article.user_id
).filter(
Article.drafted == 1
).order_by(
Article.browse_num.desc()
).limit(count).all()
else:
result = db_session.query(Article, User.nickname).join(
User, User.user_id == Article.user_id
).filter(
Article.label_name == article_type,
Article.drafted == 1
).order_by(
Article.browse_num.desc()
).limit(count).all()
return result
-
表结构定义:
__table__ = Table('article', Base.metadata, autoload_with=engine)
这行代码使用已有的数据库表
'article'
来动态加载表结构,而不是通过类属性来手动定义每个字段。autoload_with=engine
参数告诉 SQLAlchemy 使用提供的数据库连接(engine
)来反射表结构。 -
分页与查询逻辑:
- 方法
find_article(self, page, article_type="recommend")
被设计用来根据页面和文章类型检索文章。 page
参数表示当前请求的页数,默认从 1 开始。如果传入的page
小于 1,则将其设置为 1。count = int(page) * config[env].page_count
计算要获取的文章数量。这里假设在配置文件中已经定义了每页显示的文章数量page_count
。
- 方法
-
推荐文章查询:
- 如果
article_type
是"recommend"
,则会执行特定的查询来获取被标记为推荐的文章。这些文章还需要满足drafted == 1
的条件,这可能意味着它们是已发布的文章而不是草稿。 - 查询语句通过
db_session.query(Article, User.nickname).join(...)
实现,其中包含了与User
表的联结,以同时获取文章作者的昵称。 .filter(Article.drafted == 1)
添加过滤条件,确保只选择已发布的文章。.order_by(Article.browse_num.desc())
根据浏览次数对结果进行降序排序。.limit(count).all()
限制返回的结果数量并获取所有匹配项。
- 如果
-
按标签查询:
- 如果
article_type
不是"recommend"
,则根据提供的article_type
(即标签名)来筛选文章。查询逻辑与上述类似,但添加了额外的过滤条件Article.label_name == article_type
来筛选特定标签的文章。
- 如果