r"""A collection of request handlers that make up a web application. Instances of this class are callable and can be passed directly to HTTPServer to serve the application:: application = web.Application([ (r"/", MainPageHandler), ]) http_server = httpse
| 2127 | |
| 2128 | |
| 2129 | class Application(ReversibleRouter): |
| 2130 | r"""A collection of request handlers that make up a web application. |
| 2131 | |
| 2132 | Instances of this class are callable and can be passed directly to |
| 2133 | HTTPServer to serve the application:: |
| 2134 | |
| 2135 | application = web.Application([ |
| 2136 | (r"/", MainPageHandler), |
| 2137 | ]) |
| 2138 | http_server = httpserver.HTTPServer(application) |
| 2139 | http_server.listen(8080) |
| 2140 | |
| 2141 | The constructor for this class takes in a list of `~.routing.Rule` |
| 2142 | objects or tuples of values corresponding to the arguments of |
| 2143 | `~.routing.Rule` constructor: ``(matcher, target, [target_kwargs], [name])``, |
| 2144 | the values in square brackets being optional. The default matcher is |
| 2145 | `~.routing.PathMatches`, so ``(regexp, target)`` tuples can also be used |
| 2146 | instead of ``(PathMatches(regexp), target)``. |
| 2147 | |
| 2148 | A common routing target is a `RequestHandler` subclass, but you can also |
| 2149 | use lists of rules as a target, which create a nested routing configuration:: |
| 2150 | |
| 2151 | application = web.Application([ |
| 2152 | (HostMatches("example.com"), [ |
| 2153 | (r"/", MainPageHandler), |
| 2154 | (r"/feed", FeedHandler), |
| 2155 | ]), |
| 2156 | ]) |
| 2157 | |
| 2158 | In addition to this you can use nested `~.routing.Router` instances, |
| 2159 | `~.httputil.HTTPMessageDelegate` subclasses and callables as routing targets |
| 2160 | (see `~.routing` module docs for more information). |
| 2161 | |
| 2162 | When we receive requests, we iterate over the list in order and |
| 2163 | instantiate an instance of the first request class whose regexp |
| 2164 | matches the request path. The request class can be specified as |
| 2165 | either a class object or a (fully-qualified) name. |
| 2166 | |
| 2167 | A dictionary may be passed as the third element (``target_kwargs``) |
| 2168 | of the tuple, which will be used as keyword arguments to the handler's |
| 2169 | constructor and `~RequestHandler.initialize` method. This pattern |
| 2170 | is used for the `StaticFileHandler` in this example (note that a |
| 2171 | `StaticFileHandler` can be installed automatically with the |
| 2172 | static_path setting described below):: |
| 2173 | |
| 2174 | application = web.Application([ |
| 2175 | (r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}), |
| 2176 | ]) |
| 2177 | |
| 2178 | We support virtual hosts with the `add_handlers` method, which takes in |
| 2179 | a host regular expression as the first argument:: |
| 2180 | |
| 2181 | application.add_handlers(r"www\.myhost\.com", [ |
| 2182 | (r"/article/([0-9]+)", ArticleHandler), |
| 2183 | ]) |
| 2184 | |
| 2185 | If there's no match for the current request's host, then ``default_host`` |
| 2186 | parameter value is matched against host regular expressions. |