Newer Older
159 lines | 3.241kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojo::Server;
2
use Mojo::Base 'Mojo::EventEmitter';
3

            
4
use Carp 'croak';
5
use Mojo::Loader;
6
use Mojo::Util 'md5_sum';
7
use Scalar::Util 'blessed';
8

            
9
has app => sub { shift->build_app('Mojo::HelloWorld') };
10

            
11
sub new {
12
  my $self = shift->SUPER::new(@_);
13
  $self->on(request => sub { shift->app->handler(shift) });
14
  return $self;
15
}
16

            
17
sub build_app {
18
  my ($self, $app) = @_;
19
  local $ENV{MOJO_EXE};
20
  return $app->new unless my $e = Mojo::Loader->new->load($app);
21
  die ref $e ? $e : qq{Couldn't find application class "$app".\n};
22
}
23

            
24
sub build_tx { shift->app->build_tx }
25

            
26
sub load_app {
27
  my ($self, $path) = @_;
28

            
29
  # Clean environment (reset FindBin defensively)
30
  {
31
    local $0 = $path;
32
    require FindBin;
33
    FindBin->again;
34
    local $ENV{MOJO_APP_LOADER} = 1;
35
    local $ENV{MOJO_EXE};
36

            
37
    # Try to load application from script into sandbox
38
    my $app = eval sprintf <<'EOF', md5_sum($path . $$);
39
package Mojo::Server::SandBox::%s;
40
my $app = do $path;
41
if (!$app && (my $e = $@ || $!)) { die $e }
42
$app;
43
EOF
44
    die qq{Couldn't load application from file "$path": $@} if !$app && $@;
45
    die qq{File "$path" did not return an application object.\n}
46
      unless blessed $app && $app->isa('Mojo');
47
    $self->app($app);
48
  };
49
  FindBin->again;
50

            
51
  return $self->app;
52
}
53

            
54
sub run { croak 'Method "run" not implemented by subclass' }
55

            
56
1;
57

            
58
=encoding utf8
59

            
60
=head1 NAME
61

            
62
Mojo::Server - HTTP server base class
63

            
64
=head1 SYNOPSIS
65

            
66
  package Mojo::Server::MyServer;
67
  use Mojo::Base 'Mojo::Server';
68

            
69
  sub run {
70
    my $self = shift;
71

            
72
    # Get a transaction
73
    my $tx = $self->build_tx;
74

            
75
    # Emit "request" event
76
    $self->emit(request => $tx);
77
  }
78

            
79
=head1 DESCRIPTION
80

            
81
L<Mojo::Server> is an abstract HTTP server base class.
82

            
83
=head1 EVENTS
84

            
85
L<Mojo::Server> inherits all events from L<Mojo::EventEmitter> and can emit
86
the following new ones.
87

            
88
=head2 request
89

            
90
  $server->on(request => sub {
91
    my ($server, $tx) = @_;
92
    ...
93
  });
94

            
95
Emitted when a request is ready and needs to be handled.
96

            
97
  $server->unsubscribe('request');
98
  $server->on(request => sub {
99
    my ($server, $tx) = @_;
100
    $tx->res->code(200);
101
    $tx->res->headers->content_type('text/plain');
102
    $tx->res->body('Hello World!');
103
    $tx->resume;
104
  });
105

            
106
=head1 ATTRIBUTES
107

            
108
L<Mojo::Server> implements the following attributes.
109

            
110
=head2 app
111

            
112
  my $app = $server->app;
113
  $server = $server->app(MojoSubclass->new);
114

            
115
Application this server handles, defaults to a L<Mojo::HelloWorld> object.
116

            
117
=head1 METHODS
118

            
119
L<Mojo::Server> inherits all methods from L<Mojo::EventEmitter> and implements
120
the following new ones.
121

            
122
=head2 new
123

            
124
  my $server = Mojo::Server->new;
125

            
126
Construct a new L<Mojo::Server> object and subscribe to L</"request"> event
127
with default request handling.
128

            
129
=head2 build_app
130

            
131
  my $app = $server->build_app('Mojo::HelloWorld');
132

            
133
Build application from class.
134

            
135
=head2 build_tx
136

            
137
  my $tx = $server->build_tx;
138

            
139
Let application build a transaction.
140

            
141
=head2 load_app
142

            
143
  my $app = $server->load_app('/home/sri/myapp.pl');
144

            
145
Load application from script.
146

            
147
  say Mojo::Server->new->load_app('./myapp.pl')->home;
148

            
149
=head2 run
150

            
151
  $server->run;
152

            
153
Run server. Meant to be overloaded in a subclass.
154

            
155
=head1 SEE ALSO
156

            
157
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
158

            
159
=cut