Newer Older
188 lines | 4.204kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojo;
2
use Mojo::Base -base;
3

            
4
# "Professor: These old Doomsday devices are dangerously unstable. I'll rest
5
#             easier not knowing where they are."
6
use Carp 'croak';
7
use Mojo::Home;
8
use Mojo::Log;
9
use Mojo::Transaction::HTTP;
10
use Mojo::UserAgent;
11
use Scalar::Util 'weaken';
12

            
13
has home => sub { Mojo::Home->new };
14
has log  => sub { Mojo::Log->new };
15
has ua   => sub {
16
  my $self = shift;
17

            
18
  my $ua = Mojo::UserAgent->new;
19
  weaken $ua->server->app($self)->{app};
20
  weaken $self;
21
  $ua->on(error => sub { $self->log->error($_[1]) });
22

            
23
  return $ua;
24
};
25

            
26
sub new {
27
  my $self = shift->SUPER::new(@_);
28

            
29
  # Check if we have a log directory
30
  my $home = $self->home;
31
  $home->detect(ref $self) unless @{$home->parts};
32
  $self->log->path($home->rel_file('log/mojo.log'))
33
    if -w $home->rel_file('log');
34

            
35
  return $self;
36
}
37

            
38
sub build_tx { Mojo::Transaction::HTTP->new }
39

            
40
sub config { shift->_dict(config => @_) }
41

            
42
sub handler { croak 'Method "handler" not implemented in subclass' }
43

            
44
sub _dict {
45
  my ($self, $name) = (shift, shift);
46

            
47
  # Hash
48
  my $dict = $self->{$name} ||= {};
49
  return $dict unless @_;
50

            
51
  # Get
52
  return $dict->{$_[0]} unless @_ > 1 || ref $_[0];
53

            
54
  # Set
55
  %$dict = (%$dict, %{ref $_[0] ? $_[0] : {@_}});
56

            
57
  return $self;
58
}
59

            
60
1;
61

            
62
=encoding utf8
63

            
64
=head1 NAME
65

            
66
Mojo - Duct tape for the HTML5 web!
67

            
68
=head1 SYNOPSIS
69

            
70
  package MyApp;
71
  use Mojo::Base 'Mojo';
72

            
73
  # All the complexities of CGI, PSGI, HTTP and WebSockets get reduced to a
74
  # single method call!
75
  sub handler {
76
    my ($self, $tx) = @_;
77

            
78
    # Request
79
    my $method = $tx->req->method;
80
    my $path   = $tx->req->url->path;
81

            
82
    # Response
83
    $tx->res->code(200);
84
    $tx->res->headers->content_type('text/plain');
85
    $tx->res->body("$method request for $path!");
86

            
87
    # Resume transaction
88
    $tx->resume;
89
  }
90

            
91
=head1 DESCRIPTION
92

            
93
Mojo provides a flexible runtime environment for Perl real-time web
94
frameworks. It provides all the basic tools and helpers needed to write
95
simple web applications and higher level web frameworks, such as
96
L<Mojolicious>.
97

            
98
See L<Mojolicious::Guides> for more!
99

            
100
=head1 ATTRIBUTES
101

            
102
L<Mojo> implements the following attributes.
103

            
104
=head2 home
105

            
106
  my $home = $app->home;
107
  $app     = $app->home(Mojo::Home->new);
108

            
109
The home directory of your application, defaults to a L<Mojo::Home> object
110
which stringifies to the actual path.
111

            
112
  # Generate portable path relative to home directory
113
  my $path = $app->home->rel_file('data/important.txt');
114

            
115
=head2 log
116

            
117
  my $log = $app->log;
118
  $app    = $app->log(Mojo::Log->new);
119

            
120
The logging layer of your application, defaults to a L<Mojo::Log> object.
121

            
122
  # Log debug message
123
  $app->log->debug('It works!');
124

            
125
=head2 ua
126

            
127
  my $ua = $app->ua;
128
  $app   = $app->ua(Mojo::UserAgent->new);
129

            
130
A full featured HTTP user agent for use in your applications, defaults to a
131
L<Mojo::UserAgent> object. Note that this user agent should not be used in
132
plugins, since non-blocking requests that are already in progress will
133
interfere with new blocking ones.
134

            
135
  # Perform blocking request
136
  say $app->ua->get('example.com')->res->body;
137

            
138
=head1 METHODS
139

            
140
L<Mojo> inherits all methods from L<Mojo::Base> and implements the following
141
new ones.
142

            
143
=head2 new
144

            
145
  my $app = Mojo->new;
146

            
147
Construct a new L<Mojo> application. Will automatically detect your home
148
directory if necessary and set up logging to C<log/mojo.log> if there's a
149
C<log> directory.
150

            
151
=head2 build_tx
152

            
153
  my $tx = $app->build_tx;
154

            
155
Transaction builder, defaults to building a L<Mojo::Transaction::HTTP>
156
object.
157

            
158
=head2 config
159

            
160
  my $hash = $app->config;
161
  my $foo  = $app->config('foo');
162
  $app     = $app->config({foo => 'bar'});
163
  $app     = $app->config(foo => 'bar');
164

            
165
Application configuration.
166

            
167
  # Remove value
168
  my $foo = delete $app->config->{foo};
169

            
170
=head2 handler
171

            
172
  $app->handler(Mojo::Transaction::HTTP->new);
173

            
174
The handler is the main entry point to your application or framework and will
175
be called for each new transaction, which will usually be a
176
L<Mojo::Transaction::HTTP> or L<Mojo::Transaction::WebSocket> object. Meant to
177
be overloaded in a subclass.
178

            
179
  sub handler {
180
    my ($self, $tx) = @_;
181
    ...
182
  }
183

            
184
=head1 SEE ALSO
185

            
186
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
187

            
188
=cut