diff --git a/Docker Test/Dockerfile b/Docker Test/Dockerfile new file mode 100644 index 0000000..d924b8a --- /dev/null +++ b/Docker Test/Dockerfile @@ -0,0 +1,17 @@ +# Use an official Python runtime as a parent image +FROM python:3.10-slim + +# Set the working directory inside the container +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY ./mock /app + +# Install any necessary dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Expose port 8080 for the container to listen on +EXPOSE 8080 + +# Command to run the Python server when the container starts +CMD ["python", "server.py"] diff --git a/Docker Test/docker-compose.yaml b/Docker Test/docker-compose.yaml new file mode 100644 index 0000000..e5e6288 --- /dev/null +++ b/Docker Test/docker-compose.yaml @@ -0,0 +1,10 @@ +services: + test-http-container: + container_name: test-http-container + ports: + - 8080:8080 + volumes: + - ./mock:/app + build: . + networks: + - testnet diff --git a/Docker Test/mock/requirements.txt b/Docker Test/mock/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/Docker Test/mock/server.py b/Docker Test/mock/server.py new file mode 100644 index 0000000..3af126c --- /dev/null +++ b/Docker Test/mock/server.py @@ -0,0 +1,44 @@ +import json +import signal +import sys +from http.server import BaseHTTPRequestHandler, HTTPServer + +# Function to generate the response +def send_funct(): + to_send = { + "body": "Hello this is a test" + } + return json.dumps(to_send) + +# Define request handler +class RequestHandler(BaseHTTPRequestHandler): + def do_GET(self): + # Send response headers + self.send_response(200) + self.send_header('Content-type', 'application/json') + self.end_headers() + + # Send the response body (the JSON data) + self.wfile.write(send_funct().encode('utf-8')) + +# Graceful shutdown handler +def signal_handler(sig, frame): + print("\nShutting down server gracefully...") + sys.exit(0) + +# Set up and start the server +if __name__ == "__main__": + # Register signal handler for graceful shutdown (e.g., Ctrl+C) + signal.signal(signal.SIGINT, signal_handler) + + # Set the server address (localhost) and port (8080) + server_address = ('', 8080) + httpd = HTTPServer(server_address, RequestHandler) + + print("Server started on port 8080") + try: + # Start the server and listen for requests + httpd.serve_forever() + except KeyboardInterrupt: + # Server shutdown is handled in signal_handler + pass