|
| 1 | +"""Mock Dispatch server for use in test environments. |
| 2 | +
|
| 3 | +Usage: |
| 4 | + dispatch.test <endpoint> [--api-key=<key>] [--hostname=<name>] [--port=<port>] [-v | --verbose] |
| 5 | + dispatch.test -h | --help |
| 6 | +
|
| 7 | +Options: |
| 8 | + --api-key=<key> API key to require when clients connect to the server [default: test]. |
| 9 | +
|
| 10 | + --hostname=<name> Hostname to listen on [default: 127.0.0.1]. |
| 11 | + --port=<port> Port to listen on [default: 4450]. |
| 12 | +
|
| 13 | + -v --verbose Show verbose details in the log. |
| 14 | + -h --help Show this help information. |
| 15 | +""" |
| 16 | + |
| 17 | +import base64 |
| 18 | +import logging |
| 19 | +import os |
| 20 | +import sys |
| 21 | + |
| 22 | +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey |
| 23 | +from docopt import docopt |
| 24 | + |
| 25 | +from dispatch.test import DispatchServer, DispatchService, EndpointClient |
| 26 | + |
| 27 | + |
| 28 | +def main(): |
| 29 | + args = docopt(__doc__) |
| 30 | + |
| 31 | + if args["--help"]: |
| 32 | + print(__doc__) |
| 33 | + exit(0) |
| 34 | + |
| 35 | + endpoint = args["<endpoint>"] |
| 36 | + api_key = args["--api-key"] |
| 37 | + hostname = args["--hostname"] |
| 38 | + port_str = args["--port"] |
| 39 | + |
| 40 | + try: |
| 41 | + port = int(port_str) |
| 42 | + except ValueError: |
| 43 | + print(f"error: invalid port: {port_str}", file=sys.stderr) |
| 44 | + exit(1) |
| 45 | + |
| 46 | + if not os.getenv("NO_COLOR"): |
| 47 | + logging.addLevelName(logging.WARNING, f"\033[1;33mWARN\033[1;0m") |
| 48 | + logging.addLevelName(logging.ERROR, "\033[1;31mERROR\033[1;0m") |
| 49 | + |
| 50 | + logger = logging.getLogger() |
| 51 | + if args["--verbose"]: |
| 52 | + logger.setLevel(logging.DEBUG) |
| 53 | + fmt = "%(asctime)s [%(levelname)s] %(name)s - %(message)s" |
| 54 | + else: |
| 55 | + logger.setLevel(logging.INFO) |
| 56 | + fmt = "%(asctime)s [%(levelname)s] %(message)s" |
| 57 | + logging.getLogger("httpx").disabled = True |
| 58 | + |
| 59 | + log_formatter = logging.Formatter(fmt=fmt, datefmt="%Y-%m-%d %H:%M:%S") |
| 60 | + log_handler = logging.StreamHandler(sys.stderr) |
| 61 | + log_handler.setFormatter(log_formatter) |
| 62 | + logger.addHandler(log_handler) |
| 63 | + |
| 64 | + # This private key was generated randomly. |
| 65 | + signing_key = Ed25519PrivateKey.from_private_bytes( |
| 66 | + b"\x0e\xca\xfb\xc9\xa9Gc'fR\xe4\x97y\xf0\xae\x90\x01\xe8\xd9\x94\xa6\xd4@\xf6\xa7!\x90b\\!z!" |
| 67 | + ) |
| 68 | + verification_key = base64.b64encode( |
| 69 | + signing_key.public_key().public_bytes_raw() |
| 70 | + ).decode() |
| 71 | + |
| 72 | + endpoint_client = EndpointClient.from_url(endpoint, signing_key=signing_key) |
| 73 | + |
| 74 | + with DispatchService(endpoint_client, api_key=api_key) as service: |
| 75 | + with DispatchServer(service, hostname=hostname, port=port) as server: |
| 76 | + print(f"Spawned a mock Dispatch server on {hostname}:{port}") |
| 77 | + print() |
| 78 | + print(f"Dispatching function calls to the endpoint at {endpoint}") |
| 79 | + print() |
| 80 | + print("The Dispatch SDK can be configured with:") |
| 81 | + print() |
| 82 | + print(f' export DISPATCH_API_URL="http://{hostname}:{port}"') |
| 83 | + print(f' export DISPATCH_API_KEY="{api_key}"') |
| 84 | + print(f' export DISPATCH_ENDPOINT_URL="{endpoint}"') |
| 85 | + print(f' export DISPATCH_VERIFICATION_KEY="{verification_key}"') |
| 86 | + print() |
| 87 | + |
| 88 | + server.wait() |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + main() |
0 commit comments