Spaces:
Sleeping
Sleeping
Commit
·
750073f
1
Parent(s):
0527f34
initial project
Browse files- Dockerfile +25 -0
- main.py +45 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Install necessary dependencies
|
| 5 |
+
RUN apt-get update \
|
| 6 |
+
&& apt-get install -y openssl \
|
| 7 |
+
&& apt-get clean \
|
| 8 |
+
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
|
| 9 |
+
|
| 10 |
+
# Set working directory
|
| 11 |
+
WORKDIR /app
|
| 12 |
+
|
| 13 |
+
# Copy the FastAPI application code to the container
|
| 14 |
+
COPY requirements.txt .
|
| 15 |
+
|
| 16 |
+
# Install Python dependencies
|
| 17 |
+
RUN pip install -r requirements.txt
|
| 18 |
+
|
| 19 |
+
COPY . .
|
| 20 |
+
|
| 21 |
+
# Expose the HTTPS port
|
| 22 |
+
EXPOSE 443 5080
|
| 23 |
+
|
| 24 |
+
# Command to run FastAPI with SSL
|
| 25 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5080"]
|
main.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Depends, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import uvicorn
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# CORS Middleware: restrict access to only trusted origins
|
| 13 |
+
app.add_middleware(
|
| 14 |
+
CORSMiddleware,
|
| 15 |
+
allow_origins=["*"],
|
| 16 |
+
#allow_origins=["https://your-frontend-domain.com"],
|
| 17 |
+
#allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Fetch the secure token from environment variables
|
| 23 |
+
SECURE_TOKEN = os.getenv("SECURE_TOKEN")
|
| 24 |
+
PORT = os.getenv("PORT")
|
| 25 |
+
|
| 26 |
+
# Simple token-based authentication dependency
|
| 27 |
+
async def authenticate_token(token: str):
|
| 28 |
+
if token != f"Bearer {SECURE_TOKEN}":
|
| 29 |
+
raise HTTPException(status_code=401, detail="Invalid token")
|
| 30 |
+
|
| 31 |
+
# WebSocket endpoint
|
| 32 |
+
@app.websocket("/ws")
|
| 33 |
+
async def websocket_endpoint(websocket: WebSocket, token: str = Depends(authenticate_token)):
|
| 34 |
+
await websocket.accept()
|
| 35 |
+
try:
|
| 36 |
+
while True:
|
| 37 |
+
data = await websocket.receive_text()
|
| 38 |
+
print(f"Message from client: {data}")
|
| 39 |
+
await websocket.send_text(f"Server received: {data}")
|
| 40 |
+
except WebSocketDisconnect:
|
| 41 |
+
print("Client disconnected")
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
# Run the server without TLS (insecure)
|
| 45 |
+
uvicorn.run("main:app", host="0.0.0.0", port=PORT)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-dotenv==1.0.1
|
| 2 |
+
fastapi==0.115.2
|
| 3 |
+
uvicorn==0.31.1
|
| 4 |
+
websockets==13.1
|