Skip to content

Commit a087611

Browse files
committed
feat(docker): docker
1 parent 4bbddb9 commit a087611

23 files changed

+2448
-2925
lines changed

.dockerignore

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1-
/**/.env.development
21
/**/.data
2+
/.*
3+
/logs
4+
/tmp
5+
/tests
6+
/img
7+
/dist
8+
/**/*.egg-info
9+
10+
# local env files
11+
/.env.dev
12+
/.env.prod
13+
/.env.local
14+
15+
# Editor directories and files
16+
.DS_Store
17+
.idea
18+
.vscode
19+
*.suo
20+
*.ntvs*
21+
*.njsproj
22+
*.sln
23+
*.sw?
24+
25+
/temp
26+
/src/retk/plugins/official_plugins/**/*.json

Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3.11.9-slim-bullseye
2+
3+
4+
WORKDIR /app
5+
6+
COPY . /app
7+
ENV PYTHONPATH=/app/src
8+
RUN python3 -m pip install --no-cache-dir -e .
9+
10+
EXPOSE 8080
11+
12+
ENV APP_LANGUAGE en
13+
ENV APP_PASSWORD ""
14+
ENV API_URL "http://127.0.0.1:8080"
15+
16+
ENTRYPOINT ["python3", "run_in_docker.py"]

README.md

+40-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<strong>English</strong> | <a href="README_ZH.md" target="_blank">简体中文</a>
1414
</p>
1515

16-
Rethink is my understanding of self-developing.
16+
Rethink is a new understanding of self-developing.
1717

1818
Every time a new thought is recorded,
1919
the relevant old thought will automatically emerge,
@@ -43,7 +43,45 @@ Making the new thought more connectable and memorable.
4343
which makes it easy to synchronize across multiple platforms.
4444
5. **Multi-language**: Support multiple languages, including Chinese and English.
4545

46-
## Install
46+
## Deploy by Docker
47+
48+
1. Pull the image:
49+
50+
```shell
51+
docker pull morvanzhou/rethink
52+
```
53+
54+
2. Run the container:
55+
56+
For keeping your data safe, you should mount a local path to the container.
57+
58+
```shell
59+
docker run -p 8080:8080 -v /your/data/path:/app/.data morvanzhou/rethink
60+
```
61+
62+
Then you can visit `http://127.0.0.1:8080` in your browser.
63+
64+
If you want to define another host's port, you have to also add an environment variable `API_URL`:
65+
66+
```shell
67+
docker run -e API_URL=http://127.0.0.1:8001 -p 8001:8080 -v /your/data/path:/app/.data morvanzhou/rethink
68+
```
69+
70+
If you want to set a password for authorization, you have to also add a environment variable `APP_PASSWORD`:
71+
72+
```shell
73+
docker run -e APP_PASSWORD=12345678 -p 8080:8080 -v /your/data/path:/app/.data morvanzhou/rethink
74+
```
75+
76+
All environment variables:
77+
78+
- `API_URL`: the API url in app js code, default is `http://127.0.0.1:8080`
79+
- `APP_PASSWORD`: authorization password, default is None
80+
- `APP_LANGUAGE`: language, default is English, optional: zh, en
81+
82+
## Use Python and pip to install
83+
84+
The second way to use Rethink is to install it via pip.
4785

4886
First install:
4987

README_ZH.md

+41-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</p>
1515

1616

17-
Rethink 表示重新思考,是我对于成长的理解
17+
Rethink 表示重新思考,是对个人成长的新理解
1818

1919
每次记下新的认知时,相关的老认知都会自动涌现,交叉贯通,新旧认知不断 类比-迁移。
2020
使得每次记录都有再次发光的机会。所以 Rethink 的中文名是 比移。
@@ -38,7 +38,46 @@ Rethink 表示重新思考,是我对于成长的理解。
3838
,便于在多个平台之间同步;
3939
5. **多语言**:支持多种语言,包括中文和英文。
4040

