jack vor 1 Jahr
Commit
d35b197aea
3 geänderte Dateien mit 148 neuen und 0 gelöschten Zeilen
  1. 11 0
      Dockerfile
  2. 10 0
      docker-compose.yaml
  3. 127 0
      main.py

+ 11 - 0
Dockerfile

@@ -0,0 +1,11 @@
+FROM python:3.8
+
+WORKDIR /app
+
+COPY main.py /app
+
+RUN pip install --no-cache-dir fastapi uvicorn python-multipart
+
+EXPOSE 8082
+
+CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8082"]

+ 10 - 0
docker-compose.yaml

@@ -0,0 +1,10 @@
+version: '3.8'
+
+services:
+  send_gotify:
+    build: .
+    image: send_gotify
+    container_name: send_gotify
+    ports:
+      - "8082:8082"
+    restart: always

+ 127 - 0
main.py

@@ -0,0 +1,127 @@
+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)