LoginSignup
1
1

More than 5 years have passed since last update.

Mojo::Server::Daemonで簡易なWebサーバを作る

Posted at

以下のように、Mojo::Server::Daemonで簡易なWebサーバを作ることができます。

sample.pl
use strict;
use warnings;
use utf8;
use Encode;
use Mojo::Server::Daemon;

my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']);
$daemon->unsubscribe('request');
$daemon->on(request => sub {
    my ($daemon, $tx) = @_;

    # Request
    my $method = $tx->req->method;
    my $path   = $tx->req->url->path;

    if ($path eq '/test.html') {
         $tx->res->code(200);
          $tx->res->headers->content_type('text/html');
         $tx->res->body(encode_utf8('<html><head><title>テスト</title></head><body><h1>テスト</h1></body></html>'));

    }
    elsif ($path eq '/test.txt') {
         $tx->res->code(200);
         $tx->res->headers->content_type('text/plain');
         $tx->res->body(encode_utf8('テスト'));
    }
    else {
         $tx->res->code(404);
         $tx->res->body('Not Found');
    }

    # Resume transaction
    $tx->resume
});
$daemon->run;

Test::TCPを使えば、一時的にWebサーバを立ち上げてテストすることができます。

sample.pl
use strict;
use warnings;
use utf8;
use Encode;
use Mojo::Server::Daemon;
use Test::More;
use Test::TCP;
use Mojo::UserAgent;


test_tcp(
    client => sub {
        my $post = shift;

        my $url = 'http://localhost:'.$post;
        my $client = Mojo::UserAgent->new;

        my $tx = $client->get($url.'/test.html');
        if (my $res = $tx->success) {
            is decode_utf8($res->body), '<html><head><title>テスト</title></head><body><h1>テスト</h1></body></html>';
        }

        $tx = $client->get($url.'/test.txt');
        if (my $res = $tx->success) {
            is decode_utf8($res->body), 'テスト';
        }
    },
    server => sub {
        my $port = shift;
        my $daemon = build_server($port);
        $daemon->run;
    },
);


sub build_server {
    my ($port) = @_;

    my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:'.$port]);
    $daemon->unsubscribe('request');
    $daemon->on(request => sub {
        my ($daemon, $tx) = @_;

        # Request
        my $method = $tx->req->method;
        my $path   = $tx->req->url->path;

        if ($path eq '/test.html') {
             $tx->res->code(200);
              $tx->res->headers->content_type('text/html');
             $tx->res->body(encode_utf8('<html><head><title>テスト</title></head><body><h1>テスト</h1></body></html>'));

        }
        elsif ($path eq '/test.txt') {
             $tx->res->code(200);
             $tx->res->headers->content_type('text/plain');
             $tx->res->body(encode_utf8('テスト'));
        }
        else {
             $tx->res->code(404);
             $tx->res->body('Not Found');
        }

        # Resume transaction
        $tx->resume
    });
    $daemon;
}

done_testing;
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1