-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtapp.pl
executable file
·147 lines (126 loc) · 4.07 KB
/
dtapp.pl
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env perl
use 5.014;
use strict;
use warnings;
use Carp;
use Browser::Open qw( open_browser );
#
# This effort is based on the following talk by
# Build a desktop application with Perl, HTTP::Engine, SQLite and jQuery
# By Tatsuhiko Miyagawa (miyagawa):
#
# http://www.yapcna.org/yn2009/talk/2018
#
# The database example comes from Joel Berger
#
# http://blogs.perl.org/users/joel_berger/2012/10/a-simple-mojoliciousdbi-example.html
#
my $port = Mojo::IOLoop::Server->generate_port;
my $url = "http://127.0.0.1:$port";
my $me = $$;
my $pid = fork;
if ( !defined $pid ) {
croak "Cannot fork: $!";
}
elsif ( $pid == 0 ) {
# child process
sleep 1; # Just because...
open_browser($url);
say "Here we go...";
}
else {
use Mojolicious::Lite;
# connect to database
use DBI;
my $dbh = DBI->connect( "dbi:SQLite:database.db", "", "" )
or croak "Could not connect: $!";
# shortcut for use in template
helper db => sub { $dbh };
plugin 'Config';
my $name = app->config('name');
get '/' => sub {
my $self = shift;
$self->render('index');
};
get '/kill' => sub {
my $self = shift;
$self->res->code(301);
$self->redirect_to('http://www.google.com');
$self->app->log->debug("Goodbye, $name.");
# I need this function to return so I delay the kill a little.
#
# system("(sleep 1; kill $me)&");
#
# Another way proposed by Joel Berger:
# http://stackoverflow.com/questions/22871601/how-do-i-properly-shut-down-a-mojoliciouslite-server
#
$self->tx->on( finish => sub {exit} );
};
my $insert;
while (1) { # Repeat forever until disk space is available.
# create insert statement
my $insert = eval { $dbh->prepare('INSERT INTO people VALUES (?,?)') };
# break out of loop if statement prepared
last if $insert;
# if statement didn't prepare, assume its because the table doesn't exist
warn "Creating table 'people'\n";
$dbh->do('CREATE TABLE people (name varchar(255), age int);');
}
# setup route which receives data and returns to /
post '/insert' => sub {
my $self = shift;
my $user = $self->param('name');
my $age = $self->param('age');
$insert->execute( $user, $age );
$self->redirect_to('/');
};
app->secrets(["Don't tell anyone"]);
app->start( 'daemon', '-l', $url );
}
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
% my $sth = db->prepare('SELECT * FROM people');
% $sth->execute;
<form action="<%=url_for('insert')->to_abs%>" method="post">
Name: <input type="text" name="name"> Age: <input type="text" name="age"> <input type="submit" value="Add">
</form>
<br>
Data: <br>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
% while (my $row = $sth->fetchrow_arrayref) {
<tr>
% for my $text (@$row) {
<td><%= $text %></td>
% }
</tr>
% }
</table>
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head>
<title><%= config 'name' %></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>
<body>
<h1><%= config 'name' %></h1>
<div class="well">This is a <%= config 'name' %> application.</div>
<%= content %>
<div><a class="btn btn-sm btn-danger" href="/kill">Quit</a></div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>