-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.psgi
108 lines (89 loc) · 3.19 KB
/
app.psgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw/$Bin/;
use Plack;
use Plack::Builder;
use Plack::App::File;
use Plack::Session::Store::Cache;
use CHI;
## OAuth
use lib "$Bin/lib";
use KinderGarden::Basic;
use KinderGarden::OAuth;
## KinderGarden-Web
use lib "$Bin/Web";
use Dancer ':syntax';
setting apphandler => 'PSGI';
## App WhereIlive
use Mojo::Server::PSGI;
use lib "$Bin/Apps";
my $root = KinderGarden::Basic->root;
builder {
enable_if { $ENV{KINDERGARDEN_DEBUG} or $_[0]->{REMOTE_ADDR} eq '127.0.0.1' } 'Debug', panels => [
qw(Timer),
['DBITrace', level => 2]
];
enable_if { ($ENV{KINDERGARDEN_DEBUG} or $_[0]->{REMOTE_ADDR} eq '127.0.0.1') and $^O ne 'MSWin32' } 'Debug::Memory';
enable_if { $ENV{KINDERGARDEN_DEBUG} or $_[0]->{REMOTE_ADDR} eq '127.0.0.1' } "StackTrace";
enable_if { $ENV{KINDERGARDEN_DEBUG} or $_[0]->{REMOTE_ADDR} eq '127.0.0.1' } "ConsoleLogger";
enable 'Session', store => Plack::Session::Store::Cache->new(
cache => CHI->new(driver => 'FastMmap')
);
mount '/favicon.ico' => Plack::App::File->new( file => "$root/static/favicon.ico" ),
mount '/static/' => Plack::App::File->new( root => "$root/static" ),
mount '/static/docs/' => builder {
enable_if { $_[0]->{PATH_INFO} =~ /\.html/ } 'FileWrap',
headers => ["$root/static/docs/header.html"], footers => ["$root/static/docs/footer.html"];
Plack::App::File->new( root => "$root/static/docs" )->to_app;
},
my $oauth_provider_yml = -e "$root/conf/oauth_local.yml" ? "$root/conf/oauth_local.yml" : "$root/conf/oauth.yml";
mount "/oauth" => builder {
enable 'OAuth',
on_success => sub {
my ( $self, $token ) = @_;
KinderGarden::OAuth->new( status => 'success', handler => $self, token => $token )->response;
},
on_error => sub {
my ( $self, $token ) = @_;
KinderGarden::OAuth->new( status => 'error', handler => $self, token => $token )->response;
},
providers => $oauth_provider_yml;
},
mount '/app/whereilive' => sub {
_mojo_wrap('KinderGarden::App::WhereILive', @_);
},
mount '/comment' => sub {
my $env = shift;
_dance_wrap('KinderGarden::Comment', $env);
},
mount '/' => sub {
my $env = shift;
_dance_wrap('KinderGarden::Web', $env);
},
};
sub _dance_wrap {
my $app = shift;
my $env = shift;
lib->import("$root/lib");
lib->import("$root/Web");
local $ENV{DANCER_APPDIR} = "$root/Web";
local $ENV{DANCER_CONFDIR} = "$root/Web";
load_app $app;
Dancer::App->set_running_app($app);
setting appdir => "$root/Web";
setting confdir => "$root/Web";
Dancer::Config->load;
# damn, how many fixes should I write it here!
setting 'views' => "$root/templates";
setting 'public' => "$root/static";
my $request = Dancer::Request->new( env => $env );
Dancer->dance($request);
}
sub _mojo_wrap {
my $app = shift;
lib->import("$root/lib");
lib->import("$root/Apps");
my $psgi = Mojo::Server::PSGI->new(app_class => $app);
$psgi->run(@_);
}