Пример #1
0
def start(app: PaywalledProxy, host: str, port: int, elasticsearch: str):
    auth = os.environ.get('ES_CREDENTIALS')
    if auth is None:
        print(
            'Please provide elasticsearch credentials via ES_CREDENTIALS environment.'
        )
        sys.exit(1)
    elasticsearch_connection = Elasticsearch(elasticsearch,
                                             timeout=30,
                                             http_auth=auth)
    backend = ElasticsearchBackend(elasticsearch_connection)
    APIServer(app, backend)
    app.run(host=host, port=port, debug=True)
    app.join()
if __name__ == '__main__':
    private_key = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    tempfile = os.path.join(os.path.expanduser('~'), '.raiden/echo_server.pkl')
    # set up a paywalled proxy
    # arguments are:
    #  - channel manager contract
    #  - private key to use for receiving funds
    #  - temporary file for storing state information (balance proofs)
    app = PaywalledProxy(CHANNEL_MANAGER_ADDRESS, private_key, tempfile)

    # Add resource defined by regex and with a fixed price of 1 token
    # We setup the resource to return whatever is supplied as a second argument
    #  in the URL.
    app.add_content(
        PaywalledContent("echofix\/[a-zA-Z0-9]+", 1, lambda request:
                         (request.split("/")[1], 200)))
    # Resource with a price determined by the second parameter
    app.add_content(
        PaywalledContent("echodyn\/[0-9]+",
                         lambda request: int(request.split("/")[1]),
                         lambda request: (int(request.split("/")[1]), 200)))
    app.add_content(
        PaywalledProxyUrl(
            "p\/[0-9]+", 1,
            lambda request: 'google.com/search?q=' + request.split("/")[1]))
    # start the app. proxy is a WSGI greenlet, so you must join it properly
    app.run(debug=True)
    app.join()
    # now use echo_client to get the resources