You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.7 KiB
90 lines
3.7 KiB
|
6 months ago
|
#!/usr/bin/env python3
|
||
|
|
import os
|
||
|
|
import json
|
||
|
|
import urllib.parse
|
||
|
|
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
||
|
|
|
||
|
|
PORT = 8000
|
||
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||
|
|
|
||
|
|
class DocHandler(SimpleHTTPRequestHandler):
|
||
|
|
def do_GET(self):
|
||
|
|
parsed = urllib.parse.urlparse(self.path)
|
||
|
|
path = parsed.path
|
||
|
|
|
||
|
|
# ---------- 1. API 接口 ----------
|
||
|
|
if path == '/api/list':
|
||
|
|
self.send_response(200)
|
||
|
|
self.send_header('Content-Type', 'application/json; charset=utf-8')
|
||
|
|
self.end_headers()
|
||
|
|
|
||
|
|
modules = []
|
||
|
|
for root, dirs, files in os.walk(BASE_DIR):
|
||
|
|
dirs[:] = [d for d in dirs if not d.startswith('.') and d not in ['__pycache__']]
|
||
|
|
for f in files:
|
||
|
|
if f.endswith('.md') and not f.startswith('.'):
|
||
|
|
rel_path = os.path.relpath(os.path.join(root, f), BASE_DIR)
|
||
|
|
module_name = '通用文档' if root == BASE_DIR else os.path.basename(root)
|
||
|
|
modules.append({
|
||
|
|
'filename': rel_path,
|
||
|
|
'name': f.replace('.md', '').replace('实施文档', '').strip() or f.replace('.md', ''),
|
||
|
|
'module': module_name
|
||
|
|
})
|
||
|
|
modules.sort(key=lambda x: (x['module'], x['filename']))
|
||
|
|
self.wfile.write(json.dumps(modules, ensure_ascii=False).encode('utf-8'))
|
||
|
|
return
|
||
|
|
|
||
|
|
if path.startswith('/api/read/'):
|
||
|
|
raw_filename = path[10:]
|
||
|
|
filename = urllib.parse.unquote(urllib.parse.unquote(raw_filename))
|
||
|
|
if '..' in filename or filename.startswith('/') or filename.startswith('\\'):
|
||
|
|
self.send_response(403)
|
||
|
|
self.end_headers()
|
||
|
|
self.wfile.write(b'Forbidden')
|
||
|
|
return
|
||
|
|
|
||
|
|
filepath = os.path.join(BASE_DIR, filename)
|
||
|
|
if not os.path.exists(filepath) or not filepath.endswith('.md'):
|
||
|
|
self.send_response(404)
|
||
|
|
self.end_headers()
|
||
|
|
self.wfile.write(f'文件不存在: {filename}'.encode('utf-8'))
|
||
|
|
return
|
||
|
|
|
||
|
|
try:
|
||
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
||
|
|
content = f.read()
|
||
|
|
self.send_response(200)
|
||
|
|
self.send_header('Content-Type', 'text/plain; charset=utf-8')
|
||
|
|
self.end_headers()
|
||
|
|
self.wfile.write(content.encode('utf-8'))
|
||
|
|
except Exception as e:
|
||
|
|
self.send_response(500)
|
||
|
|
self.end_headers()
|
||
|
|
self.wfile.write(f'服务器错误: {str(e)}'.encode('utf-8'))
|
||
|
|
return
|
||
|
|
|
||
|
|
# ---------- 2. 静态文件检查(修复图片无法显示的关键) ----------
|
||
|
|
# 拼接完整的本地路径
|
||
|
|
full_path = os.path.join(BASE_DIR, path.lstrip('/'))
|
||
|
|
# 如果文件真实存在,直接提供文件
|
||
|
|
if os.path.exists(full_path) and os.path.isfile(full_path):
|
||
|
|
self.path = path
|
||
|
|
return super().do_GET()
|
||
|
|
|
||
|
|
# ---------- 3. SPA 回退(所有其他请求返回 index.html) ----------
|
||
|
|
# 这样前端路由可以接管 /view/ 等路径
|
||
|
|
self.path = '/index.html'
|
||
|
|
return super().do_GET()
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
server_address = ('', PORT)
|
||
|
|
httpd = HTTPServer(server_address, DocHandler)
|
||
|
|
print(f'🚀 文档管理系统已启动!')
|
||
|
|
print(f'📂 工作目录: {BASE_DIR}')
|
||
|
|
print(f'🌐 访问地址: http://localhost:{PORT}')
|
||
|
|
print(f'📄 按 Ctrl+C 停止服务\n')
|
||
|
|
try:
|
||
|
|
httpd.serve_forever()
|
||
|
|
except KeyboardInterrupt:
|
||
|
|
print('\n🛑 服务已停止')
|
||
|
|
httpd.shutdown()
|