Skip to content

Commit

Permalink
Merge pull request #162 from TeskaLabs/revert-161-refactoring/web-sec…
Browse files Browse the repository at this point in the history
…tion-config

Revert "Draft: Web section config refactoring"
  • Loading branch information
byewokko authored Jun 23, 2021
2 parents 2753e2f + b49814b commit c700e35
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 32 deletions.
12 changes: 9 additions & 3 deletions asab/api/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class ApiService(asab.Service):
def __init__(self, app, service_name):
super().__init__(app, service_name)

self.WebContainer = self._initialize_web()
listen = asab.Config["asab:web"]["listen"]
self.WebContainer = self._initialize_web(listen)

if len(asab.Config["asab:zookeeper"]["servers"]) > 0:
self.ZkContainer = self._initialize_zookeeper()
Expand Down Expand Up @@ -59,10 +60,15 @@ def _build_zookeeper_adv_data(self):
return adv_data


def _initialize_web(self):
def _initialize_web(self, listen):
websvc = self.App.get_service("asab.WebService")

container = websvc.WebApp.Container
# Create a dedicated web container
container = asab.web.WebContainer(
websvc, "asab:web",
config={"listen": listen}
)
# TODO: refactor to use custom config section, instead of explicitly passing "listen" param?

# TODO: Logging level configurable via config file
self.APILogHandler = WebApiLoggingHandler(self.App, level=logging.NOTSET)
Expand Down
8 changes: 2 additions & 6 deletions asab/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ def handler(type):
self.TaskService = TaskService(self)

# Setup ASAB API
if (
(Config.has_option("web", "listen") and len(Config.get("web", "listen")) > 0)
# Backward compatibility: try fallback to "asab:web"
or (Config.has_option("asab:web", "listen") and len(Config.get("asab:web", "listen")) > 0)
):
if len(Config['asab:web']["listen"]) > 0:
from asab.api import Module
self.add_module(Module)

Expand Down Expand Up @@ -199,7 +195,7 @@ def parse_arguments(self, args=None):
Config._default_values['logging:file']['path'] = args.log_file

if args.web_api:
Config._default_values['web']['listen'] = args.web_api
Config._default_values['asab:web']['listen'] = args.web_api
return args


Expand Down
2 changes: 1 addition & 1 deletion asab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ConfigParser(configparser.ConfigParser):
"rotate_every": "",
},

"web": {
"asab:web": {
"listen": "",
},

Expand Down
4 changes: 1 addition & 3 deletions asab/web/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ def __init__(self, websvc, config_section_name, config=None):
elif param.startswith('ssl'):
ssl_context = SSLContextBuilder("<none>", config=self.Config).build()
else:
raise RuntimeError(
"Unknown listen parameter in section [{}]: {}".format(config_section_name, param)
)
raise RuntimeError("Unknown asab:web listen parameter: '{}'".format(param))
self._listen.append((addr, port, ssl_context))

self.WebApp = aiohttp.web.Application(loop=websvc.App.Loop)
Expand Down
21 changes: 2 additions & 19 deletions asab/web/service.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import logging
import asyncio
import asab


L = logging.getLogger(__name__)


class WebService(asab.Service):
ObsoleteConfigAliases = ["asab:web"]

def __init__(self, app, service_name):
super().__init__(app, service_name)

self.WebConfigName = "web"
self.Containers = {}
self.App = app

Expand All @@ -32,19 +26,8 @@ def WebApp(self):
'''
This is here to maintain backward compatibility.
'''
# The WebContainer should be configured in the config section [web]
if self.WebConfigName not in asab.Config.sections():
# If there is no [web] section, try other aliases for backwards compatibility
for alias in self.ObsoleteConfigAliases:
if alias in asab.Config.sections():
self.WebConfigName = alias
L.warning("Using obsolete web config alias [{}]. Preferred section name is [web]. ".format(alias))
break
else:
raise RuntimeError("No [web] section configured.")

try:
return self.Containers[self.WebConfigName].WebApp
return self.Containers['asab:web'].WebApp
except KeyError:
from .container import WebContainer
return WebContainer(self, self.WebConfigName).WebApp
return WebContainer(self, 'asab:web').WebApp

0 comments on commit c700e35

Please sign in to comment.