__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from fastapi import FastAPI
  2. from fastapi.staticfiles import StaticFiles
  3. from fastapi.responses import HTMLResponse
  4. from fastapi.middleware.cors import CORSMiddleware
  5. from pathlib import Path
  6. app = FastAPI(title="24点游戏计算器")
  7. # 添加CORS中间件
  8. app.add_middleware(
  9. CORSMiddleware,
  10. allow_origins=["*"], # 允许所有来源
  11. allow_credentials=True,
  12. allow_methods=["*"], # 允许所有方法
  13. allow_headers=["*"], # 允许所有头
  14. )
  15. # 创建静态文件目录
  16. static_dir = Path(__file__).parent.parent / "static"
  17. # 挂载静态文件
  18. app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
  19. from routes import api
  20. app.include_router(api.router, prefix="/api")
  21. @app.get("/", response_class=HTMLResponse)
  22. async def get_index():
  23. """返回主页HTML"""
  24. html_path = static_dir / "index.html"
  25. if html_path.exists():
  26. with open(html_path, encoding="utf-8") as f:
  27. return f.read()
  28. else:
  29. return """
  30. <html>
  31. <head>
  32. <title>24点游戏计算器</title>
  33. <meta charset="utf-8">
  34. <meta name="viewport" content="width=device-width, initial-scale=1">
  35. <link rel="stylesheet" href="/static/styles.css">
  36. <script src="/static/script.js"></script>
  37. </head>
  38. <body>
  39. <div class="container">
  40. <h1>24点游戏计算器</h1>
  41. <div class="description">
  42. <p>输入4个1-13之间的数字,查询所有可能的24点解法</p>
  43. </div>
  44. <div class="input-container">
  45. <div class="number-inputs">
  46. <div class="input-group">
  47. <label for="num1">数字1:</label>
  48. <input type="number" id="num1" min="1" max="13" value="1">
  49. </div>
  50. <div class="input-group">
  51. <label for="num2">数字2:</label>
  52. <input type="number" id="num2" min="1" max="13" value="2">
  53. </div>
  54. <div class="input-group">
  55. <label for="num3">数字3:</label>
  56. <input type="number" id="num3" min="1" max="13" value="3">
  57. </div>
  58. <div class="input-group">
  59. <label for="num4">数字4:</label>
  60. <input type="number" id="num4" min="1" max="13" value="4">
  61. </div>
  62. </div>
  63. <button id="calculate-btn">计算解法</button>
  64. </div>
  65. <div class="results-container">
  66. <div class="results-header">
  67. <h2>计算结果</h2>
  68. <div class="stats" id="stats"></div>
  69. </div>
  70. <div class="filter-container">
  71. <label>
  72. <input type="checkbox" id="filter-flag1" checked>
  73. 显示一星解法
  74. </label>
  75. <label>
  76. <input type="checkbox" id="filter-flag2" checked>
  77. 显示二星解法
  78. </label>
  79. </div>
  80. <div class="solutions" id="solutions">
  81. <p class="placeholder">请输入4个数字并点击"计算解法"按钮</p>
  82. </div>
  83. </div>
  84. </div>
  85. </body>
  86. </html>
  87. """