Hide keyboard shortcuts

Hot-keys on this page

r m x p   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 

8 

9 

10class ServerHypercorn: 

11 """ 

12 Implements a server based on :epkg:`hypercorn`. 

13 """ 

14 

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"])) 

25 

26 for k, v in kwargs.items(): 

27 if k in ('binds',): 

28 continue 

29 setattr(config, k, v) 

30 

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 

37 

38 def run(self, verbose=True): 

39 """ 

40 Starts the server. 

41 

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)