...

Text file src/github.com/emissary-ingress/emissary/v3/docker/test-shadow/shadow.py

Documentation: github.com/emissary-ingress/emissary/v3/docker/test-shadow

     1#!/usr/bin/env python
     2
     3# Copyright 2018 Datawire. All rights reserved.
     4#
     5# Licensed under the Apache License, Version 2.0 (the "License");
     6# you may not use this file except in compliance with the License.
     7# You may obtain a copy of the License at
     8#
     9#     http://www.apache.org/licenses/LICENSE-2.0
    10#
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS,
    13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14# See the License for the specific language governing permissions and
    15# limitations under the License
    16
    17import sys
    18
    19import logging
    20import os
    21
    22from flask import Flask, Response, jsonify, request
    23
    24__version__ = "?.?.?"
    25
    26app = Flask(__name__)
    27
    28counts = {}
    29
    30@app.before_request
    31def before():
    32    logging.debug("=>> %s" % request)
    33
    34    for header in sorted(request.headers.keys()):
    35        logging.debug("=>>   %s: %s" % (header, request.headers[header]))
    36
    37@app.route('/clear/')
    38def clear():
    39    global counts
    40    counts = {}
    41
    42    resp = Response("CLEARED")
    43
    44    return resp
    45
    46@app.route('/mark/<count>')
    47def mark(count):
    48    c = counts.setdefault(count, 0)
    49    counts[count] = c + 1
    50
    51    resp = Response("COUNT %d" % counts[count])
    52    resp.headers['X-Shadowed'] = True
    53
    54    return resp
    55
    56@app.route('/check/')
    57def check():
    58    return jsonify(counts)
    59
    60if __name__ == "__main__":
    61    port = 3000
    62    ssl_context = None
    63    conn_type = "HTTP"
    64
    65    if len(sys.argv) > 1:
    66        __version__ = sys.argv[1]
    67
    68    if (len(sys.argv) > 2) and (sys.argv[2] == "--tls"):
    69        ssl_context = ('shadowsvc.crt', 'shadowsvc.key')
    70        conn_type = "HTTPS"
    71
    72    logging.basicConfig(
    73        level=logging.DEBUG,
    74        format="%%(asctime)s shadow %s %s %%(levelname)s: %%(message)s" % (__version__, conn_type),
    75        datefmt="%Y-%m-%d %H:%M:%S"
    76    )
    77
    78    logging.info("initializing using %s on port %d" % (conn_type, port))
    79    app.run(host='0.0.0.0', port=port, debug=True, ssl_context=ssl_context)

View as plain text