Skip to content

Commit 6ca940a

Browse files
committed
👏更新docker镜像中处理裂图
1 parent 6d51fb9 commit 6ca940a

12 files changed

Lines changed: 78 additions & 25 deletions

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ WORKDIR /app
66

77
COPY . ./
88

9-
RUN pip install -r requirements.txt
9+
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
1010

1111
RUN python mkdocs.py
1212

@@ -16,8 +16,11 @@ FROM python:3.11-slim AS production
1616

1717
WORKDIR /app
1818

19+
RUN pip install fastapi uvicorn requests -i https://pypi.tuna.tsinghua.edu.cn/simple
20+
1921
COPY --from=builder-base /app/site ./site
22+
ADD app.py .
2023

2124
EXPOSE 8091
2225

23-
CMD python -m http.server 8091 --directory /app/site
26+
CMD python app.py

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313

1414
### markdown 在线文档
1515

16-
* [github](https://uaxe.github.io/geektime-docs/) (可能有裂图问题,可以在仓库里直接看markdown)
17-
* [netlify](https://geektime-docs.netlify.app/) (可能有裂图问题,可以在仓库里直接看markdown)
18-
16+
* [github](https://uaxe.github.io/geektime-docs/)
17+
* [netlify](https://geektime-docs.netlify.app/)
1918

2019
> tips: 在线文档支持 PC 浏览器,也支持移动端浏览器
20+
>
21+
> 第三方托管在线文档可能有裂图问题,可以在仓库里直接看markdown,或使用docker方式本地部署
2122
2223
### 本地部署
2324

2425
#### docker方式
26+
> 该方式实现了服务端代理请求图片,解决裂图,放心使用
2527
```shell
2628
docker run -d -p 8091:8091 --restart always --name geektime-docs zkep/geektime-docs
2729
```
@@ -50,7 +52,7 @@ mkdocs serve
5052

5153
方案1: 直接看pdf吧 [geektime-pdfs](https://github.com/uaxe/geektime-pdfs)
5254

53-
方案2: VIP用户,部署[my-geektime](https://github.com/zkep/my-geektime)服务,缓存对应的VIP课程
55+
方案2: VIP用户,部署[my-geektime](https://github.com/zkep/my-geektime)服务,缓存对应的VIP课程, 支持播放下载音视频
5456

5557
方案3: 推荐本地使用中间代理人服务,拦截请求,改写 http 请求的 Referer 的思路
5658

app.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import requests
2+
from fastapi import FastAPI, Query, HTTPException
3+
from fastapi.staticfiles import StaticFiles
4+
from fastapi.responses import StreamingResponse
5+
6+
app = FastAPI()
7+
8+
9+
@app.get("/proxy")
10+
async def proxy(
11+
url: str = Query(None),
12+
):
13+
headers = {
14+
"User-Agent": "Mozilla/5.0",
15+
"Referer": url,
16+
}
17+
try:
18+
with requests.get(url, timeout=60, headers=headers) as resp:
19+
if not resp or resp.status_code != 200:
20+
raise HTTPException(status_code=resp.status_code)
21+
content_type = resp.headers.get("Content-Type")
22+
return StreamingResponse(resp, headers=resp.headers, media_type=content_type)
23+
except Exception as e:
24+
raise HTTPException(status_code=500, detail=str(e))
25+
26+
27+
app.mount("/", StaticFiles(html=True, directory="./site"), name="static")
28+
29+
if __name__ == "__main__":
30+
import uvicorn
31+
uvicorn.run("app:app", workers=6, host="0.0.0.0", port=8091)

mkdocs.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import os
2+
import re
23
import shutil
34
from pathlib import Path
4-
55
import yaml
66

77

88
def _main():
9-
dirs = [
10-
'AI-大数据',
11-
'产品-运营',
12-
'前端-移动',
13-
'后端-架构',
14-
'管理-成长',
15-
'计算机基础',
16-
'运维-测试',
9+
dirs = ['AI-大数据','产品-运营','前端-移动','后端-架构','管理-成长','计算机基础','运维-测试']
10+
patterns = [
11+
re.compile(r'!\[\]\((https?://\S+?)\)'),
12+
re.compile(r'!$.*?$$(https?://[^\s$]+)'),
13+
]
14+
proxy_url = "http://127.0.0.1:8091/proxy?url={url}"
15+
proxy_urls = [
16+
"https://static001.geekbang.org/resource/image",
17+
"https://static001.geekbang.org/resource/avatar",
1718
]
1819
all = []
1920
docs_dir = Path(__file__).parent.joinpath('dist')
@@ -32,7 +33,20 @@ def _main():
3233
real_nav_path = os.path.join(docs_dir, nav_path)
3334
src_nav_path = os.path.join(item_dir, "docs", nav)
3435
os.makedirs(os.path.dirname(real_nav_path), exist_ok=True)
35-
shutil.copyfile(src_nav_path, real_nav_path)
36+
dst_raw = ''
37+
with open(src_nav_path, 'r') as ff:
38+
for line in ff.readlines():
39+
line = line.strip()
40+
for pattern in patterns:
41+
for uri in pattern.findall(line):
42+
for purl in proxy_urls:
43+
if purl in uri:
44+
dst_url = proxy_url.format(url=uri)
45+
line = line.replace(uri, dst_url)
46+
dst_raw += line
47+
dst_raw += "\n"
48+
with open(real_nav_path, 'w') as fi:
49+
fi.write(dst_raw)
3650
items.append(nav_path)
3751
index_text += f'\n[{os.path.basename(item_dir)}]({os.path.basename(item_dir)}/{os.path.basename(item_dir)})\n'
3852
navs.append({sub_dir: items})

requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ pyyaml
88
webdriver-manager
99
requests
1010
pypdf
11-
pillow
11+
pillow
12+
fastapi
13+
uvicorn
14+
markdown==3.6
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)