-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusermesh.pl
194 lines (115 loc) · 3.66 KB
/
usermesh.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env perl
use Mojolicious::Lite;
use Digest::MD5 qw(md5_hex);
use File::Basename 'dirname';
use lib dirname( __FILE__ ) . "/lib";
# SET THIS
app->secret('YOUR SECRET HERE');
# SET THIS
# CHANGE THE PORT ON THE NEXT LINE IF YOU DON'T WANT TO RUN ON PORT 80
app->config(hypnotoad => { listen => ['http://*:80'] });
app->types->type(rss => 'application/xhtml+xml');
use Usermesh();
my $um = Usermesh->new(app);
helper um => sub {
my $self = shift;
$um->setrequest($self);
return $um;
};
helper config => sub {
my $self = shift;
return $um->{CONFIG};
};
# Load all the plugins
opendir(PLUGINDIR, dirname(__FILE__) . "/lib/Usermesh/Plugins");
while(my $file = readdir(PLUGINDIR)) {
next if ($file !~ /\.pm$/);
$file =~ s/\.pm$//;
plugin "Usermesh::Plugins::$file";
}
closedir(PLUGINDIR);
get "/" => sub {
my $self = shift;
return $self->redirect_to('/index.html');
};
group {
under '/admin' => sub {
my $self = shift;
if ($self->session->{auth} eq $self->config->{adminpassword}) {
return 1;
}
$self->redirect_to("/login");
return undef;
};
get "/" => sub {
my $self = shift;
my ($output);
$output .= $self->um->html->hparagraphs("Hello!");
$output .= $self->um->html->h3paragraphs("Current Drafts");
$output .= $self->draftposts();
$output .= $self->um->html->h3paragraphs("Recent Posts");
$output .= $self->recentposts();
return $self->render('admin/surround', replace => { title => "Home", content => $output });
};
get "/generator" => sub {
my $self = shift;
my ($output);
my $done = $self->param("done");
if ($done == 1) {
$output .= $self->um->getblock("form/success", { text => "All changes have been regenerated as static pages." });
} elsif ($done == 2) {
$output .= $self->um->getblock("form/success", { text => "All the static pages of your site have been regenerated." });
}
$output .= $self->um->html->hparagraphs("Static Generator", "You can generate the static version of your site from here, just by clicking one of the buttons below.");
$output .= $self->um->html->buttons({ "Generate Changes" => "/admin/generator/changes/" }, { "Generate All Pages" => "/admin/generator/all/" });
return $self->render('admin/surround', replace => { title => "Static Generator", content => $output });
};
get "/generator/changes" => sub {
my $self = shift;
$self->um->generator->run();
$self->redirect_to("/admin/generator?done=1");
};
get "/generator/all" => sub {
my $self = shift;
$self->um->generator->run("1");
$self->redirect_to("/admin/generator?done=2");
};
};
get "/login" => sub {
my $self = shift;
my ($error);
my $e = $self->param("e");
if ($e eq "1") {
$error = qq|<div class="alert alert-error">Invalid password</div>|;
}
return $self->render('admin/login', replace => { title => "Login", error => $error });
};
post "/login/auth" => sub {
my $self = shift;
my $password = $self->param("password");
my $hashedpassword = md5_hex(md5_hex($password));
if ($hashedpassword eq $self->config->{adminpassword}) {
$self->session->{auth} = $hashedpassword;
$self->redirect_to("/admin/");
} else {
$self->redirect_to("/login?e=1");
}
};
get "/logout" => sub {
my $self = shift;
$self->session->{auth} = undef;
return $self->redirect_to('/');
};
get "/rss" => sub {
my $self = shift;
$self->render_static('rss/index.rss');
};
get "*page" => sub {
my $self = shift;
if ($self->param("page") !~ m!/index.html!) {
return $self->redirect_to($self->param("page") . "/index.html");
} else {
return return $self->render_not_found;
}
};
app->start;