Spaces:
Sleeping
Sleeping
Commit
·
c30d351
1
Parent(s):
dc6950f
fix: startup process
Browse files- Dockerfile +11 -20
- main.py +17 -6
Dockerfile
CHANGED
@@ -1,25 +1,16 @@
|
|
1 |
-
#
|
2 |
-
FROM python:3.10
|
3 |
|
4 |
-
#
|
5 |
-
|
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 |
-
#
|
17 |
-
|
18 |
|
19 |
-
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
1 |
+
# Use the official Python 3.10.9 image
|
2 |
+
FROM python:3.10.9
|
3 |
|
4 |
+
# Copy the current directory contents into the container at .
|
5 |
+
COPY . .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Set the working directory to /
|
8 |
+
WORKDIR /
|
9 |
|
10 |
+
# Install requirements.txt
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
12 |
|
13 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
14 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
15 |
|
16 |
+
EXPOSE 7860
|
|
main.py
CHANGED
@@ -1,4 +1,4 @@
|
|
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
|
@@ -6,6 +6,10 @@ import os
|
|
6 |
|
7 |
# Load environment variables from .env file
|
8 |
load_dotenv()
|
|
|
|
|
|
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
@@ -19,15 +23,20 @@ app.add_middleware(
|
|
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)):
|
@@ -40,6 +49,8 @@ async def websocket_endpoint(websocket: WebSocket, token: str = Depends(authenti
|
|
40 |
except WebSocketDisconnect:
|
41 |
print("Client disconnected")
|
42 |
|
|
|
|
|
|
|
43 |
if __name__ == "__main__":
|
44 |
-
|
45 |
-
uvicorn.run("main:app", host="0.0.0.0", port=PORT)
|
|
|
1 |
+
from fastapi import FastAPI, Response, WebSocket, WebSocketDisconnect, Depends, HTTPException
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
import uvicorn
|
4 |
from dotenv import load_dotenv
|
|
|
6 |
|
7 |
# Load environment variables from .env file
|
8 |
load_dotenv()
|
9 |
+
IS_DEV = os.environ.get('ENV', 'DEV') != 'PROD'
|
10 |
+
SECURE_TOKEN = os.getenv("SECURE_TOKEN")
|
11 |
+
X_REQUEST_USER = os.environ.get('X_REQUEST_USER')
|
12 |
+
X_API_KEY = os.environ.get('X_API_KEY')
|
13 |
|
14 |
app = FastAPI()
|
15 |
|
|
|
23 |
allow_headers=["*"],
|
24 |
)
|
25 |
|
|
|
|
|
|
|
26 |
|
27 |
# Simple token-based authentication dependency
|
28 |
async def authenticate_token(token: str):
|
29 |
if token != f"Bearer {SECURE_TOKEN}":
|
30 |
raise HTTPException(status_code=401, detail="Invalid token")
|
31 |
|
32 |
+
@app.get("/")
|
33 |
+
def root():
|
34 |
+
return Response(status=200, data='ok')
|
35 |
+
|
36 |
+
@app.get("/health")
|
37 |
+
def healthcheck():
|
38 |
+
return Response(status=200, data='ok')
|
39 |
+
|
40 |
# WebSocket endpoint
|
41 |
@app.websocket("/ws")
|
42 |
async def websocket_endpoint(websocket: WebSocket, token: str = Depends(authenticate_token)):
|
|
|
49 |
except WebSocketDisconnect:
|
50 |
print("Client disconnected")
|
51 |
|
52 |
+
def is_valid(u, p):
|
53 |
+
return u == X_REQUEST_USER and p == X_API_KEY
|
54 |
+
|
55 |
if __name__ == "__main__":
|
56 |
+
uvicorn.run('app:app', host='0.0.0.0', port=7860, reload=True)
|
|