41-
## 安装
41+
## 容器化部署
42+
43+
1. 拉取镜像:
44+
45+
```shell
46+
docker pull morvanzhou/rethink
47+
```
48+
49+
2. 运行容器:
50+
51+
为了保证数据安全,您应该将本地路径挂载到容器中。
52+
53+
```shell
54+
docker run -p 8080:8080 -v /your/data/path:/app/.data morvanzhou/rethink
55+
```
56+
57+
现在你可以在浏览器中访问 `http://127.0.0.1:8080` 使用服务。
58+
59+
如果你想自定义其他端口,你除了需要修改 `-p` 参数的前半部分,还需要添加一个环境变量 `API_URL` 来重定向前端服务中的 API 的地址:
60+
请确保 `API_URL` 里的端口号和 `-p` 参数的前半部分一致。
61+
62+
```shell
63+
docker run -e API_URL=http://127.0.0.1:8081 -p 8081:8080 -v /your/data/path:/app/.data morvanzhou/rethink
64+
```
65+
66+
如果你想做为 Rethink 鉴权,你可以添加环境变量 `APP_PASSWORD`
67+
68+
```shell
69+
docker run -e APP_PASSWORD=12345678 -p 8080:8080 -v /your/data/path:/app/.data morvanzhou/rethink
70+
```
71+
72+
全部可配置的环境变量如下:
73+
74+
- `API_URL`:前端服务中 API 的地址,默认为 `http://127.0.0.1:8080`
75+
- `APP_PASSWORD`:鉴权密码,默认为 None
76+
- `APP_LANGUAGE`:语言,默认为英语,可选值:zh, en
77+
78+
## 使用 python 的 pip 安装
79+
80+
第二种安装使用 Rethink 的方法是通过 pip 安装。然后用 python 直接启动服务。
4281

4382
首次安装:
4483

dockerBuild.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version=$(grep "^version" setup.cfg | awk -F'=' '{print $2}' | tr -d '[:space:]')
2+
docker build -t morvanzhou/rethink:$version -t morvanzhou/rethink:latest .

main.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
args = parser.parse_args()
1818

1919
if __name__ == "__main__":
20-
os.environ["VUE_APP_API_PORT"] = str(args.port)
20+
os.environ["VUE_APP_API_URL"] = f"http://{args.host}:{args.port}"
2121
if os.environ.get("VUE_APP_MODE") is None:
2222
os.environ["VUE_APP_MODE"] = args.mode
2323

2424
os.environ["RETHINK_SERVER_HEADLESS"] = "1" if args.headless else "0"
25-
os.environ["RETHINK_SERVER_HOSTNAME"] = args.host
2625
os.environ["RETHINK_SERVER_DEBUG"] = "true" if args.debug else "false"
2726
if args.password is not None:
2827
os.environ["RETHINK_SERVER_PASSWORD"] = args.password

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ httpx>=0.25.0
1212
captcha>=0.5.0
1313
python-multipart>=0.0.9
1414
cos-python-sdk-v5~=1.9.26
15-
numpy>=1.24.4
1615
whoosh~=2.7.4
1716
jieba>=0.42.1
1817
starlette>=0.37.2

run.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ do
5252
esac
5353
done
5454

55-
export VUE_APP_API_PORT=$port
55+
export VUE_APP_API_URL=http://$host:$port
5656
export VUE_APP_MODE=$mode
5757
# if mode is local, export RETHINK_LOCAL_STORAGE_PATH to .data
5858
if [ $mode == "local" ]; then
5959
export RETHINK_LOCAL_STORAGE_PATH=.
60-
export RETHINK_SERVER_HOSTNAME=localhost
6160
export RETHINK_SERVER_HEADLESS=1
6261
fi
6362
echo "Running in $mode mode with reload=$reload on $host:$port"

run_in_docker.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
import uvicorn
4+
5+
from retk.run import _plugins_start_on_schedule
6+
7+
8+
def main():
9+
os.environ["VUE_APP_API_URL"] = os.environ.get("API_URL")
10+
os.environ["VUE_APP_MODE"] = "local"
11+
12+
os.environ["RETHINK_DEFAULT_LANGUAGE"] = os.environ.get("APP_LANGUAGE")
13+
os.environ["RETHINK_LOCAL_STORAGE_PATH"] = os.getcwd()
14+
os.environ["RETHINK_SERVER_HEADLESS"] = "0"
15+
os.environ["RETHINK_SERVER_DEBUG"] = "true"
16+
17+
pw = os.environ.get("APP_PASSWORD", "")
18+
if pw != "":
19+
if len(pw) < 6 or len(pw) > 20:
20+
raise ValueError("Password length should be between 6 and 20 characters!")
21+
os.environ["RETHINK_SERVER_PASSWORD"] = pw
22+
23+
_plugins_start_on_schedule()
24+
uvicorn.run(
25+
"retk.application:app",
26+
host="0.0.0.0",
27+
port=8080,
28+
reload=False,
29+
workers=1,
30+
log_level="error",
31+
env_file=os.path.join(os.path.abspath(os.path.dirname(__file__)), "src", "retk", ".env.local"),
32+
)
33+
34+
35+
if __name__ == "__main__":
36+
main()

