From ea7f3ababa401149f067a73008b213b0e2e8f208 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Thu, 22 Dec 2022 17:02:00 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=AF=B9D?= =?UTF-8?q?eta=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=92=8C=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=80=E7=82=B9=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 ++++++++++++++ deta.json | 13 +++++++++++++ docs/README.md | 9 --------- src/config.py | 4 +++- src/db/__init__.py | 13 ++++++++++--- src/db/db.py | 5 ++++- 6 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 deta.json diff --git a/README.md b/README.md index ed5efd2..36fb283 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,20 @@ * 暂未支持`MongoDB` +# 安装 & 运行 + +```bash +$ pip3 install -r requirements.txt +$ uvicorn src:create_app --factory or +$ sh run.sh or +$ python3 main.py # 两条命令等价 +``` + +# 部署到Deta + +* ***你需要提前准备一个Deta账号*** +* [![Deploy](https://button.deta.dev/1/svg)](https://go.deta.dev/deploy?repo=https://github.com/RTAkland/MoeCounter) + # 开源 - 本项目以[Apache-2.0](./LICENSE)许可开源, 即: diff --git a/deta.json b/deta.json new file mode 100644 index 0000000..c42c897 --- /dev/null +++ b/deta.json @@ -0,0 +1,13 @@ +{ + "name": "MoeCounter", + "description": "一个Python版的MoeCounter重写, 并增加了一些功能", + "runtime": "python3.9", + "env": [ + { + "key": "COUNTER_DB", + "description": "数据库类型, 默认是sqlite3, 也可以使用mysql, 格式: user:pwd@host:port/db", + "value": "sqlite3", + "required": true + } + ] +} \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index c85febd..6bacade 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,15 +6,6 @@ * 使用sqlite, 直接填写为`sqlite3` 即默认 * 使用mysql, 需要按照以下格式填写: user:pwd@host:port/db -# 安装 & 运行 - -```bash -$ pip3 install -r requirements.txt -$ uvicorn src:create_app --factory or -$ sh run.sh or -$ python3 main.py # 两条命令等价 -``` - # 使用 * 访问`http://127.0.0.1:8000/` diff --git a/src/config.py b/src/config.py index f2c94ea..c5e737c 100644 --- a/src/config.py +++ b/src/config.py @@ -4,6 +4,7 @@ # @Development Tool: PyCharm # @Create Time: 2022/9/11 # @File Name: config.py +import os class Config: @@ -12,4 +13,5 @@ class Config: sqlite3 -> sqlite3 (default) mysql -> user:pwd@host:port/db """ - database = "sqlite3" # Database type + database = os.getenv('COUNTER_DB') or "sqlite3" # Database type + DETA = eval(os.getenv('DETA_RUNTIME').title()) # mark Deta diff --git a/src/db/__init__.py b/src/db/__init__.py index 60ea2d8..7df81d5 100644 --- a/src/db/__init__.py +++ b/src/db/__init__.py @@ -12,13 +12,20 @@ from src.config import Config database = Config.database # operator -if not os.path.exists('./src/db/data.sqlite') and \ - Config.database == 'sqlite3': + +def download_file(path: str): print('Downloading database file. Please wait...') file_url = 'https://static.rtast.cn/data.sqlite' - urlretrieve(file_url, './src/db/data.sqlite') # standard lib for downloading file + urlretrieve(file_url, path) # standard lib for downloading file print('Download database file successfully.') + +if Config.DETA and Config.database == 'sqlite3': + if not os.path.exists('/tmp/data.sqlite'): + download_file('/tmp/data.sqlite') +elif not Config.DETA and Config.database == 'sqlite3': + download_file('./src/db/data.sqlite') + if Config.database == 'sqlite3': from src.db.db import SQLite as Database diff --git a/src/db/db.py b/src/db/db.py index 3ff90c6..429ca54 100644 --- a/src/db/db.py +++ b/src/db/db.py @@ -56,7 +56,10 @@ class BaseSQL: class SQLite(BaseSQL): def __init__(self): super().__init__() - self.conn = operator.connect('./src/db/data.sqlite') + if Config.DETA: + self.conn = operator.connect('/tmp/data.sqlite') + else: + self.conn = operator.connect('./src/db/data.sqlite') self.cursor = self.conn.cursor()