diff options
-rw-r--r-- | templating/README.md | 70 | ||||
-rwxr-xr-x | templating/templating.py | 3 | ||||
-rw-r--r-- | web/templates/test.conf | 17 |
3 files changed, 89 insertions, 1 deletions
diff --git a/templating/README.md b/templating/README.md new file mode 100644 index 0000000..bea1c0a --- /dev/null +++ b/templating/README.md @@ -0,0 +1,70 @@ +# Templating Engine + +This engine does the templating for The Gathering. + +The flask server sits behind varnish and waits for incomming GET requests with a template name and optional variables. + +```varnish +backend templating { + .host = "::1"; + .port = "8081"; +} +.... +if (req.url ~ "^/api/templates") { + set req.url = regsub(req.url,"^/api/templates",""); + set req.backend_hint = templating; +} +``` + +## Requirements + +* Python3 +* jinja2 +* requests +* flask +* netaddr + +## Settings + +``` +python3 templating.py +usage: templating.py [-t TEMPLATES [TEMPLATES ...]] [-h HOST] [-p PORT] [-d] + [-s SERVER] [-x TIMEOUT] + +Process templates for gondul. + +optional arguments: + -t TEMPLATES [TEMPLATES ...], --templates TEMPLATES [TEMPLATES ...] + location of templates + -h HOST, --host HOST host address + -p PORT, --port PORT host port + -d, --debug enable debug mode + -s SERVER, --server SERVER + gondul server address + -x TIMEOUT, --timeout TIMEOUT + gondul server timeout +``` + +## How to test locally + +You need a directory with all the jinja2 templates. I just assume you git cloned the entire gondul repo. + +An example using [test.conf](../web/templates/test.conf) + +```bash +python3 templating.py --host ::1 --port 8081 --templates ../web/templates --server http://tech:rules@<gondul>:80 +``` + +```bash +curl -s "http://[::1]:8081/test.conf?switch=e1-1" | jq . +{ + "distro_name": "core-dev", + "placement": { + "height": 20, + "width": 250, + "x": "830", + "y": "620" + }, + "tags": [] +} +``` diff --git a/templating/templating.py b/templating/templating.py index 140745f..603b370 100755 --- a/templating/templating.py +++ b/templating/templating.py @@ -88,12 +88,13 @@ parser.add_argument("-h", "--host", type=str, default="127.0.0.1", help="host ad parser.add_argument("-p", "--port", type=int, default=8080, help="host port") parser.add_argument("-d", "--debug", action="store_true", help="enable debug mode") parser.add_argument("-s", "--server", type=str, default="http://localhost:80", help="gondul server address") -parser.add_argument("-x", "--timeout", type=int, default=1, help="gondul server timeout") +parser.add_argument("-x", "--timeout", type=int, default=2, help="gondul server timeout") args = parser.parse_args() env.loader.searchpath = args.templates if not sys.argv[1:]: parser.print_help() + sys.exit(1) app.run(host=args.host, port=args.port, debug=args.debug) diff --git a/web/templates/test.conf b/web/templates/test.conf new file mode 100644 index 0000000..ce2fe0d --- /dev/null +++ b/web/templates/test.conf @@ -0,0 +1,17 @@ +{# Fetches something simple from gondul #} +{# Query parameters: ?switch=e1-1 #} + +{# Check if ?switch option is given#} +{% if not options["switch"] %} + {# pretty print public/switches endpoint#} + {{ objects["public/switches"] | pprint }} +{% else %} + {# sets sw variable using the query parameter #} + {% set sw = options["switch"] %} +{% endif %} +{% if sw %} + {# find the correct switch from public/switches api endpoint using query paramter#} + {% set switch = objects["public/switches"].switches[sw] %} + {# pretty print info in api #} + {{ switch | tojson }} +{% endif %} |