Introducing Sub::Spec::HTTP::Server: Perl functions over HTTP, the dead easy way
Below is one way to serve function call requests (e.g. providing API service) using just one line.
Modules hitting CPAN mirrors as we speak (metacpan link: Sub::Spec::HTTP::Server):
# example module to export to HTTP, provides sprintfn()
$ cpanm Text::sprintfn
# module to do the magic, provides 'servepm'
$ cpanm Sub::Spec::HTTP::Server
$ servepm Text::sprintfn
$ wget -O- -q 'http://localhost:5000/api/Text::sprintfn?args:j=["Agent %(num)03d",{"num":7}]'
[200,"OK","Agent 007"]
Features:
- Plack/PSGI-based
- Support HTTPS natively, as well as Unix sockets (using Gepok PSGI server)
- by default support JSON/YAML/PHP/text input/output (support complex arguments instead of just scalars)
- more than just function call: usage, spec, list modules, list subroutines
- propagates Log::Any log messages from server
- use middleware to add authentication, authorization, etc.
- support proxying
I also wrote Sub::Spec::Use, to easily use a remote module:
use Sub::Spec::Use "http://localhost:5000/api/Text::sprintfn" => qw(:all);
printfn "%(num)03d\n", {num=>7};
Just FYI, I have deprecated Sub::Spec in favor of Rinci/Perinci. Rinci/Perinci is basically the same thing, but with formal separation of specification/implementation (there will be Phinci implementation for PHP, Rubinci for Ruby, and Pyrinci for Python).
Perinci modules itself can work with old Sub::Spec specs.
Sub::Spec::HTTP::Server and its servepm script is now replaced by Perinci::Access::HTTP::Server and its peri-htserve script. A little demonstration:
Likewise, Sub::Spec::Use is replaced by Perinci::Use.