diff --git a/containers/docker-compose.yaml b/containers/docker-compose.yaml index e76ad35..eab86e8 100644 --- a/containers/docker-compose.yaml +++ b/containers/docker-compose.yaml @@ -5,6 +5,12 @@ services: dockerfile: graphviz.Dockerfile volumes: - ../cache:/data + graphviz-server: + build: + context: ./dockerfiles/graphviz-server + dockerfile: Dockerfile + ports: + - 8080:8080 apache: image: httpd:latest volumes: diff --git a/containers/dockerfiles/graphviz-server/Dockerfile b/containers/dockerfiles/graphviz-server/Dockerfile new file mode 100644 index 0000000..ffe4eb4 --- /dev/null +++ b/containers/dockerfiles/graphviz-server/Dockerfile @@ -0,0 +1,20 @@ +# Use an official Python runtime as a parent image +FROM python:3.10-slim + +# Set the working directory in the container +WORKDIR /app + +# Install graphviz +RUN apt-get update && apt-get install -y graphviz && rm -rf /var/lib/apt/lists/* + +# Copy the current directory contents into the container at /app +COPY . /app + +# Install any needed packages specified in requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +# Define environment variable +ENV NAME World + +# Run app.py when the container launches +CMD ["python", "server.py"] diff --git a/containers/dockerfiles/graphviz-server/requirements.txt b/containers/dockerfiles/graphviz-server/requirements.txt new file mode 100644 index 0000000..bd506ad --- /dev/null +++ b/containers/dockerfiles/graphviz-server/requirements.txt @@ -0,0 +1,2 @@ +Flask +graphviz diff --git a/containers/dockerfiles/graphviz-server/server.py b/containers/dockerfiles/graphviz-server/server.py new file mode 100644 index 0000000..3080c1f --- /dev/null +++ b/containers/dockerfiles/graphviz-server/server.py @@ -0,0 +1,24 @@ +from flask import Flask, request, send_file +import graphviz + +app = Flask(__name__) + +@app.route('/graph', methods=['POST']) +def graph(): + # Get the dot format graph from the request + dot_string = request.data.decode() + + try: + # Create a graph from the dot string + dot = graphviz.Source(dot_string) + # Render the graph to a file + output_path = '/tmp/graph' + dot.render(output_path, format='png', cleanup=True) + + # Send the file back + return send_file(output_path + '.png', mimetype='image/png') + except Exception as e: + return str(e), 400 + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0', port=8080) diff --git a/containers/graphviz-client.py b/containers/graphviz-client.py new file mode 100644 index 0000000..d5de575 --- /dev/null +++ b/containers/graphviz-client.py @@ -0,0 +1,25 @@ +import requests + +# URL of the graph endpoint +url = 'http://localhost:8080/graph' + +# DOT language description of the graph +dot_text = """ + digraph G { + A -> B; + B -> C; + C -> A; + } +""" + +# Send the DOT string as a POST request to the server +response = requests.post(url, data=dot_text) + +# Check if the request was successful +if response.status_code == 200: + # Save the image + with open('output_graph.png', 'wb') as f: + f.write(response.content) + print("Graph image saved as 'output_graph.png'.") +else: + print("Failed to generate graph:", response.text) diff --git a/requirements.txt b/requirements.txt index 8ab5d5e..47ec99e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pyyaml networkx +requests