Skip to content

Commit 208e00f

Browse files
committed
Example web server in Perl
1 parent 66ccecd commit 208e00f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ go get github.com/xyproto/algernon
5454
algernon server.lua
5555
```
5656

57+
### Perl
58+
59+
```sh
60+
cpan Mojolicious
61+
perl server.pl
62+
```
63+
5764
And visit <http://localhost:3000/>. Try opening multiple tabs!
5865

5966
## Changing the port

server.pl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

Comments
 (0)