Skip to content

Commit

Permalink
static route
Browse files Browse the repository at this point in the history
  • Loading branch information
librasteve committed Aug 12, 2024
1 parent 2efbb0a commit 9abcd1b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 55 deletions.
44 changes: 12 additions & 32 deletions bin/server.raku
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
#!/usr/bin/env raku
use v6.d;

use Cro::HTTP::Router;
use Cro::HTTP::Server;
#use Cro::HTTP::Log;

my $dynamic-routes-supply = Supply.new;

sub create-router {
my @dynamic-routes;
$dynamic-routes-supply.tap(-> $route { @dynamic-routes.push($route) });

route {
get -> 'hello' {
content 'text/plain', 'Hello, World!';
}
for @dynamic-routes -> $route {
get -> $route.path {
content 'text/plain', $route.response;
}
my $application = route {
get -> 'greet', $name {
content 'text/plain', "Hello, $name!";
}
get -> {
static 'index.html';
}
}
}

my $router = create-router();

my $server = Cro::HTTP::Server.new:
:host<localhost>,
:port(10000),
:$router;
my Cro::Service $hello = Cro::HTTP::Server.new:
:host<localhost>, :port<10000>, :$application;

$server.start;
$hello.start;

# Example of adding a dynamic route
$dynamic-routes-supply.emit({ path => 'dynamic', response => 'This is a dynamic route' });
react whenever signal(SIGINT) { $hello.stop; exit; }

react whenever signal(SIGINT) {
$server.stop;
exit;
}
#raku server.raku
#lynx localhost:10000/greet/librasteve
51 changes: 28 additions & 23 deletions stash.rakumod → lib/HTMOO.rakumod
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use v6.d;
unit class HTMOO;

#`[I found this lying around (in Cro?) and think it would be good to repurpose for the HTMX world so that we have examples of OO htmx pages]
#`[
I found this lying around (in Cro?) and think it would be good to repurpose for the HTMX world so that we have examples of OO htmx pages
my $page = Page.new:
Html => Html.new:
Head => Head.new:
Meta => [
Meta.new: m1,
Meta.new: m2
],
Title => Title.new:
This has the superpower of defaults and overrrides
#]

class Meta {
has $.name;
has $.content;
class Meta {
has $.name;
has $.content;

method render {
'<meta name="' ~ $!name ~ '" content="' ~ $!content ~ '">' ~ "\n"
}
}
method render {
'<meta name="' ~ $!name ~ '" content="' ~ $!content ~ '">' ~ "\n"
}
}

class Title {
has $.title;
Expand Down Expand Up @@ -62,10 +57,10 @@ class Head {

method render {
{ .render for @!metas } ~ "\n" ~
$!title.render ~ "\n" ~
{ .render for @!scripts } ~ "\n" ~
{ .render for @!links } ~ "\n" ~
$!style.render ~ "\n"
$!title.render ~ "\n" ~
{ .render for @!scripts } ~ "\n" ~
{ .render for @!links } ~ "\n" ~
$!style.render ~ "\n"
}
}

Expand All @@ -83,7 +78,7 @@ class Html {

method render {
$!head.render ~ "\n" ~
$!body.render ~ "\n"
$!body.render ~ "\n"
}
}

Expand All @@ -93,7 +88,17 @@ class Page {

method render {
"<!doctype $!doctype>\n" ~
$!html.render ~ "\n"
$!html.render ~ "\n"
}
}

# example ... refactor to .new iamerejh
my $page = Page.new:
Html => Html.new:
Head => Head.new:
Meta => [
Meta.new: m1,
Meta.new: m2
],
Title => Title.new:

0 comments on commit 9abcd1b

Please sign in to comment.