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
| 1959 | |
| 1960 | |
| 1961 | class Application(ReversibleRouter): |
| 1962 | r"""A collection of request handlers that make up a web application. |
| 1963 | |
| 1964 | Instances of this class are callable and can be passed directly to |
| 1965 | HTTPServer to serve the application:: |
| 1966 | |
| 1967 | application = web.Application([ |
| 1968 | (r"/", MainPageHandler), |
| 1969 | ]) |
| 1970 | http_server = httpserver.HTTPServer(application) |
| 1971 | http_server.listen(8080) |
| 1972 | |
| 1973 | The constructor for this class takes in a list of `~.routing.Rule` |
| 1974 | objects or tuples of values corresponding to the arguments of |
| 1975 | `~.routing.Rule` constructor: ``(matcher, target, [target_kwargs], [name])``, |
| 1976 | the values in square brackets being optional. The default matcher is |
| 1977 | `~.routing.PathMatches`, so ``(regexp, target)`` tuples can also be used |
| 1978 | instead of ``(PathMatches(regexp), target)``. |
| 1979 | |
| 1980 | A common routing target is a `RequestHandler` subclass, but you can also |
| 1981 | use lists of rules as a target, which create a nested routing configuration:: |
| 1982 | |
| 1983 | application = web.Application([ |
| 1984 | (HostMatches("example.com"), [ |
| 1985 | (r"/", MainPageHandler), |
| 1986 | (r"/feed", FeedHandler), |
| 1987 | ]), |
| 1988 | ]) |
| 1989 | |
| 1990 | In addition to this you can use nested `~.routing.Router` instances, |
| 1991 | `~.httputil.HTTPMessageDelegate` subclasses and callables as routing targets |
| 1992 | (see `~.routing` module docs for more information). |
| 1993 | |
| 1994 | When we receive requests, we iterate over the list in order and |
| 1995 | instantiate an instance of the first request class whose regexp |
| 1996 | matches the request path. The request class can be specified as |
| 1997 | either a class object or a (fully-qualified) name. |
| 1998 | |
| 1999 | A dictionary may be passed as the third element (``target_kwargs``) |
| 2000 | of the tuple, which will be used as keyword arguments to the handler's |
| 2001 | constructor and `~RequestHandler.initialize` method. This pattern |
| 2002 | is used for the `StaticFileHandler` in this example (note that a |
| 2003 | `StaticFileHandler` can be installed automatically with the |
| 2004 | static_path setting described below):: |
| 2005 | |
| 2006 | application = web.Application([ |
| 2007 | (r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}), |
| 2008 | ]) |
| 2009 | |
| 2010 | We support virtual hosts with the `add_handlers` method, which takes in |
| 2011 | a host regular expression as the first argument:: |
| 2012 | |
| 2013 | application.add_handlers(r"www\.myhost\.com", [ |
| 2014 | (r"/article/([0-9]+)", ArticleHandler), |
| 2015 | ]) |
| 2016 | |
| 2017 | If there's no match for the current request's host, then ``default_host`` |
| 2018 | parameter value is matched against host regular expressions. |