bibibi12345 commited on
Commit
4d82260
·
1 Parent(s): 1ad60f7
Files changed (4) hide show
  1. Dockerfile +27 -0
  2. README.md +79 -7
  3. app.py +43 -0
  4. requirements.txt +1 -0
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the requirements file into the container at /app
8
+ COPY requirements.txt .
9
+
10
+ # Install any needed packages specified in requirements.txt
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Copy the current directory contents into the container at /app
14
+ COPY . .
15
+
16
+ # Make port 7860 available to the world outside this container
17
+ # Hugging Face Spaces expects the application to run on port 7860
18
+ EXPOSE 7860
19
+
20
+ # Define environment variable
21
+ ENV NAME World
22
+
23
+ # Run app.py when the container launches
24
+ # Use gunicorn for a more robust production server (optional, but recommended)
25
+ # CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
26
+ # Fallback to Flask's built-in server for simplicity
27
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,11 +1,83 @@
1
  ---
2
- title: Print
3
- emoji: 🦀
4
- colorFrom: purple
5
- colorTo: gray
6
  sdk: docker
7
- pinned: false
8
- license: apache-2.0
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Simple Request Logger
3
+ emoji: 📝
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: docker
7
+ app_port: 7860
 
8
  ---
9
 
10
+ # Simple Request Logging Server (Hugging Face Compatible)
11
+
12
+ This repository contains a simple Flask application designed to run in a Docker container. It listens for incoming POST requests on the root path (`/`) and logs the request headers and body (JSON or raw) to the standard output. This is useful for debugging or inspecting webhook payloads.
13
+
14
+ The application is configured to run on port `7860`, which is commonly used for Hugging Face Spaces deployments.
15
+
16
+ ## Files
17
+
18
+ * `app.py`: The main Flask application code.
19
+ * `requirements.txt`: Lists the Python dependencies (Flask).
20
+ * `Dockerfile`: Instructions for building the Docker image.
21
+ * `README.md`: This file.
22
+
23
+ ## Prerequisites
24
+
25
+ * Docker installed on your system.
26
+
27
+ ## Build the Docker Image
28
+
29
+ Navigate to the directory containing the `Dockerfile` and run the following command:
30
+
31
+ ```bash
32
+ docker build -t request-logger .
33
+ ```
34
+
35
+ ## Run the Docker Container
36
+
37
+ To run the container and map the internal port `7860` to a port on your host machine (e.g., `7860`), use:
38
+
39
+ ```bash
40
+ docker run -p 7860:7860 request-logger
41
+ ```
42
+
43
+ The server will start, and you should see output indicating it's listening on `0.0.0.0:7860`.
44
+
45
+ ## Sending Requests
46
+
47
+ You can send POST requests to the server using tools like `curl` or any HTTP client.
48
+
49
+ **Example using `curl` (JSON payload):**
50
+
51
+ ```bash
52
+ curl -X POST http://localhost:7860/ \
53
+ -H "Content-Type: application/json" \
54
+ -H "X-Custom-Header: MyValue" \
55
+ -d '{"key": "value", "message": "Hello Server!"}'
56
+ ```
57
+
58
+ **Example using `curl` (Form data):**
59
+
60
+ ```bash
61
+ curl -X POST http://localhost:7860/ \
62
+ -H "Content-Type: application/x-www-form-urlencoded" \
63
+ -d "param1=value1&param2=value2"
64
+ ```
65
+
66
+ **Example using `curl` (Plain text):**
67
+
68
+ ```bash
69
+ curl -X POST http://localhost:7860/ \
70
+ -H "Content-Type: text/plain" \
71
+ -d "This is a plain text body."
72
+ ```
73
+
74
+ ## Viewing Logs
75
+
76
+ The request details (headers and body) will be printed to the terminal where you ran the `docker run` command.
77
+
78
+ ## Deployment on Hugging Face Spaces
79
+
80
+ 1. Create a new Space on Hugging Face ([https://huggingface.co/new-space](https://huggingface.co/new-space)).
81
+ 2. Choose "Docker" as the Space SDK. The settings in this `README.md` file (like `sdk: docker` and `app_port: 7860`) will configure the Space automatically.
82
+ 3. Upload or push (`git`) the `app.py`, `requirements.txt`, `Dockerfile`, and this updated `README.md` to your Space repository.
83
+ 4. Hugging Face will automatically build the Docker image and deploy the application using the information from the `Dockerfile` and the YAML header in this README. The application will be accessible at `https://<your-username>-<your-space-name>.hf.space/`. Remember that requests should be sent to the root path (`/`).
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import logging
3
+ import sys
4
+
5
+ # Configure logging to output to stdout
6
+ logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
7
+
8
+ app = Flask(__name__)
9
+
10
+ @app.route('/', methods=['POST'])
11
+ def handle_request():
12
+ """Handles incoming POST requests, logs details, and returns a response."""
13
+ logging.info("="*20 + " New Request Received " + "="*20)
14
+
15
+ # Log headers
16
+ logging.info("Headers:")
17
+ for header, value in request.headers.items():
18
+ logging.info(f" {header}: {value}")
19
+
20
+ # Log JSON body if present
21
+ content_type = request.headers.get('Content-Type')
22
+ if content_type and 'application/json' in content_type:
23
+ try:
24
+ json_data = request.get_json()
25
+ logging.info("JSON Body:")
26
+ logging.info(json_data)
27
+ except Exception as e:
28
+ logging.error(f"Error parsing JSON: {e}")
29
+ logging.info("Raw Body (JSON parsing failed):")
30
+ logging.info(request.get_data(as_text=True))
31
+ else:
32
+ # Log raw body if not JSON
33
+ logging.info("Raw Body:")
34
+ logging.info(request.get_data(as_text=True))
35
+
36
+ logging.info("="*20 + " End of Request " + "="*20 + "\n")
37
+
38
+ return jsonify({"status": "success", "message": "Request received and logged."}), 200
39
+
40
+ if __name__ == '__main__':
41
+ # Hugging Face Spaces typically expect the app on port 7860
42
+ # Using 0.0.0.0 to be accessible externally within the container
43
+ app.run(host='0.0.0.0', port=7860)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ Flask==3.0.3