Terse - Lightweight Web Framework
Version 0.16
package MyAPI; use base 'Terse'; sub auth { my ($self, $t, $session) = @_; return 0 if $t->params->not; return $session; } sub hello_world { my ($self, $t) = @_; if ($t->params->throw_error) { $t->logError('throw 500 error which is also logged', 500); return; } $t->response->hello = "world"; } sub delayed_hello_world { my ($self, $t) = @_; $t->delayed_response(sub { if ($t->params->throw_error) { $t->logError('throw 500 error which is also logged', 500); return; } ... do something which takes a long time ... $t->response->hello = "world"; return $t->response; }); } sub websock { my ($self, $t) = @_; $t->websocket( connect => { my ($websocket) = @_; $websocket->send('Hello'); }, recieve => { my ($websocket, $message) = @_; $websocket->send($message); # echo }, error => { ... }, disconnect => { ... } ); } sub redirect { my ($self, $t) = @_; if ($t->params->hello) { $t->redirect('', { req => 'hello_world' }); } else { $t->redirect('https://world-wide.world/'); } } .... MyAPI.psgi ... use Terse; use MyAPI; my $app = MyAPI->to_app(); .... plackup -s Starman MyAPI.psgi GET http://localhost:5000/?req=delayed_hello_world # {"authenticated":1,"error":false,"errors":[],"hello":"world","status_code":200} GET http://localhost:5000/?req=hello_world¬=1 # {"authenticated":0,"error":true,"errors":["Invalid request"],"status_code":400} GET http://localhost:5000/?req=hello_world&throw_error=1 # {"authenticated":1,"error":true,"errors":["throw 500 error which is also logged"],"status_code":500}
Alot of the inspiration, and some code, for this module came from JSONP - which is a module to quickly build JSON/JSONP web services, providing also some syntactic sugar acting a bit like a sort of DSL (domain specific language) for JSON. ( thanks Anselmo Canfora ACANFORA! )
There are several key differences between Terse and JSONP, the main being Terse uses Plack and not CGI. Terse also makes it simpler to provision the data which should be returned from the API (and what should not), finally it adds logging support.
Instantiate a new Terse object.
my $object = Terse->new(%params);
Run terse as a plack application.
Terse->run( login => 'login', logout => 'logout', auth => 'auth', insecure_session => 0, application => Terse->new( auth => sub { ... }, login => sub { ... }, logout => sub { ... } ), plack_env => $env );
The "application" does not need to be a "Terse application", the only requirment is it implements the auth, login and logout methods (go crazy!).
Retrieve params for the request.
$terse->params;
Returns the Plack::Request.
$terse->request;
Retrieve current session data, set in your auth or login methods
$terse->session;
Set the response body data.
$terse->response->foo = { ... };
Set or Retrieve the logger for the application.
$terse->logger($logger); $terse->logger->info(); $terse->logger->err();
Log and raise an error message.
$terse->logError('this is an error message', 404);
Log an info message.
$terse->logInfo('this an info message');
Raise an error message.
$terse->raiseError('this is an error message', 404);
Decode a JSON string.
$terse->response->graft('config', "{...}");
Set JSON to pretty print mode.
$terse->pretty(1);
Encode a perl struct as a JSON string.
$terse->serialize({ ... });
Delay the response for non-blocking I/O based server streaming or long-poll Comet push technology.
$terse->delayed_response(sub { $terse->response->test = 'okay'; return $terse->response; });
Redirect the response to a different url.
$terse->redirect($host_path, \%query_string);
LNATION, <email at lnation.org>
<email at lnation.org>
Please report any bugs or feature requests to bug-terse at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Terse. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-terse at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Terse
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
https://rt.cpan.org/NoAuth/Bugs.html?Dist=Terse
CPAN Ratings
https://cpanratings.perl.org/d/Terse
Search CPAN
https://metacpan.org/release/Terse
This software is Copyright (c) 2022 by LNATION.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Terse, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Terse
CPAN shell
perl -MCPAN -e shell install Terse
For more information on module installation, please visit the detailed CPAN module installation guide.