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 22:59 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
1"""WSGI application for MapProxy integration with GWS.
3This module provides a WSGI application that serves MapProxy requests.
4It is based on the MapProxy WSGI application with adaptations for GWS.
6See: https://mapproxy.org/docs/1.11.0/deployment.html?highlight=make_wsgi_app#server-script
7"""
9import yaml
10from typing import Any, Dict, List, Optional, Tuple, Union, Callable
12import gws
14import mapproxy.wsgiapp
15import logging
17from . import config
19logging.basicConfig()
20logging.getLogger().setLevel(logging.INFO)
23# from mapproxy.wsgiapp, adapted to our needs
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.
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.
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.
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()
44 config_files = conf.config_files()
45 conf.base_config.debug_mode = True
47 app = mapproxy.wsgiapp.MapProxyApp(services, conf.base_config)
49 app.config_files = config_files
50 return app
53mapproxy_app = make_wsgi_app(config.CONFIG_PATH)
56def init() -> None:
57 """Initialize the MapProxy application.
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'])
68def application(environ: Dict[str, Any], start_response: Callable) -> List[bytes]:
69 """WSGI application function for MapProxy.
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.
75 Args:
76 environ: The WSGI environment dictionary.
77 start_response: The WSGI start_response callable.
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')]
90# import os, sys
91# sys.path.append(os.path.dirname(os.path.realpath(__file__)))
92# from util import AnnotationFilter
93# application = AnnotationFilter(application)