|
| 1 | +# Copyright 2019 Google, LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from flask import Flask, make_response, request |
| 16 | +import os |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | + |
| 20 | + |
| 21 | +app = Flask(__name__) |
| 22 | + |
| 23 | + |
| 24 | +# [START run_system_package_handler] |
| 25 | +@app.route("/diagram.png", methods=["GET"]) |
| 26 | +def index(): |
| 27 | + # Takes an HTTP GET request with query param dot and |
| 28 | + # returns a png with the rendered DOT diagram in a HTTP response. |
| 29 | + try: |
| 30 | + image = create_diagram(request.args.get("dot")) |
| 31 | + response = make_response(image) |
| 32 | + response.headers.set("Content-Type", "image/png") |
| 33 | + return response |
| 34 | + |
| 35 | + except Exception as e: |
| 36 | + print("error: {}".format(e)) |
| 37 | + |
| 38 | + # Flush the stdout to avoid log buffering. |
| 39 | + sys.stdout.flush() |
| 40 | + |
| 41 | + # If no graphviz definition or bad graphviz def, return 400 |
| 42 | + if "syntax" in str(e): |
| 43 | + return "Bad Request: {}".format(e), 400 |
| 44 | + |
| 45 | + return "Internal Server Error", 500 |
| 46 | + |
| 47 | + |
| 48 | +# [END run_system_package_handler] |
| 49 | + |
| 50 | + |
| 51 | +# [START run_system_package_exec] |
| 52 | +def create_diagram(dot): |
| 53 | + # Generates a diagram based on a graphviz DOT diagram description. |
| 54 | + if not dot: |
| 55 | + raise Exception("syntax: no graphviz definition provided") |
| 56 | + |
| 57 | + dot_args = [ # These args add a watermark to the dot graphic. |
| 58 | + "-Glabel=Made on Cloud Run", |
| 59 | + "-Gfontsize=10", |
| 60 | + "-Glabeljust=right", |
| 61 | + "-Glabelloc=bottom", |
| 62 | + "-Gfontcolor=gray", |
| 63 | + "-Tpng", |
| 64 | + ] |
| 65 | + |
| 66 | + # Uses local `dot` binary from Graphviz: |
| 67 | + # https://graphviz.gitlab.io |
| 68 | + image = subprocess.run( |
| 69 | + ["dot"] + dot_args, input=dot.encode("utf-8"), stdout=subprocess.PIPE |
| 70 | + ).stdout |
| 71 | + |
| 72 | + if not image: |
| 73 | + raise Exception("syntax: bad graphviz definition provided") |
| 74 | + return image |
| 75 | + |
| 76 | + |
| 77 | +# [END run_system_package_exec] |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + PORT = int(os.getenv("PORT")) if os.getenv("PORT") else 8080 |
| 82 | + |
| 83 | + # This is used when running locally. Gunicorn is used to run the |
| 84 | + # application on Cloud Run. See entrypoint in Dockerfile. |
| 85 | + app.run(host="127.0.0.1", port=PORT, debug=True) |
0 commit comments