summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2017-12-17 19:09:31 +0000
committerdakkar <dakkar@thenautilus.net>2017-12-17 19:09:31 +0000
commitba1caee706c51e25a6ee325f4c338aa4d2d31292 (patch)
tree28d041597203df828bf1e8f3cab631213006d877
parentpasses "connection test" from official Android app! (diff)
downloadUltramarine-ba1caee706c51e25a6ee325f4c338aa4d2d31292.tar.gz
Ultramarine-ba1caee706c51e25a6ee325f4c338aa4d2d31292.tar.bz2
Ultramarine-ba1caee706c51e25a6ee325f4c338aa4d2d31292.zip
split pieces into modules
-rw-r--r--bin/ultramarine81
-rw-r--r--lib/Ultramarine/Controller.pm634
-rw-r--r--lib/Ultramarine/Model/License.pm611
-rw-r--r--lib/Ultramarine/Server.pm46
4 files changed, 112 insertions, 60 deletions
diff --git a/bin/ultramarine b/bin/ultramarine
index 9df01d3..07cd869 100644
--- a/bin/ultramarine
+++ b/bin/ultramarine
@@ -1,80 +1,41 @@
#!/usr/bin/env perl6
use v6.d.PREVIEW;
-use Cro::HTTP::Router;
-use Cro::HTTP::Server;
+use Ultramarine::Model::License;
use Ultramarine::Model::Users;
use Ultramarine::Middleware::Authentication;
use Ultramarine::Middleware::Authorisation;
use Ultramarine::Middleware::SetContentType;
use Ultramarine::Serialiser::XML;
use Ultramarine::Serialiser::JSON;
-
-sub respond(*@body) {
- response.status = 200;
- response.set-body(@body);
-}
+use Ultramarine::Controller;
+use Ultramarine::Server;
my $users = Ultramarine::Model::Users.new(
- accounts=>{me=>'sesame'},
+ accounts=> { me => 'sesame' },
);
+my $controller = Ultramarine::Controller.new(
+ license => Ultramarine::Model::License.new,
+ authorisation => Ultramarine::Middleware::Authorisation.new,
+).routes;
-my $ultramarine_rest = route {
- # this needs to be here, not in the Server because its
- # short-circuited "unauthorised" response will be emited in an
- # 'after' middleware, but the ResponseSerializerExtension is
- # applied just before those middlewares, so our serialised won't
- # be seen by the "unauthorised" response
- before Ultramarine::Middleware::Authorisation;
- post -> 'ping.view' { respond [] }
- post -> 'getLicense.view' {
- respond [ license => [
- :valid<true>,
- :email<foo@bar.com>,
- :licenseExpires<2019-01-01:00:00:00>,
- ] ],
- }
-};
-
-my $ultramarine = route {
- include rest => $ultramarine_rest;
-}
-
-my class Trace does Cro::HTTP::Middleware::RequestResponse {
- method process-requests(Supply $requests --> Supply) {
- supply whenever $requests -> $req {
- say $req.Str;
- emit $req;
- }
- }
- method process-responses(Supply $responses --> Supply) {
- supply whenever $responses -> $res {
- say $res.Str;
- emit $res;
- }
- }
-}
-
-my Cro::Service $um = Cro::HTTP::Server.new(
+my $server = Ultramarine::Server.new(
:host<192.168.1.145>,
:port<8080>,
- application => $ultramarine,
- before => [
- Trace,
- Ultramarine::Middleware::Authentication.new(:$users),
- ],
- add-body-serializers => [
- Ultramarine::Serialiser::XML,
- Ultramarine::Serialiser::JSON,
- ],
- after => [
- Ultramarine::Middleware::SetContentType,
- ],
+ :$controller,
+ authentication => Ultramarine::Middleware::Authentication.new(:$users),
+ serialisers => [
+ Ultramarine::Serialiser::XML.new,
+ Ultramarine::Serialiser::JSON.new,
+ ],
+ set-content-type => Ultramarine::Middleware::SetContentType.new,
+ do-trace => True,
+).server;
-);
+$server.start;
-$um.start;
+say "ready";
react whenever signal(SIGINT) {
- $um.stop;
+ $server.stop;
exit;
}
diff --git a/lib/Ultramarine/Controller.pm6 b/lib/Ultramarine/Controller.pm6
new file mode 100644
index 0000000..29433fd
--- /dev/null
+++ b/lib/Ultramarine/Controller.pm6
@@ -0,0 +1,34 @@
+use v6.d.PREVIEW;
+use Cro::HTTP::Router;
+
+class Ultramarine::Controller {
+ has $.license is required;
+ has $.authorisation is required;
+
+ sub respond(*@body) {
+ response.status = 200;
+ response.set-body(@body);
+ }
+
+ submethod inner-routes() {
+ return route {
+ # this needs to be here, not in the Server because its
+ # short-circuited "unauthorised" response will be emited
+ # in an 'after' middleware, but the
+ # ResponseSerializerExtension is applied just before those
+ # middlewares, so our serialised won't be seen by the
+ # "unauthorised" response
+ before $!authorisation;
+ post -> 'ping.view' { respond [] }
+ post -> 'getLicense.view' {
+ respond [ license => [ |%($!license.status) ] ],
+ }
+ }
+ }
+
+ method routes() {
+ return route {
+ include rest => self.inner-routes(),
+ }
+ }
+}
diff --git a/lib/Ultramarine/Model/License.pm6 b/lib/Ultramarine/Model/License.pm6
new file mode 100644
index 0000000..2ceeaa5
--- /dev/null
+++ b/lib/Ultramarine/Model/License.pm6
@@ -0,0 +1,11 @@
+use v6.d.PREVIEW;
+
+class Ultramarine::Model::License {
+ method status() {
+ return {
+ :valid<true>,
+ :email<foo@bar.com>,
+ :licenseExpires<2019-01-01:00:00:00>,
+ }
+ }
+}
diff --git a/lib/Ultramarine/Server.pm b/lib/Ultramarine/Server.pm
new file mode 100644
index 0000000..ec7220c
--- /dev/null
+++ b/lib/Ultramarine/Server.pm
@@ -0,0 +1,46 @@
+use v6.d.PREVIEW;
+use Cro::HTTP::Server;
+use Cro::HTTP::Request;
+use Cro::HTTP::Response;
+
+class Ultramarine::Server {
+ has $.authentication is required;
+ has @.serialisers is required;
+ has $.set-content-type is required;
+ has $.controller is required;
+ has $.host = 'localhost';
+ has $.port = 8080;
+ has $.do-trace = False;
+
+ my class Trace does Cro::Transform {
+ has $.type is required;
+
+ method consumes() { $!type }
+ method produces() { $!type }
+
+ method transformer(Supply $input --> Supply) {
+ supply whenever $input -> $thing {
+ say $thing.trace-output,"\n";
+ say (await $thing.body-text).indent(2);
+ emit $thing;
+ }
+ }
+ }
+
+ method server() {
+ return Cro::HTTP::Server.new(
+ :$!host,
+ :$!port,
+ application => $!controller,
+ before => [
+ $.do-trace ?? Trace.new(type=>Cro::HTTP::Request) !! (),
+ $.authentication,
+ ],
+ add-body-serializers => @.serialisers,
+ after => [
+ $.set-content-type,
+ $.do-trace ?? Trace.new(type=>Cro::HTTP::Response) !! (),
+ ],
+ );
+ }
+}