presto

所属分类:大数据
开发工具:Perl
文件大小:36KB
下载次数:0
上传日期:2017-10-19 16:26:30
上 传 者sh-1993
说明:  Perl REST容器服务器
(Perl REST container server)

文件列表:
Makefile (67, 2017-10-20)
lib (0, 2017-10-20)
lib\perl5 (0, 2017-10-20)
lib\perl5\Net (0, 2017-10-20)
lib\perl5\Net\REST.pm (3882, 2017-10-20)
lib\perl5\Net\REST (0, 2017-10-20)
lib\perl5\Net\REST\BufferedReader.pm (1415, 2017-10-20)
lib\perl5\Net\REST\Request.pm (1589, 2017-10-20)
lib\perl5\Net\REST\Response.pm (2648, 2017-10-20)
test.pl (1847, 2017-10-20)
test01.html (61, 2017-10-20)
test02.txt (38500, 2017-10-20)

presto ====== Perl REST container / server ```perl #!/usr/bin/perl use strict; use Net::REST; my $app = new Net::REST({ port => 2020 }); # hello world example $app->get("/hello", sub { my ($request, $response) = @_; $response->write("hello world!"); }); # chunked transfer encoding example $app->get('/test', sub { my ($req, $res) = @_; # begin chunk transfer encoding $res->begin(); foreach my $p (keys %{ $req->params }) { # send chunk $res->chunk(sprintf("%s => %s\n", $p, $req->params->{$p}) ); } # send an empty chunk to finish the chunk encoding. $res->chunk; }); # named parameters $app->get('/phonebook/add/:name/:number', sub { my ($req, $res) = @_; my $message = sprintf "added %s => %s", $req->params->{name}, $req->params->{number}; $res->write($message); }); # blob parameter # /blog/2014/10/31, date will be '2014/10/31' $app->get('/blog/*date', sub { my ($req, $res) = @_; my $date = $req->params->{date}; $res->write("date: $date"); }); # json example $app->post('/json/post', sub { my ($req, $res) = @_; # request->json will attempt to json-decode request body my $json_hash = $req->json; # do something with that data # response->json is a wrapper of response->write where it # json-encodes the payload, with a content-type of application/json $res->json({ 'status' => 'ok' }); }); # single file example $app->get('/file01', sub { my ($req, $res) = @_; $res->file('./test01.html'); }); $app->get('/file02', sub { my ($req, $res) = @_; $res->file('./test02.txt'); }); # run $app->run(); ``` ## Hooks Example: ```perl use Net::REST; ... my $stats = { loops => 0, clients => 0, idle => 0, }; $app->onConnect(sub { my $clientsocket = shift; # print the client connection info on-connect: printf "%s:%s has connected.\n", $clientsocket->peerhost, $clientsocket->peerport; $stats->{clients}++; }); # could be handled in onLoopEnd, also. Your choice. $app-onLoopBegin(sub { $stats->{loops}++ } ); $app->onNoConnection(sub { $stats->{idle}++; # maybe perform some kind of 'household' task... # like publish stats }); ... $app->run; ``` ## Features - GET, POST, PUT, DELETE handling. - Write standalone REST servers that can then be fronted by nginx, etc. ## Dependencies - Class::MethodMaker - IO::Socket::INET - IO::Select - JSON > curl -L http://cpanmin.us | perl - --sudo --install Class::MethodMaker JSON ## Methods ### Net::REST * get() - HTTP GET method handler. * post() - HTTP POST method handler. * put() - HTTP PUT method handler. * delete() - HTTP DELETE method handler. * onLoopBegin() - hook on loop start. * onLoopEnd() - hook on loop end. * onConnect() - hook on connection established. Provides the callback with the IO::Socket::INET object. * onNoConnection() - hook on no-connections received. * run() - runs the loop event. ### Net::REST::Request * uri - the raw uri of the request * path - the interpreted path of the request. * query - the query portion of the request. * params - any passed parameters on the request line. * error - message of the error encountered. * method - request method. * version - HTTP request version. * headers - HTTP headers of the request. * body - the content body, usually associated with POST, PUT and DELETE. * json - if the body is a json object, it returns a hash representation (decoded json). ### Net::REST::Response * type - the content-type shortcut of the response. * headers - HTTP headers to respond with. * status - HTTP status code to respond with. * write($body, $args) - respond with $body as the payload. $args is a hash reference to include with the response, like the *type* shortcut or to provide headers. * begin($args) - begin a response with **Encoding-Type: chunked**. $args is a hash reference to include with the response, like the *type* shortcut or to provide headers. * chunk($body) - send the next payload chunk. Send and empty chunk() call to end the chunked-type encoding. * redirect($url) - redirect to the url provided, via HTTP 301. * forward($url) - redirect to the url provided, via HTTP 302. * json($hash) - encodes hash in json and writes it back to the client. * file($filename) - yields the contents of $filename, else 404.

近期下载者

相关文件


收藏者