Coverage for src/mathenjeu/apps/server/server_hypercorn.py: 76%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Server with :epkg:`hypercorn`.
4"""
5from hypercorn.config import Config
6from hypercorn.run import run
7from hypercorn.utils import load_application
10class ServerHypercorn:
11 """
12 Implements a server based on :epkg:`hypercorn`.
13 """
15 def __init__(self, **kwargs):
16 """
17 @param kwargs parameters
18 """
19 self._run = run
20 config = Config()
21 app = load_application(kwargs['application_path'])
22 if app is None:
23 raise RuntimeError("Unable to load application '{0}'".format(
24 kwargs["application_path"]))
26 for k, v in kwargs.items():
27 if k in ('binds',):
28 continue
29 setattr(config, k, v)
31 if len(kwargs['bind']) > 0:
32 bind = kwargs['bind']
33 if not isinstance(bind, list):
34 bind = [bind]
35 config.bind = bind
36 self.config = config
38 def run(self, verbose=True):
39 """
40 Starts the server.
42 @param verbose display the host
43 """
44 if verbose:
45 scheme = "https" if self.config.ssl_enabled else "http"
46 print("[mathenjeu] running on '{}' and {}".format(
47 scheme, self.config.bind))
48 self._run(self.config)