Silicon Valley - Admin commited on
Commit
3dde046
1 Parent(s): dd07930

Refactor Dockerfile and server implementation for improved containerization and functionality

Browse files

- Updated Dockerfile to streamline the build process by removing the builder stage and directly installing dependencies from requirements.txt.
- Configured Hypercorn to bind to port 7860 and added WebSocket settings in hypercorn.toml.
- Enhanced README.md with updated server title, description, and API endpoints.
- Added health check endpoint to server.py and enabled CORS for better client interaction.
- Updated requirements.txt to include necessary packages for the application.

Files changed (5) hide show
  1. Dockerfile +15 -22
  2. README.md +18 -4
  3. hypercorn.toml +9 -4
  4. requirements.txt +4 -1
  5. server.py +13 -3
Dockerfile CHANGED
@@ -1,34 +1,27 @@
1
  # Dockerfile
2
- FROM python:3.11-slim as builder
3
-
4
- # Instala dependencias de compilaci贸n si es necesario
5
- RUN apt-get update && apt-get install -y build-essential
6
-
7
- # Instala poetry
8
- RUN pip install poetry
9
 
10
- # Copia archivos de la aplicaci贸n
11
  WORKDIR /app
12
- COPY pyproject.toml poetry.lock /app/
13
- COPY broker.py /app/broker.py
14
- COPY server.py /app/server.py
15
 
16
- # Instala dependencias
17
- RUN poetry install --no-dev --no-root
 
 
18
 
19
- # Imagen final
20
- FROM python:3.11-slim
21
-
22
- WORKDIR /app
 
23
 
24
- # Copia desde el builder
25
- COPY --from=builder /app /app
26
 
27
  # Variables de entorno
28
  ENV PYTHONUNBUFFERED=1
29
 
30
- # Expone el puerto
31
  EXPOSE 7860
32
 
33
- # Comando para ejecutar la aplicaci贸n
34
- CMD ["poetry", "run", "python", "server.py"]
 
1
  # Dockerfile
2
+ FROM python:3.11-slim
 
 
 
 
 
 
3
 
 
4
  WORKDIR /app
 
 
 
5
 