setup.cfg

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = retk
3-
version = 0.2.8
3+
version = 0.2.9
44
author = MorvanZhou
55
author_email = [email protected]
66
description = keep and reuse your thoughts
@@ -39,8 +39,6 @@ install_requires =
3939
httpx>=0.25.0
4040
captcha>=0.5.0
4141
python-multipart>=0.0.6
42-
cos-python-sdk-v5~=1.9.26
43-
numpy>=1.24.4
4442
whoosh~=2.7.4
4543
jieba>=0.42.1
4644
starlette>=0.27.0
@@ -66,3 +64,4 @@ build =
6664
remote =
6765
motor>=3.3.2
6866
elasticsearch[async]~=8.11.0
67+
cos-python-sdk-v5~=1.9.26

src/retk/application.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ async def lifespan(app: FastAPI):
4040
)
4141
logger.debug(f'startup_event RETHINK_LOCAL_STORAGE_PATH: {os.environ.get("RETHINK_LOCAL_STORAGE_PATH")}')
4242
logger.debug(f'startup_event VUE_APP_MODE: {os.environ.get("VUE_APP_MODE")}')
43-
logger.debug(f'startup_event VUE_APP_API_PORT: {os.environ.get("VUE_APP_API_PORT")}')
43+
logger.debug(f'startup_event VUE_APP_API_URL: {os.environ.get("VUE_APP_API_URL")}')
4444
logger.debug(f'startup_event RETHINK_DEFAULT_LANGUAGE: {os.environ.get("RETHINK_DEFAULT_LANGUAGE")}')
4545
await client.init()
46-
logger.debug("db initialized")
4746

4847
# schedule job
4948
scheduler.start()

src/retk/core/files/saver.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
from PIL import Image, UnidentifiedImageError
77
from bson import ObjectId
8-
from qcloud_cos import CosConfig, CosServiceError, CosS3Client
8+
9+
try:
10+
from qcloud_cos import CosConfig, CosServiceError, CosS3Client
11+
except ImportError:
12+
pass
913

1014
from retk.config import get_settings, is_local_db
1115
from retk.const.app import FileTypesEnum

src/retk/core/node/backup.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
from bson.tz_util import utc
77
from pydantic_settings import BaseSettings
8-
from qcloud_cos import CosConfig, CosServiceError, CosS3Client
8+
9+
try:
10+
from qcloud_cos import CosConfig, CosServiceError, CosS3Client
11+
except ImportError:
12+
pass
913

1014
from retk import config, const
1115
from retk.logger import logger
@@ -200,7 +204,7 @@ def __cos_connect(
200204
nid: str,
201205
version: str,
202206
exist_ok: bool = False,
203-
) -> Tuple[CosS3Client, str, const.CodeEnum]:
207+
) -> Tuple["CosS3Client", str, const.CodeEnum]:
204208
cos_client, key = __get_client_and_key(settings, uid, nid, version)
205209

206210
if not exist_ok:
@@ -217,7 +221,7 @@ def __cos_connect(
217221
return cos_client, key, const.CodeEnum.OK
218222

219223

220-
def __get_client_and_key(settings: BaseSettings, uid: str, nid: str, version: str = None) -> Tuple[CosS3Client, str]:
224+
def __get_client_and_key(settings: BaseSettings, uid: str, nid: str, version: str = None) -> Tuple["CosS3Client", str]:
221225
cos_client = CosS3Client(
222226
CosConfig(
223227
Region=settings.COS_REGION,

0 commit comments

Comments
 (0)