Skip to content

Commit

Permalink
✨ feat: 新增软件更新接口
Browse files Browse the repository at this point in the history
  • Loading branch information
kalicyh committed Aug 9, 2024
1 parent 1edadb3 commit f3f50ec
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 6 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ jobs:
ghcr.io/${{ github.repository }}:${{ env.VERSION }}
ghcr.io/${{ github.repository }}:latest
platforms: linux/amd64,linux/arm64

- name: Upload Tag and Artifacts to FastAPI
env:
FASTAPI_URL: ${{ secrets.FASTAPI_URL }}
run: |
TAG="${GITHUB_REF#refs/tags/}"
echo "Tag: $TAG"
# 上传 tag
curl -X POST "$FASTAPI_URL/store-tag/" \
-H "Content-Type: application/json" \
-d "{\"tag\": \"$TAG\"}"
37 changes: 36 additions & 1 deletion api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,39 @@ def get_records_by_full_filter(db: Session, category: str, month: str, name: str
Record.category.like(f'%{category}%'),
Record.month.like(f'%{month}%'),
Record.name.like(f'%{name}%')
).all()
).all()

def update_version_filename(db: Session, filename: str):
version_info = db.query(Info).first()
if version_info:
version_info.client_filename = filename
else:
new_info = Info(
client_filename=filename
)
db.add(new_info)
db.commit()

def update_version_info(db: Session, version: str, client_type: str):
if client_type not in ["client", "backend"]:
raise ValueError("Invalid client_type. Must be 'client' or 'backend'.")

version_info = db.query(Info).first()

if client_type == "client":
if version_info:
version_info.client_versions = version
else:
new_info = Info(
client_versions=version
)
db.add(new_info)
elif client_type == "backend":
if version_info:
version_info.backend_versions = version
else:
new_info = Info(
backend_versions=version
)
db.add(new_info)
db.commit()
7 changes: 5 additions & 2 deletions api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ class Record(Base):
class Info(Base):
__tablename__ = 'info'
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
last_updated = Column(String(20))
total_rows = Column(Integer)
last_updated = Column(String(20), nullable=True)
total_rows = Column(Integer, nullable=True)
client_versions = Column(String(255), nullable=True)
client_filename = Column(String(255), nullable=True)
backend_versions = Column(String(255), nullable=True)

class Number(Base):
__tablename__ = 'numbers'
Expand Down
47 changes: 45 additions & 2 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from fastapi import FastAPI
from fastapi import FastAPI, UploadFile, File
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from .routes.talking_points import router as talking_points_router
from .routes.numbers import router as numbers_router
from pydantic import BaseModel
from pathlib import Path
from .crud import update_version_info, update_version_filename, get_info
from .database import SessionLocal

app = FastAPI()

Expand All @@ -26,6 +30,45 @@ def root():

@app.get("/info")
async def get_infos():
db = SessionLocal()
info = get_info(db)
db.close()
print()
return JSONResponse(content={
"version": "v1.6.1"
"client_versions": info.client_versions,
"backend_versions": info.backend_versions,
"client_filename": info.client_filename
})

# 文件存储路径
UPLOAD_DIR = Path("./file")
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)

class TagModel(BaseModel):
tag: str

@app.post("/store-tag/")
async def store_tag(tag: TagModel):
tag = tag.tag
db = SessionLocal()
update_version_info(db, tag, "client")
db.close()
return {"message": "Tag stored successfully"}

@app.post("/upload-file/")
async def upload_file(tag: str, file: UploadFile = File(...)):
db = SessionLocal()
update_version_info(db, tag, "backend")
update_version_filename(db, file.filename+"_"+tag)
db.close()
file_path = UPLOAD_DIR / f"{file.filename}_{tag}"
with file_path.open("wb") as buffer:
buffer.write(await file.read())
return {"message": "File uploaded successfully"}

@app.get("/download-file/{filename}")
async def download_file(filename: str):
file_path = UPLOAD_DIR / filename
if file_path.exists():
return FileResponse(path=file_path, media_type='application/octet-stream', filename=filename)
return {"error": "File not found"}
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default {
async fetchInfo() {
try {
const result = await fetchInfo('/info');
this.version = result.version;
this.version = result.backend_versions;
} catch (error) {
this.error = error.message;
}
Expand Down

0 comments on commit f3f50ec

Please sign in to comment.