main.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from fastapi import FastAPI, Request, Form
  2. from fastapi.responses import HTMLResponse
  3. import subprocess
  4. app = FastAPI()
  5. @app.get("/", response_class=HTMLResponse)
  6. async def read_root(request: Request):
  7. html_content = """
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>FastAPI Form</title>
  14. <style>
  15. body {
  16. display: flex;
  17. flex-direction: column;
  18. justify-content: center;
  19. align-items: center;
  20. height: 100vh;
  21. margin: 0;
  22. font-family: Arial, sans-serif;
  23. background-color: #f7f7f7;
  24. }
  25. .container {
  26. width: 90%;
  27. max-width: 400px;
  28. text-align: center;
  29. background: white;
  30. padding: 20px;
  31. border-radius: 8px;
  32. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  33. }
  34. input, button {
  35. width: calc(100% - 20px);
  36. padding: 10px;
  37. margin: 10px 0;
  38. font-size: 16px;
  39. box-sizing: border-box;
  40. border: 1px solid #ddd;
  41. border-radius: 4px;
  42. }
  43. input:focus, button:focus {
  44. outline: none;
  45. border-color: #007bff;
  46. box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
  47. }
  48. #context {
  49. height: 100px;
  50. }
  51. button {
  52. background-color: #007bff;
  53. color: white;
  54. cursor: pointer;
  55. border: none;
  56. }
  57. button:hover {
  58. background-color: #0056b3;
  59. }
  60. @media (max-width: 600px) {
  61. input, button {
  62. width: 100%;
  63. }
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <div class="container">
  69. <form id="myForm" action="/submit/" method="post">
  70. <input type="text" id="title" name="title" placeholder="Title">
  71. <input type="text" id="context" name="context" placeholder="Context" style="height: 100px;">
  72. <button type="submit">Send</button>
  73. </form>
  74. </div>
  75. <script>
  76. document.getElementById('myForm').onsubmit = function(event) {
  77. var title = document.getElementById('title').value.trim();
  78. var context = document.getElementById('context').value.trim();
  79. if (!context) {
  80. alert('Context is a required field.');
  81. event.preventDefault(); // Prevent form submission
  82. } else if (!title) {
  83. document.getElementById('title').value = 'default';
  84. }
  85. };
  86. </script>
  87. </body>
  88. </html>
  89. """
  90. return html_content
  91. @app.post("/submit/")
  92. async def submit(title: str = Form(...), context: str = Form(...)):
  93. if not context:
  94. return {"error": "Context is required"}
  95. result = _send(title, context)
  96. return {"message": result}
  97. def _send(title, context):
  98. command = [
  99. 'curl',
  100. '-k',
  101. 'https://gotify.erhe.top/message?token=A0Xg6ZE5946iBYg',
  102. '-F', f'title={title}',
  103. '-F', f'message={context}',
  104. '-F', 'priority=5'
  105. ]
  106. # 执行命令
  107. try:
  108. result = subprocess.run(command, check=True, text=True, capture_output=True)
  109. print('命令执行成功,输出:')
  110. print(result.stdout)
  111. except subprocess.CalledProcessError as e:
  112. print('命令执行失败:')
  113. print(e.stderr)
  114. return result.stdout
  115. if __name__ == "__main__":
  116. import uvicorn
  117. uvicorn.run(app, host="0.0.0.0", port=8082)