/
Loader.pm6
56 lines (40 loc) · 1.17 KB
/
Loader.pm6
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
use v6;
unit role Smack::Loader;
use Smack::Handler;
has &.app is rw;
method watch(*@paths) {
# do nothing. override in subclass
}
method auto(Smack::Loader:U: *@args) {
my $backend = self.guess
or die "Could not auto-guess server implementation. Set it with SMACK_SERVER";
my $server = self.load($backend, @args);
CATCH {
default {
if %*ENV<SMACK_ENV>//'' eq 'development' {
warn "Autoloading '$backend' failed. Falling back to the Standalone. "
~ "(You might need to install Plack::Handler::$backend from the Ecosystem."
~ "Caught error was: $_)\n";
}
$server = self.load('Standalone', @args);
}
}
$server;
}
method load($server, @args) {
my $server-class = ::("Smack::Handler::$server");
require $server-class;
$server-class.new(|@args);
}
method preload-app(&builder) {
&!app = builder();
}
method guess() {
return %.env<PLACK_SERVER> if %.env<PLACK_SERVER>;
return 'CGI' if %.env<GATEWAY_INTERFACE>;
return "Standalone";
}
method env() { %*ENV }
method run(Smack::Handler $server) {
$server.run(&.app);
}