WSGI application
| 523 | |
| 524 | |
| 525 | class Bottle(object): |
| 526 | """ WSGI application """ |
| 527 | |
| 528 | def __init__(self, catchall=True, autojson=True, config=None): |
| 529 | """ Create a new bottle instance. |
| 530 | You usually don't do that. Use `bottle.app.push()` instead. |
| 531 | """ |
| 532 | self.routes = [] # List of installed :class:`Route` instances. |
| 533 | self.router = Router() # Maps requests to :class:`Route` instances. |
| 534 | self.plugins = [] # List of installed plugins. |
| 535 | |
| 536 | self.error_handler = {} |
| 537 | #: If true, most exceptions are catched and returned as :exc:`HTTPError` |
| 538 | self.config = ConfigDict(config or {}) |
| 539 | self.catchall = catchall |
| 540 | #: An instance of :class:`HooksPlugin`. Empty by default. |
| 541 | self.hooks = HooksPlugin() |
| 542 | self.install(self.hooks) |
| 543 | if autojson: |
| 544 | self.install(JSONPlugin()) |
| 545 | self.install(TemplatePlugin()) |
| 546 | |
| 547 | def mount(self, prefix, app, **options): |
| 548 | ''' Mount an application (:class:`Bottle` or plain WSGI) to a specific |
| 549 | URL prefix. Example:: |
| 550 | |
| 551 | root_app.mount('/admin/', admin_app) |
| 552 | |
| 553 | :param prefix: path prefix or `mount-point`. If it ends in a slash, |
| 554 | that slash is mandatory. |
| 555 | :param app: an instance of :class:`Bottle` or a WSGI application. |
| 556 | |
| 557 | All other parameters are passed to the underlying :meth:`route` call. |
| 558 | ''' |
| 559 | if isinstance(app, basestring): |
| 560 | prefix, app = app, prefix |
| 561 | depr('Parameter order of Bottle.mount() changed.') # 0.10 |
| 562 | |
| 563 | parts = filter(None, prefix.split('/')) |
| 564 | if not parts: raise ValueError('Empty path prefix.') |
| 565 | path_depth = len(parts) |
| 566 | options.setdefault('skip', True) |
| 567 | options.setdefault('method', 'ANY') |
| 568 | |
| 569 | @self.route('/%s/:#.*#' % '/'.join(parts), **options) |
| 570 | def mountpoint(): |
| 571 | try: |
| 572 | request.path_shift(path_depth) |
| 573 | rs = BaseResponse([], 200) |
| 574 | def start_response(status, header): |
| 575 | rs.status = status |
| 576 | for name, value in header: rs.add_header(name, value) |
| 577 | return rs.body.append |
| 578 | rs.body = itertools.chain(rs.body, app(request.environ, start_response)) |
| 579 | return HTTPResponse(rs.body, rs.status, rs.headers) |
| 580 | finally: |
| 581 | request.path_shift(-path_depth) |
| 582 |