Skip to content

Commit

Permalink
Implement rudimentary website
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed Dec 26, 2017
1 parent 30a82a0 commit 4354ecb
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .cro.yml
@@ -0,0 +1,6 @@
---
name: hello
cro: 1
id: hello
entrypoint: bin/server.p6
...
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
/lib/.precomp
/alerts.sqlite.db
1 change: 1 addition & 0 deletions META6.json
Expand Up @@ -6,6 +6,7 @@
"description" : "Code for alerts.perl6.org and supporting IRC bot",
"tags" : [ "core dev" ],
"depends" : [
"Cro",
"DBIish",
"Subset::Helper"
],
Expand Down
71 changes: 71 additions & 0 deletions bin/server.p6
@@ -0,0 +1,71 @@
use lib <lib>;
use Cro::HTTP::Router;
use Cro::HTTP::Server;
use P6lert::Model::Alerts;

my $Alerts := P6lert::Model::Alerts.new;
# my $CSS := $*PROGRAM.sibling('../assets/main.css').absolute;

sub MAIN (Str:D :$host = 'localhost', UInt:D :$port = 10000) {
my $application = route {
get -> {
content 'text/html', html-render-alerts $Alerts.all
}
get -> 'alert', Int $id {
content 'text/html', html-render-alerts $Alerts.get: $id
}
get -> 'main.css' { static 'static/main.css' }#$CSS }
}

with Cro::HTTP::Server.new: :$host, :$port, :$application {
$^server.start;
say "Started server http://$host:$port";
react whenever signal SIGINT {
$server.stop;
exit;
}
}
}

sub html-render-alerts(*@alerts) {
html-layout-default '<ul id="alerts">'
~ @alerts.map(-> $a {
q:to/✎✎✎✎✎/;
<li class="alert alert-\qq[$a.severity()]">
<p class="info"><a href="/alert/\qq[$a.id()]">#\qq[$a.id()]</a>
| \qq[$a.time-human()]
| posted by \qq[$a.creator()]
| severity: \qq[$a.severity()]
\qq[{"| affects: $a.affects()" if $a.affects}]
<p>\qq[$a.alert()]</p>
</li>
✎✎✎✎✎
}) ~ '</ul>'
}

sub html-layout-default (Str:D $content) {
q:to/✎✎✎✎✎/;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Perl 6 Alerts</title>
<link rel="stylesheet" href="/main.css">
</head>
<body>
<h1>Perl 6 Alerts</h1>
<div id="content">
\qq[$content]
</div>
<footer>
<small>Code for this website is available at
<a href="https://github.com/perl6/alerts">github.com/perl6/alerts</a>
</footer>
</body>
</html>
✎✎✎✎✎
}
4 changes: 4 additions & 0 deletions lib/P6lert/Alert.pm6
Expand Up @@ -10,3 +10,7 @@ has UInt:D $.time is required;
has Str:D $.creator is required;
has Str:D $.affects is required;
has Severity:D $.severity is required;

method time-human {
DateTime.new: $!time
}
9 changes: 9 additions & 0 deletions lib/P6lert/Model/Alerts.pm6
Expand Up @@ -50,6 +50,15 @@ method all {
}
}

method get (UInt:D $id) {
given $!dbh.prepare: SELECT * FROM alerts where id = ? {
LEAVE .finish;
.execute: $id;
if .row: :hash { P6lert::Alert.new: |$^alert }
else { Empty }
}
}

method delete (UInt:D $id) {
given $!dbh.prepare: DELETE FROM alerts WHERE id = ? {
.execute: $id;
Expand Down
32 changes: 32 additions & 0 deletions static/main.css
@@ -0,0 +1,32 @@

body {
margin: 10px auto;
max-width: 1100px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

#content {
padding: 10px 20px;
margin-bottom: 20px;
border-radius: 4px;
border: 1px solid #eee;
}

code, kbd, pre, samp {
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
}

h1 {
font-size: 120%;
font-weight: normal;
}

footer, footer * {
color: #999;
}

#alerts {
list-style-type: none;
padding-left: 0;
margin-left: 0;
}

0 comments on commit 4354ecb

Please sign in to comment.