From 98314cbfe3ee8420b57a70cc71e614d9d5d3d2eb Mon Sep 17 00:00:00 2001 From: skimrme Date: Tue, 24 Mar 2026 14:24:08 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BF=AE=E6=94=B9=E9=9B=86?= =?UTF-8?q?=E4=BD=93=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 14 ++++++++++---- api/api.py | 41 ++++++++++++++++++++++++++++++++++++++++ api/docs.py | 14 ++++++++++++++ main.py | 13 ++++++++++--- templates/blog/home.html | 2 +- 5 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 api/api.py create mode 100644 api/docs.py diff --git a/.gitignore b/.gitignore index 9e26d9a..75569aa 100755 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,21 @@ +# python/cache **/__pycache__ +**/venv/* +**/.venv/* + +# databases **/*.db **/*.sql **/*.db.back -**/venv/* -**/.venv/* databases/* + +# flask folder mirrors/* blueprint/kami_views.py +blueprint/massage_views.py templates/kami/* +templates/message/* static/upload/* static/counter.txt static/message/* -templates/message/* -blueprint/massage_views.py +static/test/* \ No newline at end of file diff --git a/api/api.py b/api/api.py new file mode 100644 index 0000000..0319eef --- /dev/null +++ b/api/api.py @@ -0,0 +1,41 @@ +from flask import Blueprint +import json +import sqlite3 + +api_api = Blueprint('/api/', __name__) +json_title = {'Content-Type': 'application/json'} # 设定json的类型 + +# 引入数据库 +def get_owp_db_conn(): + conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/sqlite/owp.db') + conn.row_factory = sqlite3.Row + return conn + + +# 获取日志内容 +@api_api.get("/get/logs/") +def get_logs(): + # 获取并查询数据库日志内容 + conn = get_owp_db_conn() + sql_logs = "SELECT * FROM logs;" + logs = conn.execute(sql_logs).fetchall() + conn.close() + + data_logs = [ + dict(row) for row in logs + ] + return (json.dumps(data_logs, ensure_ascii=False), json_title) + + +@api_api.get("/get/posts/") +def get_posts(): + # 获取并查询数据库日志内容 + conn = get_owp_db_conn() + sql_posts = "SELECT * FROM posts WHERE status=1;" + posts = conn.execute(sql_posts).fetchall() + conn.close() + + data_posts = [ + dict(row) for row in posts + ] + return (json.dumps(data_posts, ensure_ascii=False), json_title) \ No newline at end of file diff --git a/api/docs.py b/api/docs.py new file mode 100644 index 0000000..8ace5d0 --- /dev/null +++ b/api/docs.py @@ -0,0 +1,14 @@ +from flask import Blueprint + +docs_api = Blueprint('/api/docs', __name__) + +@docs_api.get("/") +def home(): + return """ +

其实我最开始是想用fastapi的.....

..... +
+
+ 查询日志 +
+ https://ww3.tw/api/s/get/logs/ + """ \ No newline at end of file diff --git a/main.py b/main.py index c2c2b79..42597da 100755 --- a/main.py +++ b/main.py @@ -1,12 +1,16 @@ from flask import Flask, render_template, redirect, url_for, send_from_directory, make_response, g +import os +import sqlite3 +import secrets +# 引入蓝图 from blueprint.blog_views import blog_bp from blueprint.study_views import study_bp from flask_autoindex import AutoIndex from blueprint.kami_views import kami_bp from blueprint.index_views import index_bp -import os -import sqlite3 -import secrets +# 引入api +from api.docs import docs_api +from api.api import api_api def get_owp_db_conn(): conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/sqlite/owp.db') @@ -28,6 +32,9 @@ app.register_blueprint(study_bp, url_prefix='/study') app.register_blueprint(kami_bp, url_prefix='/kami') app.register_blueprint(index_bp, url_prefix='/') +# 注册api +app.register_blueprint(docs_api, url_prefix='/api/docs') +app.register_blueprint(api_api, url_prefix='/api/s/') diff --git a/templates/blog/home.html b/templates/blog/home.html index 760a612..6018842 100644 --- a/templates/blog/home.html +++ b/templates/blog/home.html @@ -20,7 +20,7 @@ {% for logs in logs %} - + {{ logs['date'].replace('.', '-') }}