6
+ # Instalar dependencias del sistema si son necesarias
7
+ RUN apt-get update && apt-get install -y --no-install-recommends \
8
+ build-essential \
9
+ && rm -rf /var/lib/apt/lists/*
10
 
11
+ # Copiar archivos de la aplicaci贸n
12
+ COPY requirements.txt .
13
+ COPY broker.py .
14
+ COPY server.py .
15
+ COPY hypercorn.toml .
16
 
17
+ # Instalar dependencias de Python
18
+ RUN pip install --no-cache-dir -r requirements.txt
19
 
20
  # Variables de entorno
21
  ENV PYTHONUNBUFFERED=1
22
 
23
+ # Exponer el puerto que Hugging Face Spaces espera
24
  EXPOSE 7860
25
 
26
+ # Comando para ejecutar la aplicaci贸n con Hypercorn
27
+ CMD ["hypercorn", "--config", "hypercorn.toml", "server:app"]
README.md CHANGED
@@ -1,10 +1,24 @@
1
  ---
2
- title: Kaiobash Server
3
- emoji: 馃寲
4
- colorFrom: indigo
5
  colorTo: indigo
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Kaio API Server
3
+ emoji: 馃殌
4
+ colorFrom: blue
5
  colorTo: indigo
6
  sdk: docker
7
+ app_port: 7860
8
  pinned: false
9
  ---
10
 
11
+ # Kaio API Server
12
+
13
+ Este servidor proporciona una API REST y WebSocket para ejecutar comandos y gestionar archivos.
14
+
15
+ ## Endpoints
16
+
17
+ - REST API: `https://<space-name>.hf.space/`
18
+ - WebSocket: `wss://<space-name>.hf.space/session`
19
+
20
+ ## Estado
21
+
22
+ Puedes verificar el estado del servidor accediendo a:
23
+ - `https://<space-name>.hf.space/health`
24
+ - `https://<space-name>.hf.space/status`
hypercorn.toml CHANGED
@@ -1,9 +1,14 @@
1
  # Hypercorn configuration file
2
 
3
  # Server binding options
4
- bind = "0.0.0.0:8000"
5
- workers = 1
 
 
 
 
6
 
7
  # Logging configuration
8
- accesslog = "/var/log/serverwitch/access.log"
9
- logconfig = "logging.toml"
 
 
1
  # Hypercorn configuration file
2
 
3
  # Server binding options
4
+ bind = "0.0.0.0:7860"
5
+ workers = 1
6
+
7
+ # Websocket settings
8
+ websocket_ping_interval = 20
9
+ websocket_max_message_size = 16777216
10
 
11
  # Logging configuration
12
+ accesslog = "-"
13
+ errorlog = "-"
14
+ loglevel = "INFO"
requirements.txt CHANGED
@@ -1 +1,4 @@
1
-
 
 
 
 
1
+ quart>=0.18.4
2
+ quart-schema>=0.15.0
3
+ hypercorn>=0.14.3
4
+ websockets>=11.0.3
server.py CHANGED
@@ -3,11 +3,13 @@ import asyncio
3
  import uuid
4
  from typing import AsyncGenerator, Dict, Tuple, Any, Optional
5
  from dataclasses import dataclass
6
- from quart import Quart, websocket, request, FileResponse
7
  from quart_schema import QuartSchema, validate_request, validate_response
 
8
  import importlib.metadata
9
  import secrets
10
  import logging
 
11
 
12
  from broker import SessionBroker, SessionDoesNotExist, ClientRequest, ClientResponse, ClientError
13
 
@@ -16,8 +18,9 @@ TIMEOUT: int = 60
16
  LOG_LEVEL: int = logging.INFO
17
  TRUSTED_HOSTS: list[str] = ["127.0.0.1"]
18
 
19
- # Crear aplicaci贸n
20
  app = Quart(__name__)
 
21
  QuartSchema(app)
22
  app.logger.setLevel(LOG_LEVEL)
23
 
@@ -97,7 +100,14 @@ async def command(data: Command) -> Tuple[CommandResponse | ErrorResponse, int]:
97
 
98
  # Ejecutar aplicaci贸n
99
  def run():
100
- app.run(port=7860)
 
 
 
 
 
 
 
101
 
102
  if __name__ == "__main__":
103
  run()
 
3
  import uuid
4
  from typing import AsyncGenerator, Dict, Tuple, Any, Optional
5
  from dataclasses import dataclass
6
+ from quart import Quart, websocket, request, Response
7
  from quart_schema import QuartSchema, validate_request, validate_response
8
+ from quart_cors import cors
9
  import importlib.metadata
10
  import secrets
11
  import logging
12
+ import hypercorn.asyncio
13
 
14
  from broker import SessionBroker, SessionDoesNotExist, ClientRequest, ClientResponse, ClientError
15
 
 
18
  LOG_LEVEL: int = logging.INFO
19
  TRUSTED_HOSTS: list[str] = ["127.0.0.1"]
20
 
21
+ # Crear aplicaci贸n con CORS habilitado
22
  app = Quart(__name__)
23
+ app = cors(app, allow_origin="*")
24
  QuartSchema(app)
25
  app.logger.setLevel(LOG_LEVEL)
26
 
 
100
 
101
  # Ejecutar aplicaci贸n
102
  def run():
103
+ config = hypercorn.Config()
104
+ config.bind = ["0.0.0.0:7860"]
105
+ asyncio.run(hypercorn.asyncio.serve(app, config))
106
+
107
+ # Agregar un endpoint de health check
108
+ @app.route("/health")
109
+ async def health_check():
110
+ return {"status": "healthy"}
111
 
112
  if __name__ == "__main__":
113
  run()