| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- from fastapi import FastAPI
- from fastapi.staticfiles import StaticFiles
- from fastapi.responses import HTMLResponse
- from fastapi.middleware.cors import CORSMiddleware
- from pathlib import Path
- app = FastAPI(title="24点游戏计算器")
- # 添加CORS中间件
- app.add_middleware(
- CORSMiddleware,
- allow_origins=["*"], # 允许所有来源
- allow_credentials=True,
- allow_methods=["*"], # 允许所有方法
- allow_headers=["*"], # 允许所有头
- )
- # 创建静态文件目录
- static_dir = Path(__file__).parent.parent / "static"
- # 挂载静态文件
- app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
- from routes import api
- app.include_router(api.router, prefix="/api")
- @app.get("/", response_class=HTMLResponse)
- async def get_index():
- """返回主页HTML"""
- html_path = static_dir / "index.html"
- if html_path.exists():
- with open(html_path, encoding="utf-8") as f:
- return f.read()
- else:
- return """
- <html>
- <head>
- <title>24点游戏计算器</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="/static/styles.css">
- <script src="/static/script.js"></script>
- </head>
- <body>
- <div class="container">
- <h1>24点游戏计算器</h1>
- <div class="description">
- <p>输入4个1-13之间的数字,查询所有可能的24点解法</p>
- </div>
- <div class="input-container">
- <div class="number-inputs">
- <div class="input-group">
- <label for="num1">数字1:</label>
- <input type="number" id="num1" min="1" max="13" value="1">
- </div>
- <div class="input-group">
- <label for="num2">数字2:</label>
- <input type="number" id="num2" min="1" max="13" value="2">
- </div>
- <div class="input-group">
- <label for="num3">数字3:</label>
- <input type="number" id="num3" min="1" max="13" value="3">
- </div>
- <div class="input-group">
- <label for="num4">数字4:</label>
- <input type="number" id="num4" min="1" max="13" value="4">
- </div>
- </div>
- <button id="calculate-btn">计算解法</button>
- </div>
- <div class="results-container">
- <div class="results-header">
- <h2>计算结果</h2>
- <div class="stats" id="stats"></div>
- </div>
- <div class="filter-container">
- <label>
- <input type="checkbox" id="filter-flag1" checked>
- 显示一星解法
- </label>
- <label>
- <input type="checkbox" id="filter-flag2" checked>
- 显示二星解法
- </label>
- </div>
- <div class="solutions" id="solutions">
- <p class="placeholder">请输入4个数字并点击"计算解法"按钮</p>
- </div>
- </div>
- </div>
- </body>
- </html>
- """
|