The HTTP router to register to
Class instance to use for the web interface mapping
Optional parameter to customize the mapping process
For mapping method parameters without a prefixed underscore to query/form fields, the following rules are applied:
The following attributes are supported for annotating methods of the registered class:
@before, @after, @errorDisplay, @hb.web.common.method, @hb.web.common.path, @hb.web.common.contentType
The @path attribute can also be applied to the class itself, in which case it will be used as an additional prefix to the one in WebInterfaceSettings.urlPrefix.
Gives an overview of the basic features. For more advanced use, see the example in the "examples/web/" directory.
import vibe.http.router; import vibe.http.server; import hb.web.web; class WebService { private { SessionVar!(string, "login_user") m_loginUser; } @path("/") void getIndex(string _error = null) { //render!("index.dt", _error); } // automatically mapped to: POST /login @errorDisplay!getIndex void postLogin(string username, string password) { enforceHTTP(username.length > 0, HTTPStatus.forbidden, "User name must not be empty."); enforceHTTP(password == "secret", HTTPStatus.forbidden, "Invalid password."); m_loginUser = username; redirect("/profile"); } // automatically mapped to: POST /logout void postLogout() { terminateSession(); redirect("/"); } // automatically mapped to: GET /profile void getProfile() { enforceHTTP(m_loginUser.length > 0, HTTPStatus.forbidden, "Must be logged in to access the profile."); //render!("profile.dt") } } void run() { auto router = new URLRouter; router.registerWebInterface(new WebService); auto settings = new HTTPServerSettings; settings.port = 8080; listenHTTP(settings, router); }
Registers a HTTP/web interface based on a class instance.
Each public method of the given class instance will be mapped to a HTTP route. Property methods are mapped to GET/PUT and all other methods are mapped according to their prefix verb. If the method has no known prefix, POST is used. The rest of the name is mapped to the path of the route according to the given method_style. Note that the prefix word must be all-lowercase and is delimited by either an upper case character, a non-alphabetic character, or the end of the string.
The following table lists the mappings from prefix verb to HTTP verb:
Method parameters will be sourced from either the query string or form data of the request, or, if the parameter name has an underscore prefixed, from the vibe.http.server.HTTPServerRequest.params map.
The latter can be used to inject custom data in various ways. Examples of this are placeholders specified in a @path annotation, values computed by a @before annotation, error information generated by the @errorDisplay annotation, or data injected manually in a HTTP method handler that processed the request prior to passing it to the generated web interface handler routes.
Methods that return a class or interface instance, instead of being mapped to a single HTTP route, will be mapped recursively by iterating the public routes of the returned instance. This way, complex path hierarchies can be mapped to class hierarchies.