McBain::WithPSGI - Load a McBain API as a RESTful PSGI web service
version 1.001000
# write your API as you normally would, and create # a simple psgi file: #!/usr/bin/perl -w BEGIN { $ENV{MCBAIN_WITH} = 'WithPSGI'; } use warnings; use strict; use MyAPI; my $app = MyAPI->to_app;
McBain::WithPSGI turns your McBain API into a RESTful PSGI web service based on Plack, thus making McBain a web application framework.
McBain::WithPSGI
McBain
The created web service will be a JSON-in JSON-out service. Requests to your application are expected to have a Content-Type of application/json; charset=UTF-8. The JSON body of a request will be the payload. The results of the API will be formatted into JSON as well.
Content-Type
application/json; charset=UTF-8
Note that if an API method does not return a hash-ref, this runner module will automatically turn it into a hash-ref to ensure that conversion into JSON will be possible. The created hash-ref will have one key - holding the method's name, with whatever was returned from the method as its value. For example, if method GET:/divide in topic /math returns an integer (say 7), then the client will get the JSON { "GET:/math/divide": 7 }.
GET:/divide
/math
{ "GET:/math/divide": 7 }
This runner support all methods natively supported by McBain. That is: GET, PUT, POST, DELETE and OPTIONS. To add support for HEAD requests, use Plack::Middleware::Head.
GET
PUT
POST
DELETE
OPTIONS
HEAD
The OPTIONS method is special. It returns a list of all HTTP methods allowed by a specific route (in the Allow header). The response body will be the same hash-ref returned by McBain for OPTIONS requests, JSON encoded.
Allow
None.
Makes the root topic of your API inherit Plack::Component, so that it officially becomes a Plack app. This will provide your API with the to_app() method.
to_app()
Receives the PSGI env hash-ref and creates McBain's standard env hash-ref from it.
Converts the result of an API method to JSON, and returns a standard PSGI response array-ref.
Formats exceptions into JSON and returns a standard PSGI array-ref.
No configuration files are required. To tell McBain to use this runner module, the MCBAIN_WITH environment variable must be set to WithPSGI. See the "SYNOPSIS" for an example.
MCBAIN_WITH
WithPSGI
McBain::WithPSGI depends on the following CPAN modules:
Carp
JSON
Plack
None reported.
Please report any bugs or feature requests to bug-McBain-WithPSGI@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=McBain-WithPSGI.
bug-McBain-WithPSGI@rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc McBain::WithPSGI
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=McBain-WithPSGI
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/McBain-WithPSGI
CPAN Ratings
http://cpanratings.perl.org/d/McBain-WithPSGI
Search CPAN
http://search.cpan.org/dist/McBain-WithPSGI/
Ido Perlmuter <ido@ido50.net>
Copyright (c) 2013, Ido Perlmuter ido@ido50.net.
ido@ido50.net
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either version 5.8.1 or any later version. See perlartistic and perlgpl.
The full text of the license can be found in the LICENSE file included with this module.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install McBain::WithPSGI, copy and paste the appropriate command in to your terminal.
cpanm
cpanm McBain::WithPSGI
CPAN shell
perl -MCPAN -e shell install McBain::WithPSGI
For more information on module installation, please visit the detailed CPAN module installation guide.