summary refs log tree commit diff stats
path: root/lib/Crater/Service.rakumod
blob: 1119e5bacafeef0064f1730d17dfc36a5d129e87 (plain) (blame)
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
use Config::TOML;

use Cro::HTTP::Server;
use Cro::HTTP::Log::File;
use Cro::HTTP::Session::InMemory;

use Crater::Routes;
use Crater::Gallery;
use Crater::Session;

#| Crater is a photo gallery
sub MAIN(
    IO() :$config where *.IO.f = 'resources/config.toml', #= configuration file
    IO() :$directory! where *.IO.d, #= gallery directory (takes absolute path)
    Str :$password = '0x', #= password for authentication
    Bool :$verbose, #= increase verbosity
) is export {
    put "Initialized: {now - INIT now}";
    put "Gallery: {$directory.absolute}";
    put "Password: $password";

    my %conf = from-toml($config.slurp);
    %conf<server><host> //= %*ENV<CRATER_HOST>;
    %conf<server><port> //= %*ENV<CRATER_PORT>;

    my Crater::Gallery $gallery = Crater::Gallery.new(:$directory);

    my Cro::Service $http = Cro::HTTP::Server.new(
        http => <1.1>,
        allowed-methods => <GET POST>,
        host => %conf<server><host> || die("host not set"),
        port => %conf<server><port> || die("port not set"),
        application => routes(:$password, :$gallery),
        before => [
                   Cro::HTTP::Session::InMemory[Crater::Session].new(
                       expiration => Duration.new(60 * 15)
                   );
               ],
        after => [
                  Cro::HTTP::Log::File.new(logs => $*OUT, errors => $*ERR)
              ]
    );

    $http.start;
    say "Listening at http://%conf<server><host>:%conf<server><port>";

    react {
        whenever signal(SIGINT) {
            say "Shutting down...";
            $http.stop;
            done;
        }
    }
}