The web server

ASAB provides a web server in a asab.web module. This module offers an integration of a aiohttp web server.

  1. Before you start, make sure that you have aiohttp module installed.
$ pip3 install aiohttp
  1. The following code creates a simple web server application
#!/usr/bin/env python3
import asab
import aiohttp

class MyApplication(asab.Application):

    async def initialize(self):
        # Load the web service module
        from asab.web import Module
        self.add_module(Module)

        # Locate the web service
        websvc = self.get_service("asab.WebService")

        # Create a container
        container = asab.web.WebContainer(websvc, 'example:web', config={"listen": "0.0.0.0:8080"})

        # Add a route
        container.WebApp.router.add_get('/hello', self.hello)

    # Simplistic view
    async def hello(self, request):
        return aiohttp.web.Response(text='Hello!\n')

if __name__ == '__main__':
    app = MyApplication()
    app.run()
  1. Test it with curl
$ curl http://localhost:8080/hello
Hello!

Web Service

class asab.web.service.WebService

Service localization example:

from asab.web import Module
self.add_module(Module)
svc = self.get_service("asab.WebService")
WebService.Webapp

An instance of a aiohttp.web.Application class.

svc.WebApp.router.add_get('/hello', self.hello)

Configuration

TODO: Listen at 0.0.0.0:80

Sessions

ASAB Web Service provides an implementation of the web sessions.

class asab.web.session.ServiceWebSession

TODO: …

asab.web.session.session_middleware(storage)

TODO: …