From d306188caf3ee4af9f93783e2ba6b52defb75765 Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Fri, 15 Apr 2022 20:47:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86=E7=BB=86?= =?UTF-8?q?=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main/views.py | 18 +- app/templates/list.html | 32 -- app/utils/{view.py => response.py} | 14 +- docs/README.md | 458 ++++++++++++++--------------- docs/_coverpage.md | 24 +- 5 files changed, 245 insertions(+), 301 deletions(-) delete mode 100644 app/templates/list.html rename app/utils/{view.py => response.py} (90%) diff --git a/app/main/views.py b/app/main/views.py index af75cc5..f33187a 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -11,8 +11,7 @@ from flask import request from flask import make_response from flask import render_template from ..db.db import SQLite as db -from ..utils.view import view_ -from ..utils.view import listing_ +from ..utils.response import index_ from . import main @@ -44,21 +43,8 @@ def home(name: str): db().update(name, 0) # 使用设置的长度减去已有数据的长度, 将结果转换为string类型, 再和数据库内的数据进行拼接 number = '0' * (length - len(str(data[-1]))) + str(data[-1]) - response = make_response(view_(theme, int(length), name, number)) + response = make_response(index_(theme, int(length), name, number)) response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8' response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate' return response return abort(500) - - -@main.route('/list/', methods=['GET', 'POST']) -def listing(): - """ - 列出所有数据库内已有的数据 - :return: - """ - data = db().exec('select * from reqcount;') - format_data = [] - for i in data: - format_data.append({'name': i[0], 'times': i[1]}) - return listing_(format_data) diff --git a/app/templates/list.html b/app/templates/list.html deleted file mode 100644 index c1447a8..0000000 --- a/app/templates/list.html +++ /dev/null @@ -1,32 +0,0 @@ - - - - - All data - - - -
-

- 数据库内已有数据 -

