Before starting, please follow the microraiden installation instructions and the instructions to set up geth.
In this tutorial we will create a simple paywalled server that will
echo a requested path paramter, payable with a custom token. You can find example code for this tutorial
in microraiden/examples/echo_server.py
.
Please refer to README.md to install all required dependencies. You will also need a Chrome browser with the MetaMask plugin.
from microraiden.make_helpers import make_paywalled_proxy
app = make_paywalled_proxy(private_key, state_file_name)
microraiden.make_helpers.make_paywalled_proxy()
is a helper that handles the setup of the channel manager
and returns a microraiden.proxy.paywalled_proxy.PaywalledProxy
instance.
Microraiden also includes other helpers that parse common commandline options. We are not using them in this example - for a quick overview how to use them,
refer to i.e. microraiden.examples.demo_proxy.__main__()
The channel manager will start syncing with the blockchain immediately.
Now we will create a custom resource class that simply echoes a path-parameter of the user’s request for a fixed price.
The workflow is the same as with the Flask-restful: Subclass microraiden.proxy.resources.Expensive
and
implement the HTTP methods you want to expose.
from microraiden.proxy.resources import Expensive
class StaticPriceResource(Expensive):
def get(self, url: str, param: str):
log.info('Resource requested: {} with param "{}"'.format(request.url, param))
return param
We add one static resource to our PaywalledProxy
instance.
The url argument will comply with standard flask routing rules.
app.add_paywalled_resource(
cls=StaticPriceResource,
url="/echofix/<string:param>",
price=5
)
The resource will then be available for example at the URI /echofix/foo
. Only after a
payment of 5 tokens, the proxy will send the foo
parameter back to the user and will
set the Content-Type header appropriately. Without payment, the
server responds with 402 Payment Required
.
A probably more useful paywalled resource is a URL. This is useful to fetch content from a remote CDN:
from microraiden.proxy.content import PaywalledProxyUrl
app.add_paywalled_resource(
cls=PaywalledProxyUrl,
url="cdn\/.*",
resource_class_kwargs={"domain": 'http://cdn.myhost.com:8000/resource42'}
)
Note, that the kwargs for the constructor of the resource-class (here our PaywalledProxyUrl
)
have to be passed as a dict with the resource_class_kwargs
argument.
In this case, the domain
kwarg is the remote URL specifying where to fetch the content from.
We can also construct the Resource in a way that the price will be dynamically calculated, e.g. based on the requests parameters.
class DynamicPriceResource(Expensive):
def get(self, url: str, param: str):
log.info('Resource requested: {} with param "{}"'.format(request.url, param))
return param
def price(self):
return len(request.view_args['param'])
app.add_paywalled_resource(
cls=DynamicPriceResource,
url="/echodyn/<string:param>",
)
Here, the price to be paid is the length of the requested string.
A request of the /echodyn/foo
resource, would therefore require a payment of 3 tokens.
You start proxy by calling run()
method. This call is non-blocking
– the proxy is started as a WSGI greenlet. Use join()
to sync with
the task. This will block until proxy has stopped. To terminate the
server, call stop() from another greenlet.
app.run(debug=True)
app.join()
To access the content with your browser, navigate to the URL of the resource you’d like to get. You’ll be faced with a paywall – a site requesting you to pay for the resource. To do so, you first have to open a new channel. If you have the MetaMask extension installed, you can set the amount to be deposited to the channel. After confirming the deposit, you can navigate and payments will be done automatically.
Off-chain transactions are stored in a sqlite database. You should do regular backups of this file – it contains balance signatures of the client, and if you lose them, you will have no way of proving that the client is settling the channel using less funds than he has actually paid to the proxy.