blob: 31cd35e4d566d651565dd3e21a90709b9607d043 [file] [log] [blame]
Nils Diewald2fe12e12015-03-06 16:47:06 +00001package Kalamar;
Nils Diewald5d1ffb42014-05-21 17:45:34 +00002use Mojo::Base 'Mojolicious';
Nils Diewalde2c83812014-11-11 21:13:18 +00003use Mojo::ByteStream 'b';
Nils Diewalda944fab2015-04-08 21:02:04 +00004use Mojo::JSON 'decode_json';
Nils Diewald5d1ffb42014-05-21 17:45:34 +00005
Nils Diewalda748b0e2015-05-19 22:54:06 +00006# Sync with package.json
7our $VERSION = '0.15.0';
Nils Diewald7cad8402014-07-08 17:06:56 +00008
Nils Diewald7148c6f2015-05-04 15:07:53 +00009# TODO: The FAQ-Page has a contact form for new questions
10
Nils Diewald002e8fb2014-06-22 14:27:01 +000011# Start the application and register all routes and plugins
Nils Diewald5d1ffb42014-05-21 17:45:34 +000012sub startup {
13 my $self = shift;
14
Nils Diewalda944fab2015-04-08 21:02:04 +000015 # Set version based on package file
Nils Diewalda748b0e2015-05-19 22:54:06 +000016 # my $pkg = b($self->home . '/package.json')->slurp;
17 # $Kalamar::VERSION = decode_json($pkg)->{version};
Nils Diewalda944fab2015-04-08 21:02:04 +000018
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000019 # Add additional plugin path
20 push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin');
21
Nils Diewalda79b2682015-05-18 18:34:06 +000022 # korap.ids-mannheim.de specific path prefixing
Nils Diewalda0defc42015-05-07 23:54:17 +000023 $self->hook(
24 before_dispatch => sub {
25 my $c = shift;
26 my $host = $c->req->headers->header('X-Forwarded-Host');
27 if ($host && $host eq 'korap.ids-mannheim.de') {
28 $c->req->url->base->path('/kalamar/');
Nils Diewald705b74a2015-05-07 23:57:34 +000029 $c->stash(prefix => '/kalamar');
Nils Diewalda0defc42015-05-07 23:54:17 +000030 };
31 }) if $self->mode eq 'production';
32
Nils Diewalda748b0e2015-05-19 22:54:06 +000033
Nils Diewald845282c2015-05-14 07:53:03 +000034 # Cache static assets
Nils Diewalda79b2682015-05-18 18:34:06 +000035 # (not necessary, as long as shipped by nginx or Apache)
Nils Diewald845282c2015-05-14 07:53:03 +000036 $self->hook(
37 after_static => sub {
38 my $res = shift->res;
39 if ($res->code) {
40 $res->headers->cache_control('public, max-age=172800');
41 };
42 });
43
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000044 # Set secrets for signed cookies
Nils Diewald19402142015-04-30 15:44:52 +000045 if (-e (my $secret = $self->home . '/kalamar.secret')) {
Nils Diewalda79b2682015-05-18 18:34:06 +000046
47 # Load file and split lines for multiple secrets
48 $self->secrets([b($secret)->slurp->split("\n")]);
Nils Diewald4347ee92015-05-04 20:32:48 +000049 }
50 else {
51 $self->log->warn('Please create a kalamar.secret file');
Nils Diewald19402142015-04-30 15:44:52 +000052 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000053
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000054 # Load plugins
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000055 foreach (
Nils Diewaldc46003b2015-05-07 15:55:35 +000056 'Config', # Configuration framework
57 'Localize', # Localization framework
58 'Notifications', # Client notifications
59 'Search', # Abstract Search framework
60 'CHI', # Global caching mechanism
Nils Diewalda748b0e2015-05-19 22:54:06 +000061 'MailException', # Alert via Email on exception
Nils Diewaldc46003b2015-05-07 15:55:35 +000062 'TagHelpers::Pagination', # Pagination widget
63 'TagHelpers::MailToChiffre', # Obfuscate email addresses
Nils Diewalda79b2682015-05-18 18:34:06 +000064 'KalamarHelpers', # Specific Helpers for Kalamar
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000065 ) {
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000066 $self->plugin($_);
67 };
68
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000069 # Configure mail exception
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000070 $self->plugin('MailException' => $self->config('MailException'));
71
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000072 # Configure documentation navigation
Nils Diewald7148c6f2015-05-04 15:07:53 +000073 my $navi = b($self->home . '/templates/doc/navigation.json')->slurp;
74 $self->config(navi => decode_json($navi)) if $navi;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000075
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000076 # Establish routes
77 my $r = $self->routes;
78
Nils Diewalda79b2682015-05-18 18:34:06 +000079 # Base query route
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000080 $r->get('/')->to('search#query')->name('index');
81
Nils Diewalda79b2682015-05-18 18:34:06 +000082 # Documentation routes
Nils Diewald7148c6f2015-05-04 15:07:53 +000083 $r->get('/doc')->to('documentation#page', page => 'korap')->name('doc_start');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000084 $r->get('/doc/:page')->to('documentation#page', scope => undef);
85 $r->get('/doc/*scope/:page')->to('documentation#page')->name('doc');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000086
Nils Diewaldc46003b2015-05-07 15:55:35 +000087 # Contact route
88 $r->get('/contact')->to('documentation#contact');
89 $r->get('/contact')->mail_to_chiffre('documentation#contact');
90
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 # Match route
Nils Diewald8f4b5da2014-12-03 22:13:39 +000092 my $corpus = $r->route('/corpus/:corpus_id');
Nils Diewald7148c6f2015-05-04 15:07:53 +000093 my $doc = $corpus->get('/:doc_id');
94 my $text = $doc->get('/:text_id');
95 my $match = $text->get('/:match_id');
96 $match->to('search#match_info')->name('match');
Nils Diewald996aa552014-12-02 03:26:44 +000097};
98
99
1001;
101
102
103__END__
Nils Diewalda898dac2015-05-06 21:04:16 +0000104
105=pod
106
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000107=encoding utf8
Nils Diewalda898dac2015-05-06 21:04:16 +0000108
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000109=head1 NAME
110
111Kalamar
Nils Diewalda0defc42015-05-07 23:54:17 +0000112
113
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000114=head1 DESCRIPTION
Nils Diewalda0defc42015-05-07 23:54:17 +0000115
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000116L<Kalamar> is a L<Mojolicious|http://mojolicio.us/> based user interface
117frontend for the L<KorAP Corpus Analysis Platform|http://korap.ids-mannheim.de/>.
118
119=head1 INSTALLATION
120
121=head2 Generate Static Asset Files
122
123To generate the static asset files (scripts, styles, images ...),
124you need NodeJS E<gt> 0.8 and Grunt. To install Grunt, just run
125
126 $ sudo npm install -g grunt-cli
127 $ npm install
128 $ grunt
Nils Diewald652e5f42015-05-10 18:11:45 +0000129
130
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000131=head2 Start Server
Nils Diewald652e5f42015-05-10 18:11:45 +0000132
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000133Kalamar uses the L<Mojolicious|http://mojolicio.us/> framework,
134that expects a Perl version of at least 5.10.1.
135The recommended environment is based on L<Perlbrew|http://perlbrew.pl/>
136with L<App::cpanminus>.
137
138Some perl modules are not on CPAN yet,
139so you need to install them from GitHub.
140The easiest way to do this is running
141
142 $ cpanm git://github.com/Akron/Mojolicious-Plugin-Search.git
143 $ cpanm git://github.com/Akron/Mojolicious-Plugin-Localize.git
144 $ cpanm --installdeps .
145 $ perl Makefile.PL
146 $ make test
147
148Kalamar can be deployed like all
149L<Mojolicious apps|http://mojolicio.us/perldoc/Mojolicious/Guides/Cookbook#DEPLOYMENT>.
150The easiest way is to start the built-in server:
151
152 $ perl script/kalamar deamon
Nils Diewalda748b0e2015-05-19 22:54:06 +0000153
154
155=head1 COPYRIGHT AND LICENSE
156
157=head2 Bundled Software
158
159C<ALERTIFY.js> is released under the terms of the MIT License.
160C<Almond> is released under the terms of the BSD License.
161C<dagre> is released under the terms of the MIT License.
162C<Highlight.js> is released under the terms of the BSD License.
163C<Jasmine> is released under the terms of the MIT License.
164C<RequireJS> is released under the terms of the BSD License.
165
166
167=head2 Original Software
168
169Copyright (C) 2015, L<IDS Mannheim|http://www.ids-mannheim.de/>
170Author: L<Nils Diewald|http://nils-diewald.de/>
171
172Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/>
173Corpus Analysis Platform at the
174L<Institute for the German Language (IDS)|http://ids-mannheim.de/>,
175member of the
176L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/>
177and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project,
178funded by the
179L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>.
180
181Kalamar is free software published under the
182L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE).
183
184=cut