diff --git a/kubernetes/fastapi-chart/Chart.yaml b/kubernetes/fastapi-chart/Chart.yaml index c472b31..4d3b922 100644 --- a/kubernetes/fastapi-chart/Chart.yaml +++ b/kubernetes/fastapi-chart/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 -name: fastapi-k8s-app +name: mark-word-fastapi description: A Helm chart for deploying FastAPI application on Kubernetes version: 0.1.0 # Chart 版本 appVersion: "1.0.0" # 应用版本,通常与镜像版本关联 \ No newline at end of file diff --git a/kubernetes/fastapi-chart/templates/_helpers.tpl b/kubernetes/fastapi-chart/templates/_helpers.tpl index 014339a..b9c6584 100644 --- a/kubernetes/fastapi-chart/templates/_helpers.tpl +++ b/kubernetes/fastapi-chart/templates/_helpers.tpl @@ -1,7 +1,7 @@ {{/* Expand the name of the chart. */}} -{{- define "fastapi-k8s-app.name" -}} +{{- define "mark-word-fastapi.name" -}} {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} {{- end }} @@ -10,7 +10,7 @@ Create a default fully qualified app name. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). If release name contains chart name it will be used as a full name. */}} -{{- define "fastapi-k8s-app.fullname" -}} +{{- define "mark-word-fastapi.fullname" -}} {{- if .Values.fullnameOverride }} {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} {{- else }} @@ -26,16 +26,16 @@ If release name contains chart name it will be used as a full name. {{/* Create chart name and version as part of the label. */}} -{{- define "fastapi-k8s-app.chart" -}} +{{- define "mark-word-fastapi.chart" -}} {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} {{- end }} {{/* Common labels */}} -{{- define "fastapi-k8s-app.labels" -}} -helm.sh/chart: {{ include "fastapi-k8s-app.chart" . }} -{{ include "fastapi-k8s-app.selectorLabels" . }} +{{- define "mark-word-fastapi.labels" -}} +helm.sh/chart: {{ include "mark-word-fastapi.chart" . }} +{{ include "mark-word-fastapi.selectorLabels" . }} {{- if .Chart.AppVersion }} app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} {{- end }} @@ -45,7 +45,7 @@ app.kubernetes.io/managed-by: {{ .Release.Service }} {{/* Selector labels */}} -{{- define "fastapi-k8s-app.selectorLabels" -}} -app.kubernetes.io/name: {{ include "fastapi-k8s-app.name" . }} +{{- define "mark-word-fastapi.selectorLabels" -}} +app.kubernetes.io/name: {{ include "mark-word-fastapi.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} {{- end }} \ No newline at end of file diff --git a/kubernetes/fastapi-chart/templates/deployment.yaml b/kubernetes/fastapi-chart/templates/deployment.yaml index ef7bc4d..e03da87 100644 --- a/kubernetes/fastapi-chart/templates/deployment.yaml +++ b/kubernetes/fastapi-chart/templates/deployment.yaml @@ -1,18 +1,18 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: {{ include "fastapi-k8s-app.fullname" . }} + name: {{ include "mark-word-fastapi.fullname" . }} labels: - {{- include "fastapi-k8s-app.labels" . | nindent 4 }} + {{- include "mark-word-fastapi.labels" . | nindent 4 }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: - {{- include "fastapi-k8s-app.selectorLabels" . | nindent 6 }} + {{- include "mark-word-fastapi.selectorLabels" . | nindent 6 }} template: metadata: labels: - {{- include "fastapi-k8s-app.selectorLabels" . | nindent 8 }} + {{- include "mark-word-fastapi.selectorLabels" . | nindent 8 }} spec: containers: - name: {{ .Chart.Name }} diff --git a/kubernetes/fastapi-chart/templates/ingress.yaml b/kubernetes/fastapi-chart/templates/ingress.yaml new file mode 100644 index 0000000..ec5e179 --- /dev/null +++ b/kubernetes/fastapi-chart/templates/ingress.yaml @@ -0,0 +1,31 @@ +# kubernetes/fastapi-chart/templates/ingress.yaml +{{- if .Values.ingress.enabled -}} +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ include "mark-word-fastapi.fullname" . }} + labels: + {{- include "mark-word-fastapi.labels" . | nindent 4 }} + annotations: + # 可以添加 Traefik 特定的 annotation,例如强制重定向到 HTTPS + # traefik.ingress.kubernetes.io/router.entrypoints: web, websecure + # traefik.ingress.kubernetes.io/router.tls: "true" + # traefik.ingress.kubernetes.io/router.middlewares: default-redirect-https@kubernetescrd +spec: + ingressClassName: {{ .Values.ingress.className }} + rules: + - host: {{ .Values.ingress.host | quote }} + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: {{ include "mark-word-fastapi.fullname" . }} + port: + number: {{ .Values.service.port }} + {{- if .Values.ingress.tls }} + tls: + {{- toYaml .Values.ingress.tls | nindent 4 }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/kubernetes/fastapi-chart/templates/service.yaml b/kubernetes/fastapi-chart/templates/service.yaml index 3e0d6cb..19e0601 100644 --- a/kubernetes/fastapi-chart/templates/service.yaml +++ b/kubernetes/fastapi-chart/templates/service.yaml @@ -1,9 +1,9 @@ apiVersion: v1 kind: Service metadata: - name: {{ include "fastapi-k8s-app.fullname" . }} + name: {{ include "mark-word-fastapi.fullname" . }} labels: - {{- include "fastapi-k8s-app.labels" . | nindent 4 }} + {{- include "mark-word-fastapi.labels" . | nindent 4 }} spec: type: {{ .Values.service.type }} ports: @@ -12,4 +12,4 @@ spec: protocol: TCP name: http selector: - {{- include "fastapi-k8s-app.selectorLabels" . | nindent 4 }} \ No newline at end of file + {{- include "mark-word-fastapi.selectorLabels" . | nindent 4 }} \ No newline at end of file diff --git a/kubernetes/fastapi-chart/values.yaml b/kubernetes/fastapi-chart/values.yaml index b987fb4..42c51f1 100644 --- a/kubernetes/fastapi-chart/values.yaml +++ b/kubernetes/fastapi-chart/values.yaml @@ -7,3 +7,14 @@ service: type: ClusterIP # 或 LoadBalancer,根据你的需求 port: 8000 # 更多配置可以按需添加,例如 resources, probes 等 +ingress: + enabled: true + className: traefik # K3s 默认使用 Traefik + host: markword.simpla.dev # <-- 替换为你希望访问的域名 + # 或者使用 Node IP 直接访问 + # path: / + # pathType: Prefix + # tls: + # - hosts: + # - markword.simpla.dev + # secretName: fastapi-tls-secret # 你的TLS Secret名称 \ No newline at end of file diff --git a/mark_word.py b/mark_word.py deleted file mode 100644 index 4e9616f..0000000 --- a/mark_word.py +++ /dev/null @@ -1,131 +0,0 @@ -# from docx import Document -import os -from tempfile import NamedTemporaryFile - -import thulac -from fastapi import FastAPI, Form, UploadFile, File, Request -from fastapi.responses import FileResponse -from fastapi.responses import HTMLResponse -from fastapi.templating import Jinja2Templates -from pydantic import BaseModel -from reportlab.lib.pagesizes import letter -from reportlab.pdfgen import canvas - - -class ExportRequest(BaseModel): - format: str # 导出格式(html、pdf、rtf) - content: str # 要导出的内容 - -# 初始化 THULAC 分词工具 -thu = thulac.thulac() - -# 初始化 FastAPI 应用和模板 -app = FastAPI() -templates = Jinja2Templates(directory="templates") - - -@app.get("/", response_class=HTMLResponse) -async def home(request: Request): - return templates.TemplateResponse("index.html", {"request": request}) - - -@app.post("/process", response_class=HTMLResponse) -async def process(request: Request, file: UploadFile = File(None), text: str = Form(None)): - """ - 统一处理上传文件和粘贴文本,返回标注结果页面 - """ - if file: - # 读取上传文件内容 - content = await file.read() - text_content = content.decode("utf-8") - elif text: - # 读取粘贴文本内容 - text_content = text - else: - return templates.TemplateResponse( - "error.html", {"request": request, "message": "未提供文件或文本"} - ) - - # 处理文本,返回标注结果 - processed_text = process_text(text) - - return templates.TemplateResponse("result.html", {"request": request, "content": processed_text}) - - -def process_text(text: str) -> str: - """处理文本并为每种词性添加 CSS 类,同时保留段落换行""" - words = thu.cut(text) - colored_text = "" - - for word, tag in words: - if tag: # 确保 tag 不为空 - colored_text += f"{word}" - else: - colored_text += word # 没有词性时直接添加词语 - - # 替换换行符为
- return colored_text.replace("\n", "
") - - - -@app.post("/export") -async def export_file(request: ExportRequest): - format = request.format - content = request.content - - if format == "html": - file_name = "exported_result.html" - file_content = f"{content}" - return create_file_response(file_name, file_content.encode("utf-8"), "text/html") - - elif format == "pdf": - with NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file: - generate_pdf(temp_file.name, content) - return FileResponse(temp_file.name, filename="exported_result.pdf", media_type="application/pdf") - - elif format == "rtf": - file_name = "exported_result.rtf" - file_content = generate_rtf(content) - return create_file_response(file_name, file_content.encode("utf-8"), "application/rtf") - - return {"error": "Unsupported format"} - - -def create_file_response(file_name, file_content, media_type): - """ - 创建文件响应 - """ - with NamedTemporaryFile(delete=False, suffix=os.path.splitext(file_name)[1]) as temp_file: - temp_file.write(file_content) - temp_file.flush() - return FileResponse(temp_file.name, filename=file_name, media_type=media_type) - - -def generate_pdf(file_path, content): - """ - 生成 PDF 文件 - """ - c = canvas.Canvas(file_path, pagesize=letter) - c.setFont("Helvetica", 12) - width, height = letter - y = height - 40 - - for line in content.split("
"): - c.drawString(40, y, line.strip()) - y -= 20 - if y < 40: # 换页条件 - c.showPage() - c.setFont("Helvetica", 12) - y = height - 40 - c.save() - - -def generate_rtf(content): - """ - 生成 RTF 文件 - """ - rtf_header = r"{\rtf1\ansi\deff0" - rtf_footer = r"}" - rtf_body = content.replace("
", r"\line ") - return rtf_header + rtf_body + rtf_footer -