Application

Application is where components combies together. First application define in clink.Application class. To create an HTTP APIs, you must create application.

Every application implement clink.IWsgi. It is an WSGI and allow application run on WSGI server.

Creation Steps

Step 2: Get WSGI server

Here, we use built-in WSGI server in Python. You can chose other servers.

Step 3: Create application

clink.AppConf is an component, however others components may depends on it, so it MUST provide as a argument of clink.App constructor.

Step 4: Define components

This step defines components, it MUST be marked by clink.com.

We define an controller, it MUST be extend from clink.Controller to help application get knowledge about it. mapper.path(‘book’) and mapper.get(‘item’) specifies that: BookCtl.get_item will be call if client perform request GET /book/item.

Step 5: Add components to application

Simply, add type of components to application, then application knows “How to create it?. How to bring them togeter?”, etc.

Step 6: Load components

Application creates instance of components, solve depedency of components, etc. It ensure that everythings are ready to runs.

Step 7: Serve application in WSGI server

We create an WSGI server, specify address and port to bind and make it available on network.

Example

app.py
# STEP 1: get clink library
from clink import stamp, mapper, App, AppConf, Controller


# STEP 2: get an WSGI server
from wsgiref.simple_server import make_server


# STEP 3: create application
conf = AppConf('book-api')
app = App(conf)


# STEP 4: define controller
@stamp()
@mapper.path('/book')
class BookCtl(Controller):
    @mapper.get('/item')
    def get_item(self, req, res):
        res.body = {
            'name': 'How to Die',
            'author': 'Death'
        }


# STEP 5: add controller to application
app.add_ctl(BookCtl)


# STEP 6: load components
app.load()


# STEP 7: serve application on WSGI server
address = 'localhost'
port = 8080
print('Prepare API on http://%s:%i/book/item' % (address, port))
httpd = make_server(address, port, app)
httpd.serve_forever()

Testing

$ python app.py &> /dev/null &
[1] 5946

$ curl localhost:8080/book/item
{"author": "Death", "name": "How to Die"}

$ kill %1