summary refs log tree commit diff stats
path: root/lib/Crater/Service.rakumod
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2022-06-09 21:12:03 +0530
committerAndinus <andinus@nand.sh>2022-06-09 21:12:03 +0530
commit2085b4cac3a86d59360531d48251c8ab39dec0dd (patch)
tree53cabc820312da2abff2bbb9b44132b61302cad1 /lib/Crater/Service.rakumod
parent48df36de5aa962b32d3313d6a9d2ace6a5fdac11 (diff)
downloadcrater-2085b4cac3a86d59360531d48251c8ab39dec0dd.tar.gz
Initial Gallery version
- Handles login, logout, simple directories.
Diffstat (limited to 'lib/Crater/Service.rakumod')
-rw-r--r--lib/Crater/Service.rakumod52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/Crater/Service.rakumod b/lib/Crater/Service.rakumod
new file mode 100644
index 0000000..c0bdd7a
--- /dev/null
+++ b/lib/Crater/Service.rakumod
@@ -0,0 +1,52 @@
+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}";
+
+    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>,
+        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;
+        }
+    }
+}