alloyer/alloyer-cnc-api
0
1from fastapi import FastAPI, UploadFile, File, Form2from fastapi.middleware.cors import CORSMiddleware3import os4import tempfile5from analyzer import step_analyzer6 7app = FastAPI()8 9# 放行你的独立站域名10app.add_middleware(11 CORSMiddleware,12 allow_origins=["https://alloyer.com"],13 allow_credentials=True,14 allow_methods=["*"],15 allow_headers=["*"],16)17 18@app.get("/")19def root():20 return {"status": "online"}21 22@app.post("/upload-step")23async def upload_step(24 file: UploadFile = File(...),25 material: str = Form("6061"),26 quantity: int = Form(1)27):28 suffix = file.filename.split(".")[-1].lower()29 if suffix not in ["step", "stp"]:30 return {"error": "Only STEP/STP files are supported"}31 32 with tempfile.NamedTemporaryFile(delete=False, suffix=".step") as tmp:33 tmp.write(await file.read())34 tmp_path = tmp.name35 36 try:37 result = step_analyzer(tmp_path, material, quantity)38 except Exception as e:39 os.unlink(tmp_path)40 return {"error": str(e)}41 42 os.unlink(tmp_path)43 return result44 if __name__ == "__main__":45 import uvicorn46 uvicorn.run("app:app", host="0.0.0.0", port=7860)