|
| 1 | +# This file provided by Facebook is for non-commercial testing and evaluation |
| 2 | +# purposes only. Facebook reserves all rights not expressly granted. |
| 3 | +# |
| 4 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 7 | +# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 8 | +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 9 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 10 | + |
| 11 | +use Mojolicious::Lite; |
| 12 | +use Mojo::JSON qw(encode_json decode_json); |
| 13 | + |
| 14 | +app->static->paths->[0] = './public'; |
| 15 | + |
| 16 | +any '/' => sub { $_[0]->reply->static('index.html') }; |
| 17 | + |
| 18 | +any [qw(GET POST)] => '/api/comments' => sub { |
| 19 | + my $self = shift; |
| 20 | + my $comments = decode_json (do { local(@ARGV,$/) = 'comments.json';<> }); |
| 21 | + |
| 22 | + if ($self->req->method eq 'POST') |
| 23 | + { |
| 24 | + push @$comments, { |
| 25 | + author => $self->param('author'), |
| 26 | + text => $self->param('text'), |
| 27 | + }; |
| 28 | + open my $FILE, '>', 'comments.json'; |
| 29 | + print $FILE encode_json($comments); |
| 30 | + } |
| 31 | + $self->render(json => $comments); |
| 32 | +}; |
| 33 | +my $port = $ENV{PORT} || 3000; |
| 34 | +app->start('daemon', '-l', "http://*:$port"); |
0 commit comments