Skip to content

Commit

Permalink
Update: mongodb backup #33
Browse files Browse the repository at this point in the history
  • Loading branch information
howie6879 committed Jan 21, 2022
1 parent 2c3c564 commit b288563
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/api/http_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"""
Created by howie.hu at 2021/4/10.
Description:HTTP API 服务
- 启动命令: PIPENV_DOTENV_LOCATION=./pro.env pipenv run gunicorn -c src/config/gunicorn.py src.api.http_app:app
Changelog: all notable changes to this file will be documented
"""
from flask import Flask

from src.api.views import bp_api, bp_rss
from src.api.views import bp_api, bp_backup, bp_rss
from src.config import Config
from src.databases import MongodbManager
from src.utils.log import get_logger
Expand Down Expand Up @@ -34,12 +35,12 @@ def create_app():

flask_app.register_blueprint(bp_api)
flask_app.register_blueprint(bp_rss)
flask_app.register_blueprint(bp_backup)
return flask_app


app = create_app()


if __name__ == "__main__":
# PIPENV_DOTENV_LOCATION=./pro.env pipenv run gunicorn -c src/config/gunicorn.py src.api.http_app:app
app.run(host=Config.HOST, port=Config.HTTP_PORT, debug=Config.DEBUG)
1 change: 1 addition & 0 deletions src/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
"""

from .bp_api_v1 import bp_api
from .bp_backup import bp_backup
from .bp_rss import bp_rss
63 changes: 63 additions & 0 deletions src/api/views/bp_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Created by howie.hu at 2021-12-27.
Description: liuli backup html 接口
Changelog: all notable changes to this file will be documented
"""

from flask import Blueprint, current_app

from src.databases.mongodb_base import MongodbBase
from src.databases.mongodb_tools import mongodb_find

bp_backup = Blueprint("backup", __name__, url_prefix="/backup")


@bp_backup.route(
"/<doc_source>/<doc_source_name>/<doc_name>",
methods=["GET"],
strict_slashes=False,
)
def backup(doc_source, doc_source_name, doc_name):
"""
示例接口
:return:
"""
# 获取基本配置
mongodb_base: MongodbBase = current_app.config["mongodb_base"]
logger = current_app.config["app_logger"]

# 获取变量
file_path = f"{doc_source}/{doc_source_name}/{doc_name}"
coll_rss_conn = mongodb_base.get_collection(coll_name="liuli_backup")
filter_dict = {
"doc_source": doc_source,
"doc_source_name": doc_source_name,
"doc_name": doc_name,
}
db_res = mongodb_find(
coll_conn=coll_rss_conn,
filter_dict=filter_dict,
return_dict={"_id": 0},
limit=1,
)
db_satus, db_info = db_res["status"], db_res["info"]
content = ""
if db_satus:
# 查询成功
if db_info:
# 存在
content = db_info[0]["content"]
else:
# 不存在 rss
msg = f"{file_path} 不存在,请先录入!"
logger.error(msg)
else:
# 查询失败
msg = f"{file_path} 查询失败!"
logger.error(msg)

return (
content,
200,
{"Content-Type": "text/html; charset=utf-8"},
)
7 changes: 4 additions & 3 deletions src/backup/backup_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ def backup_factory(backup_type: str, backup_config: dict) -> BackupBase:
"doc_link": "https://mp.weixin.qq.com/s/NKnTiLixjB9h8fSd7Gq8lw",
}

github_backup = backup_factory(backup_type="github", backup_config={})
backup = backup_factory(backup_type="mongodb", backup_config={})
# backup = backup_factory(backup_type="github", backup_config={})

github_backup.delete(
backup.delete(
doc_source="liuli_wechat",
doc_source_name="老胡的储物柜",
doc_name="打造一个干净且个性化的公众号阅读环境",
)

github_backup.backup(test_backup_data)
backup.backup(test_backup_data)
4 changes: 2 additions & 2 deletions src/backup/mongodb_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from src.utils import LOGGER


class MongoBackup(BackupBase):
class MongodbBackup(BackupBase):
"""基于MongoDB进行文章备份"""

def __init__(self, backup_config: dict):
Expand Down Expand Up @@ -142,7 +142,7 @@ def delete(self, doc_source: str, doc_source_name: str, doc_name: str) -> bool:
"doc_name": "打造一个干净且个性化的公众号阅读环境",
"doc_link": "https://mp.weixin.qq.com/s/NKnTiLixjB9h8fSd7Gq8lw",
}
mongo_backup = MongoBackup({})
mongo_backup = MongodbBackup({})
mongo_backup.backup(test_backup_data)
# mongo_backup.delete(
# doc_source="liuli_wechat",
Expand Down

0 comments on commit b288563

Please sign in to comment.