35 |
|
|
36 |
=cut |
=cut |
37 |
|
|
38 |
|
our $editor = Frey::Editor->new; |
39 |
|
|
40 |
sub run { |
sub run { |
41 |
my ( $self, $port ) = @_; |
my ( $self, $port ) = @_; |
42 |
$server = Continuity->new( |
$server = Continuity->new( |
45 |
cookie_session => 'sid', |
cookie_session => 'sid', |
46 |
callback => \&main, |
callback => \&main, |
47 |
debug_level => 2, |
debug_level => 2, |
48 |
staticp => sub { $_[0]->url =~ m/^(static|var).*\.(jpg|jpeg|gif|png|css|ico|js|html?|xml|json|ya?ml)(\?.*)?$/ }, |
staticp => sub { |
49 |
|
$_[0]->url =~ m{^/+(static|var).*\.(jpg|jpeg|gif|png|css|ico|js|html?|xml|json|ya?ml)(\?.*)?$} |
50 |
|
}, |
51 |
); |
); |
52 |
$Module::Reload::Debug = 1; # auto if debug_level > 1 |
$Module::Reload::Debug = 1; # auto if debug_level > 1 |
53 |
Frey::ClassLoader->new->load_all_classes(); |
Frey::ClassLoader->new->load_all_classes(); |
54 |
|
$editor->switch_screen if $ENV{FREY_SWITCH_SCREEN}; |
55 |
$server->loop; |
$server->loop; |
56 |
} |
} |
57 |
|
|
65 |
sub main { |
sub main { |
66 |
my ($req) = @_; |
my ($req) = @_; |
67 |
|
|
68 |
|
# $req->send_headers("X-Frey-VERSION: $Frey::VERSION"); |
69 |
|
|
70 |
my $path = $req->request->url->path; |
my $path = $req->request->url->path; |
71 |
|
|
72 |
eval { |
eval { |
73 |
|
|
|
sub refresh { |
|
|
my $url = shift || '/'; |
|
|
warn "# refresh $url"; |
|
|
qq| |
|
|
<html> |
|
|
<head> |
|
|
<META HTTP-EQUIV="Refresh" CONTENT="2; URL=$url"></META> |
|
|
</head> |
|
|
<body> |
|
|
Refresh <a href="$url"><tt>$url</tt></a> |
|
|
</body> |
|
|
</html> |
|
|
\n\r\n\r |
|
|
|; # XXX newlines at end are important to flush content to browser |
|
|
} |
|
|
|
|
74 |
if ( $path =~ m{/reload(.*)} ) { |
if ( $path =~ m{/reload(.*)} ) { |
75 |
Frey::Server->new->load_config; |
|
76 |
Module::Reload->check; |
$ENV{FREY_NO_LOG} = 1; |
77 |
warn "# reload done"; |
my $cmd = "perl -c $0"; |
78 |
$req->print( refresh( $1 ) ); |
warn "# check config with $cmd"; |
79 |
return; |
if ( system($cmd) == 0 ) { |
80 |
|
$req->print( "\r\n" ); |
81 |
|
my $server = Frey::Server->new; |
82 |
|
$server->load_config; |
83 |
|
$req->print( "\r\n" ); |
84 |
|
Module::Reload->check; |
85 |
|
$req->print( "\r\n" ); |
86 |
|
$req->print( refresh( $1, 1 ) ); |
87 |
|
$req->print( "\r\n" ); |
88 |
|
warn "# reload done"; |
89 |
|
return; |
90 |
|
} else { |
91 |
|
warn "ERROR: $?"; |
92 |
|
} |
93 |
|
$ENV{FREY_NO_LOG} = 0; |
94 |
|
|
95 |
} elsif ( $path =~ m{/exit(.*)} ) { |
} elsif ( $path =~ m{/exit(.*)} ) { |
96 |
# FIXME do we need some kind of check here for production? :-) |
# FIXME do we need some kind of check here for production? :-) |
97 |
# ./bin/dev.sh will restart us during development |
# ./bin/dev.sh will restart us during development |
98 |
$req->print( refresh( $1 ) ); |
$req->print( refresh( $1, 2 ) ); |
99 |
|
$req->print( "\r\n" ); |
100 |
exit; |
exit; |
101 |
} |
} |
102 |
|
|
114 |
|
|
115 |
my $f; |
my $f; |
116 |
|
|
|
my $editor = Frey::Editor->new; |
|
|
|
|
117 |
# shared run params |
# shared run params |
118 |
my $run = { |
my $run = { |
119 |
request_url => $req->request->url, |
request_url => $req->request->url, |
132 |
$f->request( $req ); |
$f->request( $req ); |
133 |
} elsif ( $path =~ $editor->url_regex ) { |
} elsif ( $path =~ $editor->url_regex ) { |
134 |
$req->print( $editor->command( $path ) ); |
$req->print( $editor->command( $path ) ); |
135 |
system( $editor->command( $path ) ); |
$editor->command( $path ); |
136 |
return; |
return; |
137 |
} elsif ( |
} elsif ( |
138 |
$path =~ m{/([^/]+)/(as_\w+)/?([^/]+)?} |
$path =~ m{/([^/]+)/(\w*as_\w+)/?([^/]+)?} |
139 |
) { |
) { |
140 |
my $class = rest2class $1; |
my $class = rest2class $1; |
141 |
warn "# run $path -> $class $2"; |
warn "# run $path -> $class $2"; |
142 |
$run->{format} = $3 if $3; |
$run->{format} = $3 if $3; |
143 |
$params{request_url} = $req->request->url; |
$params{request_url} = $req->request->url; |
144 |
|
$req->print( "\r\n\r\n" ); # send something to browser so we don't time-out |
145 |
$f = Frey::Run->new( class => $class, params => \%params, run => $2, %$run ); |
$f = Frey::Run->new( class => $class, params => \%params, run => $2, %$run ); |
146 |
} elsif ( |
} elsif ( |
147 |
$path =~ m{/([^/]+)/?$} |
$path =~ m{/([^/]+)/?$} |
156 |
if ( $f ) { |
if ( $f ) { |
157 |
$f->clean_status; |
$f->clean_status; |
158 |
$f->add_status( { request => $req } ); |
$f->add_status( { request => $req } ); |
159 |
warn "## status ", dump( map { keys %$_ } $f->status ); |
$f->status_parts; |
160 |
my $html = $f->html; |
if ( my $html = $f->html ) { |
161 |
die "no html output" unless $html; |
warn "## html ",length($html)," bytes"; |
162 |
warn "## html ",length($html)," bytes"; |
$req->print( $html ); |
163 |
$req->print( "$html\n" ); |
} else { |
164 |
|
confess "no output from $f"; |
165 |
|
} |
166 |
} else { |
} else { |
167 |
warn "# can't call request on nothing!"; |
confess "# can't call request on nothing!"; |
168 |
} |
} |
169 |
|
|
170 |
}; |
}; |
171 |
|
|
172 |
if ( $@ ) { |
if ( $@ ) { |
173 |
warn $@; |
warn "SERVER ERROR: $@"; |
174 |
$req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it |
# $req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it |
175 |
$req->print( qq{<pre class="frey-error">$@<pre>} ); |
$req->print( qq{<pre class="frey-error">$@<pre>\r\n\r\n} ); |
176 |
# Carp::REPL::repl; |
# Carp::REPL::repl; |
177 |
} |
} |
178 |
|
|
186 |
send_message($req); |
send_message($req); |
187 |
} |
} |
188 |
|
|
189 |
if ($req->conn ) { |
if ( $req->conn ) { |
190 |
|
$req->print( "\r\n" ); # flush |
191 |
$req->conn->close; |
$req->conn->close; |
192 |
warn "## close connection: $@"; |
warn "## close connection: $@"; |
193 |
} |
} |
230 |
} |
} |
231 |
} |
} |
232 |
|
|
233 |
|
sub refresh { |
234 |
|
my ( $url, $time ) = @_; |
235 |
|
$url ||= '/'; |
236 |
|
$time ||= 1; |
237 |
|
warn "# refresh $url"; |
238 |
|
qq| |
239 |
|
<html> |
240 |
|
<head> |
241 |
|
<META HTTP-EQUIV="Refresh" CONTENT="$time; URL=$url"></META> |
242 |
|
</head> |
243 |
|
<body> |
244 |
|
Refresh <a href="$url"><tt>$url</tt></a> in $time sec |
245 |
|
</body> |
246 |
|
</html> |
247 |
|
\n\r\n\r |
248 |
|
|; # XXX newlines at end are important to flush content to browser |
249 |
|
} |
250 |
|
|
251 |
1; |
1; |