| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Handler::PSGI; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Handler::PSGI - PSGI Handler. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | # run the app in PSGI mode and return the PSGI closure subroutine | ||
| 24 | my $psgi = $app->object("Nile::Handler::PSGI")->run(); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Nile::Handler::PSGI - PSGI Handler. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | use Nile::Base; | ||
| 33 | use Nile::App; | ||
| 34 | use Plack::Builder; | ||
| 35 | use Plack::Middleware::Deflater; | ||
| 36 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 37 | sub run { | ||
| 38 | |||
| 39 | my ($self) = shift; | ||
| 40 | |||
| 41 | #$app->log->debug("PSGI app handler start"); | ||
| 42 | |||
| 43 | my $apps = $self->app(); # Nile object | ||
| 44 | |||
| 45 | # PSGI mode. PSGI app will loop inside this closure, so reset any user session shared data inside it. | ||
| 46 | my $psgi = sub { | ||
| 47 | |||
| 48 | my $env = shift; | ||
| 49 | |||
| 50 | my $app = Nile::App->new(app => $apps); | ||
| 51 | |||
| 52 | #$app->dump($env); | ||
| 53 | |||
| 54 | #$app->start_logger; | ||
| 55 | #$app->log->debug("PSGI request start"); | ||
| 56 | |||
| 57 | $app->env($env); | ||
| 58 | |||
| 59 | #*ENV = $env; | ||
| 60 | #%ENV = %$env; | ||
| 61 | #---------------------------------------------- | ||
| 62 | $app->new_request($app->env()); | ||
| 63 | |||
| 64 | my $request = $app->request(); | ||
| 65 | $self->dump($request); | ||
| 66 | |||
| 67 | $app->response($app->object("Nile::HTTP::Response")); | ||
| 68 | my $response = $app->response(); | ||
| 69 | |||
| 70 | $app->start(); | ||
| 71 | |||
| 72 | $app->hook->off_request(); | ||
| 73 | #---------------------------------------------- | ||
| 74 | #my $path = $app->env->{PATH_INFO} || $app->env->{REQUEST_URI}; | ||
| 75 | #$path = $app->file->catfile($app->var->get("path"), $path); | ||
| 76 | #if (-f $path) { | ||
| 77 | # # file response: /favicon.ico | ||
| 78 | # $response->file_response($path); | ||
| 79 | # $app->stop_logger; | ||
| 80 | # return $response->finalize; | ||
| 81 | #} | ||
| 82 | #-------------------------------------------------- | ||
| 83 | # dispatch the action | ||
| 84 | my $content = $app->dispatcher->dispatch(); | ||
| 85 | #-------------------------------------------------- | ||
| 86 | $app->hook->on_response(); | ||
| 87 | |||
| 88 | my $ctype = $response->header('Content-Type'); | ||
| 89 | if ($app->charset && $ctype && $app->content_type_text($ctype)) { | ||
| 90 | $response->header('Content-Type' => "$ctype; charset=" . $app->charset) if $ctype !~ /charset/i; | ||
| 91 | } | ||
| 92 | |||
| 93 | $response->content($content); | ||
| 94 | |||
| 95 | if (!$ctype) { | ||
| 96 | $response->content_type('text/html;charset=' . $app->charset || "utf-8"); | ||
| 97 | } | ||
| 98 | |||
| 99 | if (!defined $response->header('Content-Length')) { | ||
| 100 | use bytes; # turn off character semantics | ||
| 101 | $response->header('Content-Length' => length($content)); | ||
| 102 | } | ||
| 103 | |||
| 104 | #$response->code(200) unless ($response->code); | ||
| 105 | #$response->content_type('text/html') unless ($response->content_type); | ||
| 106 | |||
| 107 | #$response->content_encoding('gzip'); | ||
| 108 | #$response->cookies->{username} = {value => 'mewsoft', path => "/", domain => '.mewsoft.com', expires => time + 24 * 60 * 60,}; | ||
| 109 | #$response->header(Content_Base => 'http://www.mewsoft.com/'); | ||
| 110 | #$response->header(Accept => "text/html, text/plain, image/*"); | ||
| 111 | #$response->header(MIME_Version => '1.0', User_Agent => 'Nile Web Client/0.26'); | ||
| 112 | #$response->content("Hello world content."); | ||
| 113 | |||
| 114 | $response->content($content); | ||
| 115 | |||
| 116 | #$app->log->debug("PSGI request end"); | ||
| 117 | $app->stop_logger(); | ||
| 118 | |||
| 119 | $app->hook->off_response(); | ||
| 120 | # return the PSGI response array ref | ||
| 121 | return $response->finalize(); | ||
| 122 | }; | ||
| 123 | |||
| 124 | #return $psgi; | ||
| 125 | |||
| 126 | # support Middleware | ||
| 127 | return builder { | ||
| 128 | # serve static files with Plack | ||
| 129 | #enable "Static", path => qr{^/(web|file|theme)/}, root => './'; | ||
| 130 | #Plack::Middleware::Deflater | ||
| 131 | enable "Static", | ||
| 132 | path => sub { | ||
| 133 | my $path = $_; | ||
| 134 | if ( $path =~ m/^\/(web|file|theme|favicon\.)/ ) { | ||
| 135 | # if matched, the value of $_ is being used as a request path, modify it to your needs | ||
| 136 | $_ = $path; | ||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | return 0; | ||
| 140 | }, | ||
| 141 | root => './'; | ||
| 142 | |||
| 143 | #enable "Deflater", content_type => ['text/css','text/html','text/javascript','application/javascript'], vary_user_agent => 1; | ||
| 144 | |||
| 145 | $psgi; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 149 | |||
| 150 | =pod | ||
| 151 | |||
| 152 | =head1 Bugs | ||
| 153 | |||
| 154 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 155 | |||
| 156 | =head1 HOMEPAGE | ||
| 157 | |||
| 158 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 159 | |||
| 160 | =head1 SOURCE | ||
| 161 | |||
| 162 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 163 | |||
| 164 | =head1 SEE ALSO | ||
| 165 | |||
| 166 | See L<Nile> for details about the complete framework. | ||
| 167 | |||
| 168 | =head1 AUTHOR | ||
| 169 | |||
| 170 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 171 | Website: http://www.mewsoft.com | ||
| 172 | |||
| 173 | =head1 COPYRIGHT AND LICENSE | ||
| 174 | |||
| 175 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 176 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 177 | |||
| 178 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | 1; |