- - - - - - {#使用Jinja2模板来快速遍历传入的列表数据#} - {% for value in data %} - - - - - {% endfor %} -
- 名称 - - 次数 -
{{ value['name'] }}{{ value['times'] }}
-
- - \ No newline at end of file diff --git a/app/utils/view.py b/app/utils/response.py similarity index 90% rename from app/utils/view.py rename to app/utils/response.py index b3e120f..8dc157f 100644 --- a/app/utils/view.py +++ b/app/utils/response.py @@ -3,7 +3,7 @@ # @Author: markushammered@gmail.com # @Development Tool: PyCharm # @Create Time: 2022/3/25 -# @File Name: view.py +# @File Name: response.py from ..db.db import SQLite as db @@ -12,7 +12,7 @@ from flask import render_template from typing import Tuple -def view_(theme: str, length: int, name: str, count: str) -> str or Tuple[bool, str]: +def index_(theme: str, length: int, name: str, count: str) -> str or Tuple[bool, str]: """ 渲染模板 :param theme: @@ -77,13 +77,3 @@ def view_(theme: str, length: int, name: str, count: str) -> str or Tuple[bool, address=request.remote_addr, general_height=general_height, general_width=general_width) - - -def listing_(data: list): - """ - 展示数据库内所有的数据 - :param data: - :return: - """ - return render_template('list.html', - data=data) diff --git a/docs/README.md b/docs/README.md index 0072b45..8a76860 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,229 +1,229 @@ -# 这里是Request Counter的文档 - -> 虽然说项目很小根本不需要文档 - -# 灵感来源 - -[Moe-Counter](https://github.com/journey-ad/Moe-counter) - -# 资源来源 - -* 数据库内的`base64`图片来自`booru` -* 项目部分代码结构来自`miguelgrinberg`的书: `Falsk Web Development` - -# 工作原理 - -> Flask 建立服务器来接收请求并返回对应的响应体 - -# 部署 - -> 在部署之前你需要先安装`python3.10.x`以上的版本 - -## 部署到本地服务器 - - ```shell - $ git clone https://github.com/MarkusJoe/RequestCounter.git - $ cd RequestCounter - $ pip3 install -r requirements.txt - $ python3 app.py - ``` - -# 普通接口 - -## `/count/` 接口 - -> 此接口为基本接口请求此接口可以获取`svg矢量图`的响应体 - -> 必选参数: `name`: `str` 可选参数: `length`: `int` ; `theme`: `str` - -### 调用示例 - -```shell -$ curl -L -X https://requestcounter.herokuapp.com/count/main/ -``` - -```json -<...> -``` - -## `/list/` 接口 - -> 此接口为展示数据库内所有的数据的页面 - -> 无参数 - -### 调用示例 - -```shell -$ curl -L -X GET https://requestcounter.herokuapp.com/list/ -``` - -```json -<...> -``` - -# API - -* 注意事项 - -1. `/api/v1/`内的接口需要`key`来访问的有: `/export/` -2. `key`是从环境变量中获取或自动生成, 这取决与你部署到`Heroku`时是否设置了`ACCESS_KEY`环境变量 -3. `key`的长度为`32`位的随机英文字母大小写字符串 - -> 地址前缀: `/api/v1/` - -- 接口地址: `/overall/`, `/query/`, `/theme/` -- 请求方法: `GET` `POST` - -## `/overall/` 接口 - -> 使用此接口获取数据库中所有的数据 - -> 必选参数: `limit: int` > 自定义查询数量, 输入的数字大于等于最大值时, 将返回最大值 - -### 调用示例 - -```shell -$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/overall/?limit=20&key= -``` - -```json -{ - "code": 200, - "msg": "Success. Total: 7 row(s)", - "data": [ - { - "name": "sda", - "times": 72 - }, - { - "name": "main", - "times": 59 - } - ] -} -``` - -## `/query/` 接口 - -> 使用此接口获取数据库中指定的数据 - -***使用此接口查询的名称计数数值会加一*** - -> 必选参数: `name: str` > 查询指定名称的计数数据 - -> 可选参数: `nochange: Any` - -> `nochange`: 'Any` > 将参数值设置为任意值则仅查询不增加 - -### 调用示例 - -```shell -$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/query/?name=main&nochange=1&key= -``` - -```json -{ - "code": 200, - "msg": "Success", - "data": [ - "main", - 59 - ] -} -``` - -## `/theme/` 接口 - -> 使用此接口获取数据库内原始的base64编码的主题图片 - -> 必选参数: `theme: str` - -### 调用示例 - -```shell -$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/theme/?name=lewd&key= -``` - -```json - -{ - "code": 200, - "msg": "Success", - "data": [ - { - "index": "0", - "base64": "...", - "width": 45, - "height": 100 - }, - ... - ] -} -``` - -## `/alltables/` 接口 - -> 此接口可以获取数据库内所有的表名 > 返回的数据中不包含`ReqCount` - -> 无参数 - -```shell -$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/alltables/&key= -``` - -```json -{ - "code": 200, - "msg": "Success", - "data": [ - ... - ] -} -``` - -## `/export/` 接口 - -> 此接口可以导出应用的数据库文件 - -> 无参数 - -### 调用示例 - -```shell -$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/export/&key= -``` - -> 该接口请求成功后返回文件 - -## `/test`接口 - -> 此接口仅用于测试 只能增加`test-example`名称的计数次数 - -> 无需`key`即可请求 - -### 调用示例 - -```shell -$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/test -``` - -```json -622 -``` - -> 返回一个`int`数字 - -# 关于 - -## 开源 - -- 本项目以Apache-2.0许可开源, 即: - - 你可以直接使用该项目提供的功能, 无需任何授权 - - 你可以在**注明来源版权信息**的情况下对源代码进行任意分发和修改以及衍生 - -## 关于此页面 - -此文档由 [docsify](https://github.com/docsifyjs/docsify) 生成. docsify 是一个动态生成文档网站的工具。不同于 GitBook、Hexo 的地方是它不会生成将 .md 转成 .html -文件,所有转换工作都是在运行时进行。 - +# 这里是Request Counter的文档 + +> 虽然说项目很小根本不需要文档 + +# 灵感来源 + +[Moe-Counter](https://github.com/journey-ad/Moe-counter) + +# 资源来源 + +* 数据库内的`base64`图片来自`booru` +* 项目部分代码结构来自`miguelgrinberg`的书: `Falsk Web Development` + +# 工作原理 + +> Flask 建立服务器来接收请求并返回对应的响应体 + +# 部署 + +> 在部署之前你需要先安装`python3.10.x`以上的版本 + +## 部署到本地服务器 + + ```shell + $ git clone https://github.com/MarkusJoe/RequestCounter.git + $ cd RequestCounter + $ pip3 install -r requirements.txt + $ python3 app.py + ``` + +# 普通接口 + +## `/count/` 接口 + +> 此接口为基本接口请求此接口可以获取`svg矢量图`的响应体 + +> 必选参数: `name`: `str` 可选参数: `length`: `int` ; `theme`: `str` + +### 调用示例 + +```shell +$ curl -L -X https://requestcounter.herokuapp.com/count/main/ +``` + +```json +<...> +``` + +## `/list/` 接口 + +> 此接口为展示数据库内所有的数据的页面 + +> 无参数 + +### 调用示例 + +```shell +$ curl -L -X GET https://requestcounter.herokuapp.com/list/ +``` + +```json +<...> +``` + +# API + +* 注意事项 + +1. `/api/v1/`内的接口需要`key`来访问的有: `/export/` +2. `key`是从环境变量中获取或自动生成, 这取决与你部署到`Heroku`时是否设置了`ACCESS_KEY`环境变量 +3. `key`的长度为`32`位的随机英文字母大小写字符串 + +> 地址前缀: `/api/v1/` + +- 接口地址: `/overall/`, `/query/`, `/theme/` +- 请求方法: `GET` `POST` + +## `/overall/` 接口 + +> 使用此接口获取数据库中所有的数据 + +> 必选参数: `limit: int` > 自定义查询数量, 输入的数字大于等于最大值时, 将返回最大值 + +### 调用示例 + +```shell +$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/overall/?limit=20&key= +``` + +```json +{ + "code": 200, + "msg": "Success. Total: 7 row(s)", + "data": [ + { + "name": "sda", + "times": 72 + }, + { + "name": "main", + "times": 59 + } + ] +} +``` + +## `/query/` 接口 + +> 使用此接口获取数据库中指定的数据 + +***使用此接口查询的名称计数数值会加一*** + +> 必选参数: `name: str` > 查询指定名称的计数数据 + +> 可选参数: `nochange: Any` + +> `nochange`: 'Any` > 将参数值设置为任意值则仅查询不增加 + +### 调用示例 + +```shell +$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/query/?name=main&nochange=1&key= +``` + +```json +{ + "code": 200, + "msg": "Success", + "data": [ + "main", + 59 + ] +} +``` + +## `/theme/` 接口 + +> 使用此接口获取数据库内原始的base64编码的主题图片 + +> 必选参数: `theme: str` + +### 调用示例 + +```shell +$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/theme/?name=lewd&key= +``` + +```json + +{ + "code": 200, + "msg": "Success", + "data": [ + { + "index": "0", + "base64": "...", + "width": 45, + "height": 100 + }, + ... + ] +} +``` + +## `/alltables/` 接口 + +> 此接口可以获取数据库内所有的表名 > 返回的数据中不包含`ReqCount` + +> 无参数 + +```shell +$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/alltables/&key= +``` + +```json +{ + "code": 200, + "msg": "Success", + "data": [ + ... + ] +} +``` + +## `/export/` 接口 + +> 此接口可以导出应用的数据库文件 + +> 无参数 + +### 调用示例 + +```shell +$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/export/&key= +``` + +> 该接口请求成功后返回文件 + +## `/test`接口 + +> 此接口仅用于测试 只能增加`test-example`名称的计数次数 + +> 无需`key`即可请求 + +### 调用示例 + +```shell +$ curl -L -X GET https://requestcounter.herokuapp.com/api/v1/test +``` + +```json +622 +``` + +> 返回一个`int`数字 + +# 关于 + +## 开源 + +- 本项目以Apache-2.0许可开源, 即: + - 你可以直接使用该项目提供的功能, 无需任何授权 + - 你可以在**注明来源版权信息**的情况下对源代码进行任意分发和修改以及衍生 + +## 关于此页面 + +此文档由 [docsify](https://github.com/docsifyjs/docsify) 生成. docsify 是一个动态生成文档网站的工具。不同于 GitBook、Hexo 的地方是它不会生成将 .md 转成 .html +文件,所有转换工作都是在运行时进行。 + diff --git a/docs/_coverpage.md b/docs/_coverpage.md index 3048442..48956df 100644 --- a/docs/_coverpage.md +++ b/docs/_coverpage.md @@ -1,13 +1,13 @@ -![logo](./icon.svg) - -# RequestCounter - -> Python版的计数器 - -> 使用Python Flask 库完成的访问的次数计数器 - -* 响应速度快 -* 使用Sqlite3数据库操作 - -[GitHub](https://github.com/MarkusJoe/RequestCounter.git) +![logo](./icon.svg) + +# RequestCounter + +> Python版的计数器 + +> 使用Python Flask 库完成的访问的次数计数器 + +* 响应速度快 +* 使用Sqlite3数据库操作 + +[GitHub](https://github.com/MarkusJoe/RequestCounter.git) [Get Started](#部署) \ No newline at end of file