"""
    Classes and function for creating and starting web server
"""
import asyncio
from aiohttp import web

from command_line import define, parse_command_line


__all__ = ('init_server', 'run_server')


@asyncio.coroutine
def init_server(loop, ip, port, router=None):
    """
        Initialize instance of web server

        Args:
            ip - used IP
            port - listened port on web server
            router - instance of RestWSRouter class with user defined endpoints
    """
    app = web.Application(loop=loop, router=router)
    srv = yield from loop.create_server(app.make_handler(), str(ip), int(port))
    print("Server started at http://{0}:{1}".format(ip, port))
    return srv


def run_server(router=None):
    """
        Create and start web server with typed IP and PORT via command line

        Args:
            router - instance of RestWSRouter class with user defined endpoints
    """
    define('-ip', default='127.0.0.1', type=str, help="used ip for server")
    define('-port', default=8080, type=int, help="listened port on the server")
    args = parse_command_line()
    loop = asyncio.get_event_loop()
    server_instance = init_server(loop, args.ip, args.port, router)
    loop.run_until_complete(server_instance)
    loop.run_forever()

@@ -107,74 +107,38 @@
 eb%0A%0A
-from command_line import define, parse_command_line%0A%0A%0A__all__ = ('
+%0A__all__ = ('init_server', 'de
 init
@@ -501,24 +501,81 @@
 ter=router)%0A
+    app%5B'sockets'%5D = %5B%5D%0A    handler = app.make_handler()%0A
     srv = yi
@@ -602,34 +602,23 @@
 _server(
-app.make_
 handler
-()
 , str(ip
@@ -709,292 +709,520 @@
 urn 
-srv%0A%0A%0Adef run_server(router=None):%0A    %22%22%22%0A        Create and start web server with typed IP and PORT via command line%0A%0A        Args:%0A            router - instance of RestWSRouter class with user defined endpoints%0A    %22%22%22%0A    define('-ip', default='127.0.0.1', type=str, help=%22
+app, srv, handler%0A%0A%0A@asyncio.coroutine%0Adef deinit_server(app, server, handler):%0A    %22%22%22%0A        Deinitialize instance of web server%0A    %22%22%22%0A    for ws in app%5B'sockets'%5D:%0A        ws.close()%0A    app%5B'sockets'%5D.clear()%0A    yield from asyncio.sleep(0.1)%0A    server.close()%0A    yield from handler.finish_connections()%0A    yield from server.wait_closed()%0A%0A%0Adef run_server(ip='127.0.0.1', port=8080, router=None):%0A    %22%22%22%0A        Create and start web server with some IP and PORT%0A%0A        Args:%0A            ip - 
 used 
-ip
+IP
  for
@@ -1232,122 +1232,95 @@
 rver
-%22)%0A    define('-port', default=8080, type=int, help=%22listened port on the server%22)%0A    args = parse_command_line()
+%0A            port - listened port%0A            router - instance of RestWSRouter%0A    %22%22%22
 %0A   
@@ -1396,22 +1396,12 @@
 op, 
-args.ip, args.
+ip, 
 port
@@ -1409,24 +1409,44 @@
  router)%0A   
+ app, srv, handler =
  loop.run_un
@@ -1475,16 +1475,29 @@
 stance)%0A
+    try:%0A    
     loop
@@ -1507,12 +1507,108 @@
 n_forever()%0A
+    except KeyboardInterrupt:%0A        loop.run_until_complete(deinit_server(app, srv, handler))%0A

Rewrite functions