-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.pl
38 lines (33 loc) · 1.4 KB
/
server.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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 Time::HiRes qw(gettimeofday);
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';<> });
$self->res->headers->cache_control('no-cache');
$self->res->headers->access_control_allow_origin('*');
if ($self->req->method eq 'POST')
{
push @$comments, {
id => int(gettimeofday * 1000),
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");