NAME

Pono - Hono-like web framework for Perl, built on PSGI and Router::Boom

SYNOPSIS

# app.psgi
use Pono;

my $app = Pono->new;

$app->get('/' => sub {
    my ($c) = @_;
    $c->text('Hello Pono!');
});

$app->get('/hello/:name' => sub {
    my ($c) = @_;
    $c->json({ hello => $c->req->param('name') });
});

$app->fire;

Run it with any PSGI server:

plackup app.psgi

With signatures (Perl 5.36 or later), handlers read like the ones in Hono:

use v5.36;
use Pono;

my $app = Pono->new;
$app->get('/' => sub ($c) { $c->text('Hello Pono!') });
$app->fire;

DESCRIPTION

Pono is a small web framework with the API of Hono, written for PSGI. Routes are matched by Router::Boom, which compiles all of them into a single regular expression. The same idea is behind Hono's RegExpRouter.

Pono has no dependencies other than Router::Boom and JSON::PP, and is pure Perl.

This is an experimental release. The API may change.

ROUTING

get, post, put, delete, patch, options, head

$app->get($path => $handler);
$app->post($path => $middleware, $handler);

Adds a route for the HTTP method. $path can contain named parameters (/users/:id) and a wildcard (/files/*). Their values are read with $c->req->param('id') and $c->req->param('*').

Middleware runs before the route handlers whatever the order they were added in.

A request with the HEAD method uses the GET route when there is no HEAD route, and the body is dropped.

All routing methods return $app, so they can be chained.

all

$app->all($path => $handler);

Adds a route that matches every HTTP method.

on

$app->on($method, $path => $handler);

Adds a route for an HTTP method given as a string.

route

my $api = Pono->new;
$api->get('/users' => sub { ... });

$app->route('/api', $api);    # GET /api/users

Mounts the routes and middleware of another Pono application under a prefix.

HANDLERS

A handler receives a Pono::Context object, $c, and returns a response made with one of its methods:

$app->get('/' => sub {
    my ($c) = @_;
    return $c->text('Hello');
});

A handler can also return a PSGI response (an array reference) directly.

A route can have more than one handler. The last one is called with $c only. The ones before it are called with ($c, $next), like middleware.

MIDDLEWARE

use

$app->use(sub {
    my ($c, $next) = @_;
    my $start = time;
    $next->();
    $c->header('X-Time' => time - $start);
});

$app->use('/api/*' => sub {
    my ($c, $next) = @_;
    return $c->text('Unauthorized', 401) unless $c->req->header('Authorization');
    $next->();
});

Adds middleware, for every path or for the paths that match. Middleware runs in the order it was added, around the route handler. Call $next->() to run the rest. Code after $next->() runs on the way back. There, $c->header adds a header to the response, and $c->res reads or replaces it.

When middleware returns a response without calling $next, the route handler does not run.

ERROR HANDLING

not_found

$app->not_found(sub {
    my ($c) = @_;
    $c->text('Nothing here', 404);
});

Sets the handler for requests that match no route. The default returns 404 Not Found as plain text.

on_error

$app->on_error(sub {
    my ($error, $c) = @_;
    $c->json({ error => "$error" }, 500);
});

Sets the handler for exceptions thrown by handlers and middleware. The default passes the error to warn and returns 500 Internal Server Error.

RUNNING

fire

$app->fire;

Returns the PSGI application. Put it at the end of app.psgi. The name comes from Hono.

to_app

An alias of fire, for those who expect the usual PSGI name.

fetch

my $res = $app->fetch($env);

Takes a PSGI environment and returns a PSGI response.

TESTING

request

my $res = $app->request('/hello/yapc?q=tokyo');
is $res->status, 200;
is_deeply $res->json, { hello => 'yapc' };

$res = $app->request('/posts',
    method  => 'POST',
    headers => { 'Content-Type' => 'application/json' },
    body    => '{"title":"Hello"}',
);

Sends a request to the application without a server and returns a Pono::Response.

SEE ALSO

Pono::Context, Pono::Request, Pono::Response

Router::Boom, PSGI, Plack

Hono

LICENSE

Copyright (C) Yusuke Wada.

This library is free software; you can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE file in this distribution.

AUTHOR

Yusuke Wada