Skip to content

Commit

Permalink
Updates code examples with the end continuation.
Browse files Browse the repository at this point in the history
  • Loading branch information
arteymix committed May 29, 2015
1 parent 3ab8f7f commit 6a9b588
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 56 deletions.
8 changes: 4 additions & 4 deletions docs/ctpl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ application can produce very big output efficiently.

.. code:: vala
app.get ("", (req, res) => {
app.get ("", (req, res, end) => {
var template = new View.from_string ("");
template.stream (res.body);
res.end ();
end ();
});
It is unfortunately not possible to stream with non-blocking I/O due to the
Expand All @@ -101,7 +101,7 @@ response body.

.. code:: vala
app.get ("", (req, res) => {
app.get ("", (req, res, end) => {
var template = new View.from_string ("");
var buffer = new MemoryOutputStream.resizable ();
Expand All @@ -117,6 +117,6 @@ response body.
null,
(obj, result) => {
var spliced = res.body.splice_async.end (result);
res.end ();
end ();
});
});
4 changes: 2 additions & 2 deletions docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ changes in the framework.
var app = new Router ();
app.get("", (req, res) => {
app.get("", (req, res, end) => {
res.body.write ("Hello world!".data);
res.end ();
end ();
});
new Server (app).run ({"app", "--port", "3003"});
Expand Down
6 changes: 4 additions & 2 deletions docs/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ Let's say you need an administration section:
using Valum;
public static Router.Loader admin_loader = (admin) {
admin.get ("", (req, res) => {
admin.get ("", (req, res, end) => {
// ...
end ();
});
}
Expand Down Expand Up @@ -48,8 +49,9 @@ If you distribute your code, use namespaces to avoid conflicts:
namespace Admin {
public static void admin_loader (Router admin) {
admin.get ("", (req, res) => {
admin.get ("", (req, res, end) => {
// ...
end ();
});
}
}
8 changes: 4 additions & 4 deletions docs/persistency.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ maintained in nemequ/vala-extra-vapis GitHub repository.
var app = new Router ();
var memcached = new Memcached.Context ();
app.get ("<key>", (req, res) => {
app.get ("<key>", (req, res, end) => {
var key = req.params["key"];
int32 flags;
Expand All @@ -44,10 +44,10 @@ maintained in nemequ/vala-extra-vapis GitHub repository.
res.body.write (value);
res.end ();
end ();
});
app.post ("<key>", (req, res) => {
app.post ("<key>", (req, res, end) => {
var key = req.params["key"];
var buffer = new MemoryOutputStream.resizable ();
Expand All @@ -60,5 +60,5 @@ maintained in nemequ/vala-extra-vapis GitHub repository.
res.write (value);
res.end ();
end ();
});
22 changes: 4 additions & 18 deletions docs/redirection-and-error.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use the message as a redirect URL.

.. code:: vala
app.get ("user/<id>/save", (req, res) => {
app.get ("user/<id>/save", (req, res, end) => {
var user = User (req.params["id"]);
if (user.save ())
throw new Redirection.MOVED_TEMPORAIRLY ("/user/%u".printf (user.id));
res.end ();
end ();
});
Client (4xx) and server (5xx) error
Expand All @@ -42,7 +42,7 @@ Errors are predefined in ``ClientError`` and ``ServerError`` enumerations.

.. code:: vala
app.get ("not-found", (req, res) => {
app.get ("not-found", (req, res, end) => {
throw new ClientError.NOT_FOUND ("The requested URI was not found.");
});
Expand All @@ -58,20 +58,6 @@ the :doc:`router` can handle them properly.
next (); // will throw a 404
});
app.get ("", (req, res) => {
app.get ("", (req, res, end) => {
throw new ClientError.NOT_FOUND ("");
});
Custom handling for status
--------------------------

To do custom handling for specific status, bind a callback to the ``teardown``
signal, it is executed after the processing of a client request.

.. code:: vala
app.teardown.connect ((req, res) => {
if (res.status == Soup.Status.NOT_FOUND) {
// produce a 404 page...
}
});
28 changes: 14 additions & 14 deletions docs/route.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ parameters with their named captures.

.. code:: vala
app.get ("<int:i>", (req, res) => {
app.get ("<int:i>", (req, res, end) => {
var i = req.params["i"];
});
Expand All @@ -43,12 +43,12 @@ Rules are used by the HTTP methods alias and ``method`` function in
.. code:: vala
// using an alias
app.get ("your-rule/<int:id>", (req, res) => {
app.get ("your-rule/<int:id>", (req, res, end) => {
});
// using a method
app.method (Request.GET, "your-rule/<int:id>", (req, res) => {
app.method (Request.GET, "your-rule/<int:id>", (req, res, end) => {
});
Expand Down Expand Up @@ -96,7 +96,7 @@ shows an example for creating a 404 error page.

.. code:: vala
app.get("<any:path>", (req, res) => {
app.get("<any:path>", (req, res, end) => {
res.status = 404;
});
Expand Down Expand Up @@ -128,10 +128,10 @@ and optimized.

.. code:: vala
app.regex (Request.GET, /home\/?/, (req, res) => {
app.regex (Request.GET, /home\/?/, (req, res, end) => {
var writer = new DataOutputStream (res.body);
writer.put_string ("Matched using a regular expression.");
res.end ();
end ();
});
Matching using a callback
Expand All @@ -147,10 +147,10 @@ A matcher consist of a callback matching a given ``Request`` object.
Route.Matcher matcher = (req) => { req.path == "/custom-matcher"; };
app.matcher ("GET", matcher, (req, res) => {
app.matcher ("GET", matcher, (req, res, end) => {
var writer = new DataOutputStream (res.body);
writer.put_string ("Matched using a custom matcher.");
res.end ();
end ();
});
You could, for instance, match the request if the user is an administrator and
Expand All @@ -161,13 +161,13 @@ fallback to a default route otherwise.
app.matcher ("GET", (req) => {
var user = new User (req.query["id"]);
return "admin" in user.roles;
}, (req, res) => {
}, (req, res, end) => {
// ...
});
app.route ("<any:path>", (req, res) => {
app.route ("<any:path>", (req, res, end) => {
res.status = 404;
res.end ();
end ();
});
Combining custom matcher with existing matcher
Expand Down Expand Up @@ -208,7 +208,7 @@ the processing of a handler.

.. code:: vala
app.get ("redirection", (req, res) => {
app.get ("redirection", (req, res, end) => {
throw new Redirection.MOVED_TEMPORAIRLY ("http://example.com");
});
Expand All @@ -223,13 +223,13 @@ work.

.. code:: vala
app.get ("", (req, res) => {
app.get ("", (req, res, end) => {
// write now and block
res.body.write ("Hello world!".data);
res.body.write_async.begin ("Hello world!".data, (obj, r) => {
var written = res.write_async.end (r);
res.end ();
end ();
});
// keep processing while the response is begin written
Expand Down
21 changes: 11 additions & 10 deletions docs/router.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Callback can be connected to HTTP methods via a list of helpers having the

.. code:: vala
app.get ("rule", (req, res) => {});
app.get ("rule", (req, res, end) => {});
Helpers for the HTTP/1.1 protocol and the extra ``TRACE`` methods are included.

Expand All @@ -31,7 +31,7 @@ the ``application/x-www-form-urlencoded`` body of the :doc:`vsgi/request`.

.. code:: vala
app.post ("login", (req, res) => {
app.post ("login", (req, res, end) => {
var buffer = new MemoryOutputStream.resizable ();
// consume the request body
Expand All @@ -45,22 +45,22 @@ the ``application/x-www-form-urlencoded`` body of the :doc:`vsgi/request`.
// assuming you have a session implementation in your app
var session = new Session.authenticated_by (username, password);
res.end ();
end ();
});
It is also possible to use a custom HTTP method via the ``method``
function.

.. code:: vala
app.method ("METHOD", "rule", (req, res) => {});
app.method ("METHOD", "rule", (req, res, end) => {});
:doc:`vsgi/request` provide an enumeration of HTTP methods for your
convenience.

.. code:: vala
app.method (Request.GET, "rule", (req, res) => {});
app.method (Request.GET, "rule", (req, res, end) => {});
Multiple methods can be captured with ``methods`` and ``all``.

Expand All @@ -79,7 +79,7 @@ Regular expression

.. code:: vala
app.regex (/home/, (req, res) => {
app.regex (/home/, (req, res, end) => {
// matches /home
});
Expand All @@ -92,7 +92,7 @@ otherwise you will experience inconsistencies.

.. code:: vala
app.matcher (Request.GET, (req) => { return req.uri.get_path () == "/home"; }, (req, res) => {
app.matcher (Request.GET, (req) => { return req.uri.get_path () == "/home"; }, (req, res, end) => {
// matches /home
});
Expand All @@ -113,12 +113,12 @@ a future release.
app.scope ("admin", (admin) => {
// admin is a scoped Router
app.get ("users", (req, res) => {
app.get ("users", (req, res, end) => {
// matches /admin/users
});
});
app.get ("users", (req, res) => {
app.get ("users", (req, res, end) => {
// matches /users
});
Expand Down Expand Up @@ -166,6 +166,7 @@ matching route.
next (); // keep routing
});
app.get ("", (req, res) => {
app.get ("", (req, res, end) => {
// this is invoked!
end ();
});
7 changes: 5 additions & 2 deletions docs/scripting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Lua
var lua = new LuaVM ();
// GET /lua
app.get ("lua", (req, res) => {
app.get ("lua", (req, res, end) => {
var writer = new DataOutputStream (res.body);
// evaluate a string containing Lua code
Expand All @@ -42,6 +42,8 @@ Lua
// evaluate a file containing Lua code
writer.put_string (lua.do_file ("scripts/hello.lua"));
end ();
});
new Soup (app).run ();
Expand All @@ -67,9 +69,10 @@ Scheme can be used to produce template or facilitate computation.

.. code:: vala
app.get ("hello.scm", (req, res) => {
app.get ("hello.scm", (req, res, end) => {
var writer = new DataOutputStream (res.body);
writer.put_string (scm.run ("scripts/hello.scm"));
end ();
});
Scheme code:
Expand Down

0 comments on commit 6a9b588

Please sign in to comment.