| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- from fastapi import FastAPI, Request, Form
- from fastapi.responses import HTMLResponse
- import subprocess
- app = FastAPI()
- @app.get("/", response_class=HTMLResponse)
- async def read_root(request: Request):
- html_content = """
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>FastAPI Form</title>
- <style>
- body {
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- height: 100vh;
- margin: 0;
- font-family: Arial, sans-serif;
- background-color: #f7f7f7;
- }
- .container {
- width: 90%;
- max-width: 400px;
- text-align: center;
- background: white;
- padding: 20px;
- border-radius: 8px;
- box-shadow: 0 2px 5px rgba(0,0,0,0.2);
- }
- input, button {
- width: calc(100% - 20px);
- padding: 10px;
- margin: 10px 0;
- font-size: 16px;
- box-sizing: border-box;
- border: 1px solid #ddd;
- border-radius: 4px;
- }
- input:focus, button:focus {
- outline: none;
- border-color: #007bff;
- box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
- }
- #context {
- height: 100px;
- }
- button {
- background-color: #007bff;
- color: white;
- cursor: pointer;
- border: none;
- }
- button:hover {
- background-color: #0056b3;
- }
- @media (max-width: 600px) {
- input, button {
- width: 100%;
- }
- }
- </style>
- </head>
- <body>
- <div class="container">
- <form id="myForm" action="/submit/" method="post">
- <input type="text" id="title" name="title" placeholder="Title">
- <input type="text" id="context" name="context" placeholder="Context" style="height: 100px;">
- <button type="submit">Send</button>
- </form>
- </div>
- <script>
- document.getElementById('myForm').onsubmit = function(event) {
- var title = document.getElementById('title').value.trim();
- var context = document.getElementById('context').value.trim();
- if (!context) {
- alert('Context is a required field.');
- event.preventDefault(); // Prevent form submission
- } else if (!title) {
- document.getElementById('title').value = 'default';
- }
- };
- </script>
- </body>
- </html>
- """
- return html_content
- @app.post("/submit/")
- async def submit(title: str = Form(...), context: str = Form(...)):
- if not context:
- return {"error": "Context is required"}
-
- result = _send(title, context)
- return {"message": result}
- def _send(title, context):
- command = [
- 'curl',
- '-k',
- 'https://gotify.erhe.top/message?token=A0Xg6ZE5946iBYg',
- '-F', f'title={title}',
- '-F', f'message={context}',
- '-F', 'priority=5'
- ]
- # 执行命令
- try:
- result = subprocess.run(command, check=True, text=True, capture_output=True)
- print('命令执行成功,输出:')
- print(result.stdout)
- except subprocess.CalledProcessError as e:
- print('命令执行失败:')
- print(e.stderr)
- return result.stdout
- if __name__ == "__main__":
- import uvicorn
- uvicorn.run(app, host="0.0.0.0", port=8082)
|