diff --git a/README.md b/README.md index f72917ef..44f65390 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,13 @@ go get github.com/xyproto/algernon algernon server.lua ``` +### Perl + +```sh +cpan Mojolicious +perl server.pl +``` + And visit . Try opening multiple tabs! ## Changing the port diff --git a/server.pl b/server.pl new file mode 100644 index 00000000..ce1783e7 --- /dev/null +++ b/server.pl @@ -0,0 +1,34 @@ +# This file provided by Facebook is for non-commercial testing and evaluation +# purposes only. Facebook reserves all rights not expressly granted. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +use Mojolicious::Lite; +use Mojo::JSON qw(encode_json decode_json); + +app->static->paths->[0] = './public'; + +any '/' => sub { $_[0]->reply->static('index.html') }; + +any [qw(GET POST)] => '/api/comments' => sub { + my $self = shift; + my $comments = decode_json (do { local(@ARGV,$/) = 'comments.json';<> }); + + if ($self->req->method eq 'POST') + { + push @$comments, { + author => $self->param('author'), + text => $self->param('text'), + }; + open my $FILE, '>', 'comments.json'; + print $FILE encode_json($comments); + } + $self->render(json => $comments); +}; +my $port = $ENV{PORT} || 3000; +app->start('daemon', '-l', "http://*:$port");