-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCT.pm
233 lines (160 loc) · 6.12 KB
/
MCT.pm
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package MCT;
use Mojo::Base 'Mojolicious';
our $VERSION = '0.01';
use Mojo::Pg;
use MCT::Model;
use MCT::Model::Countries;
has pg => sub { Mojo::Pg->new(shift->config->{db}) };
sub migrations {
my $app = shift;
$app->pg->migrations->from_file($app->home->rel_file('mct.sql'));
}
sub startup {
my $app = shift;
$app->plugin('Config' => file => $ENV{MOJO_CONFIG} || $app->home->rel_file('mct.conf'));
$app->sessions->default_expiration(3600 * 24);
#$app->_setup_database;
$app->_setup_secrets;
$app->_setup_stripe;
$app->_setup_validation;
$app->helper('model.db' => sub { $_[0]->stash->{'mct.db'} ||= $_[0]->app->pg->db });
$app->helper('model.conference' => sub { MCT::Model->new_object(Conference => db => shift->model->db, @_) });
$app->helper('model.identity' => sub { MCT::Model->new_object(Identity => db => shift->model->db, @_) });
$app->helper('model.presentation' => sub { MCT::Model->new_object(Presentation => db => shift->model->db, @_) });
$app->helper('model.user' => sub { MCT::Model->new_object(User => db => shift->model->db, @_) });
$app->helper('form_row' => \&_form_row);
$app->plugin('MCT::Plugin::Auth');
$app->plugin('AssetPack');
$app->_assets;
$app->_routes;
$app->migrations->migrate unless $ENV{MCT_SKIP_MIGRATION};
}
sub _assets {
my $app = shift;
$app->asset->preprocessors->add(
css => sub {
my ($assetpack, $text, $file) = @_;
if ($file =~ /font.*awesome/) {
$$text =~ s!url\(["']../([^']+)["']\)!url(https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/$1)!g;
}
},
);
$app->asset('mojoconf.js' => (
'http://code.jquery.com/jquery-1.11.2.min.js',
'https://raw.githubusercontent.com/stripe/jquery.payment/master/lib/jquery.payment.js',
'/js/main.js',
'/js/stripe.js',
));
$app->asset('mojoconf.css' => (
'http://cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/pure-min.css',
'https://fonts.googleapis.com/css?family=Oswald:400,300,700',
'https://fonts.googleapis.com/css?family=PT+Sans+Narrow',
'https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css',
'/sass/mct.scss',
));
}
sub _form_row {
my ($c, $name, $model, $label, $field) = @_;
return $c->tag(div => class => 'form-row', sub {
return join('',
$c->tag(label => for => "form_$name", sub { $label }),
$field || $c->text_field($name, $model ? $model->$name : '', id => "form_$name"),
);
});
}
sub _routes {
my $app = shift;
# back compat
$app->plugin('MCT::Plugin::ACT' => {alias => ['/mojo2014'], path => '/2014', url => 'http://act.yapc.eu/mojo2014/'});
$app->routes->any('/*whatever' => {whatever => ''})->to(template => 'cancelled');
}
sub _setup_database {
my $app = shift;
my $migrations;
unless ($app->config->{db} ||= $ENV{MCT_DATABASE_DSN}) {
my $db = sprintf 'postgresql://%s@/mct_%s', (getpwuid $<)[0] || 'postgresql', $app->mode;
$app->config->{db} = $db;
$app->log->warn("Using default database '$db'. (Neither MCT_DATABASE_DSN or config file was set up)");
}
}
sub _setup_secrets {
my $app = shift;
my $secrets = $app->config('secrets') || [];
unless (@$secrets) {
my $unsafe = join ':', $app->config('db'), $<, $(, $^X, $^O, $app->home;
$app->log->warn('Using default (unsafe) session secrets. (Config file was not set up)');
$secrets = [Mojo::Util::md5_sum($unsafe)];
}
$app->secrets($secrets);
}
sub _setup_stripe {
my $app = shift;
my $stripe = $app->config('stripe');
$stripe->{auto_capture} = 0;
$stripe->{mocked} //= $ENV{MCT_MOCK};
$app->plugin('StripePayment' => $stripe);
}
sub _setup_validation {
my $app = shift;
$app->validator->add_check(country => sub {
my ($validation, $name, $value) = @_;
MCT::Model::Countries->name_from_code($value) ? undef : ['country'];
});
}
1;
=head1 NAME
MCT - Mojo conference toolkit
=head1 DESCRIPTION
L<MCT> is a L<Mojolicious> based web application for running conferences.
THIS PROJECT IS UNDER HEAVY DEVELOPMENT. ANY CHANGES CAN HAPPEN.
=head2 Features
The features below are ideas for what might be implemented:
=over 4
=item * Single sign on with OAuth2
We do not want to store user passwords, so instead we should use
L<Mojolicious::Plugin::OAuth2> to connect existing systems to L<MCT>.
=item * Talk admin
All conference users can post talks. Conference admins can move talks
into a schedule by accepting or rejecting talks.
=item * News
A very simple L<Text::Markdown> based editor for news items and generation of an
L<atom news feed|XML::Atom::Feed>.
=item * User management
Simple user groups need to be implemented: Admin and regular users.
=item * Wiki
A very simple L<Text::Markdown> based editor for wiki pages.
=back
=head1 REQUIREMENTS
=over 4
=item * A PostgreSQL database.
Conference data, user information and talks are stored in the local database.
=back
=head1 SYNOPSIS
=head2 Create databases
Below is how you would create the databases in Ubuntu/Debian:
$ sudo -u postgres createuser $USER
$ sudo -u postgres createdb -O $USER mct_development
$ sudo -u postgres createdb -O $USER mct_production
$ sudo -u postgres createdb -O $USER mct_test
Below is a nifty command that is usefule while developing. This command will
drop all the tables in a database:
$ psql -c 'drop schema public cascade;create schema public' mct_test
=head2 Production
To use the default settings, simply start the application with
L<hypnotoad|Mojo::Server::Hypnotoad>:
$ hypnotoad /path/to/mct/script/mct;
=head2 Development
To start developing, it's suggested to start mct with these options:
# MCT_MOCK: Mock interaction with GitHub and other third parties
# -w: Watch the directories for changes in case AssetPack need
# to rebuild the static files.
$ MCT_MOCK=1 morbo script/mct -w public/sass -w public/js -w lib
=head1 AUTHOR
Glen Hinkle E<lt>tempire@cpan.org<gt>
Jan Henning Thorsen, E<lt>jhthorsen@cpan.orgE<gt>
Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2012-2014 by Joel Berger
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut