Coverage for gws-app/gws/gis/mpx/wsgi_app.py: 0%

30 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 23:09 +0200

1"""WSGI application for MapProxy integration with GWS. 

2 

3This module provides a WSGI application that serves MapProxy requests. 

4It is based on the MapProxy WSGI application with adaptations for GWS. 

5 

6See: https://mapproxy.org/docs/1.11.0/deployment.html?highlight=make_wsgi_app#server-script 

7""" 

8 

9import yaml 

10from typing import Any, Dict, List, Optional, Tuple, Union, Callable 

11 

12import gws 

13 

14import mapproxy.wsgiapp 

15import logging 

16 

17from . import config 

18 

19logging.basicConfig() 

20logging.getLogger().setLevel(logging.INFO) 

21 

22 

23# from mapproxy.wsgiapp, adapted to our needs 

24 

25def make_wsgi_app(services_conf: Optional[str] = None, debug: bool = False, 

26 ignore_config_warnings: bool = True, reloader: bool = False) -> mapproxy.wsgiapp.MapProxyApp: 

27 """Create a MapProxy WSGI application. 

28 

29 This function creates a MapProxy WSGI application from a configuration file. 

30 It is adapted from mapproxy.wsgiapp.make_wsgi_app to suit GWS needs. 

31 

32 Args: 

33 services_conf: Path to the MapProxy configuration file. 

34 debug: Whether to enable debug mode. 

35 ignore_config_warnings: Whether to ignore configuration warnings. 

36 reloader: Whether to enable the configuration reloader. 

37 

38 Returns: 

39 A MapProxy WSGI application. 

40 """ 

41 conf = mapproxy.wsgiapp.load_configuration(mapproxy_conf=services_conf, ignore_warnings=ignore_config_warnings) 

42 services = conf.configured_services() 

43 

44 config_files = conf.config_files() 

45 conf.base_config.debug_mode = True 

46 

47 app = mapproxy.wsgiapp.MapProxyApp(services, conf.base_config) 

48 

49 app.config_files = config_files 

50 return app 

51 

52 

53mapproxy_app = make_wsgi_app(config.CONFIG_PATH) 

54 

55 

56def init() -> None: 

57 """Initialize the MapProxy application. 

58 

59 This function reads the MapProxy configuration file and ensures that 

60 all required directories exist. 

61 """ 

62 cfg = yaml.safe_load(gws.u.read_file(config.CONFIG_PATH)) 

63 gws.u.ensure_dir(cfg['globals']['cache']['base_dir']) 

64 gws.u.ensure_dir(cfg['globals']['cache']['lock_dir']) 

65 gws.u.ensure_dir(cfg['globals']['cache']['tile_lock_dir']) 

66 

67 

68def application(environ: Dict[str, Any], start_response: Callable) -> List[bytes]: 

69 """WSGI application function for MapProxy. 

70 

71 This function handles WSGI requests for MapProxy. It catches any exceptions 

72 that might occur and returns an error response instead of letting the 

73 exception propagate. 

74 

75 Args: 

76 environ: The WSGI environment dictionary. 

77 start_response: The WSGI start_response callable. 

78 

79 Returns: 

80 A list of bytes containing the response body. 

81 """ 

82 try: 

83 return mapproxy_app(environ, start_response) 

84 except Exception as exc: 

85 gws.log.exception() 

86 headers = [('Content-type', 'text/plain')] 

87 start_response('500', headers) 

88 return [(f'Exception\n{repr(exc)}').encode('utf-8')] 

89 

90# import os, sys 

91# sys.path.append(os.path.dirname(os.path.realpath(__file__))) 

92# from util import AnnotationFilter 

93# application = AnnotationFilter(application)