Returns a WSGI compatible API server for the given Hug API module
(self, default_not_found=True, base_url=None)
| 357 | ) |
| 358 | |
| 359 | def server(self, default_not_found=True, base_url=None): |
| 360 | """Returns a WSGI compatible API server for the given Hug API module""" |
| 361 | falcon_api = self.falcon = falcon.API(middleware=self.middleware) |
| 362 | if not self.api.future: |
| 363 | falcon_api.req_options.keep_blank_qs_values = False |
| 364 | falcon_api.req_options.auto_parse_qs_csv = True |
| 365 | falcon_api.req_options.strip_url_path_trailing_slash = True |
| 366 | |
| 367 | default_not_found = self.documentation_404() if default_not_found is True else None |
| 368 | base_url = self.base_url if base_url is None else base_url |
| 369 | |
| 370 | not_found_handler = default_not_found |
| 371 | self.api._ensure_started() |
| 372 | if self.not_found_handlers: |
| 373 | if len(self.not_found_handlers) == 1 and None in self.not_found_handlers: |
| 374 | not_found_handler = self.not_found_handlers[None] |
| 375 | else: |
| 376 | not_found_handler = partial( |
| 377 | self.version_router, |
| 378 | api_version=False, |
| 379 | versions=self.not_found_handlers, |
| 380 | not_found=default_not_found, |
| 381 | ) |
| 382 | not_found_handler.interface = True |
| 383 | |
| 384 | if not_found_handler: |
| 385 | falcon_api.add_sink(not_found_handler) |
| 386 | self._not_found = not_found_handler |
| 387 | |
| 388 | for sink_base_url, sinks in self.sinks.items(): |
| 389 | for url, extra_sink in sinks.items(): |
| 390 | falcon_api.add_sink(extra_sink, sink_base_url + url + "(?P<path>.*)") |
| 391 | |
| 392 | for router_base_url, routes in self.routes.items(): |
| 393 | for url, methods in routes.items(): |
| 394 | router = {} |
| 395 | for method, versions in methods.items(): |
| 396 | method_function = "on_{0}".format(method.lower()) |
| 397 | if len(versions) == 1 and None in versions.keys(): |
| 398 | router[method_function] = versions[None] |
| 399 | else: |
| 400 | router[method_function] = partial( |
| 401 | self.version_router, versions=versions, not_found=not_found_handler |
| 402 | ) |
| 403 | |
| 404 | router = namedtuple("Router", router.keys())(**router) |
| 405 | falcon_api.add_route(router_base_url + url, router) |
| 406 | if self.versions and self.versions != (None,): |
| 407 | falcon_api.add_route(router_base_url + "/v{api_version}" + url, router) |
| 408 | |
| 409 | def error_serializer(request, response, error): |
| 410 | response.content_type = self.output_format.content_type |
| 411 | response.body = self.output_format( |
| 412 | {"errors": {error.title: error.description}}, request, response |
| 413 | ) |
| 414 | |
| 415 | falcon_api.set_error_serializer(error_serializer) |
| 416 | return falcon_api |