blob: 58558ff808aa60b0365830a03b7ad1bd95b9237e [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 Diewalda944fab2015-04-08 21:02:04 +00006our $VERSION;
Nils Diewald7cad8402014-07-08 17:06:56 +00007
Nils Diewald7148c6f2015-05-04 15:07:53 +00008# TODO: The FAQ-Page has a contact form for new questions
9
Nils Diewald002e8fb2014-06-22 14:27:01 +000010# Start the application and register all routes and plugins
Nils Diewald5d1ffb42014-05-21 17:45:34 +000011sub startup {
12 my $self = shift;
13
Nils Diewalda944fab2015-04-08 21:02:04 +000014 # Set version based on package file
15 my $pkg = b($self->home . '/package.json')->slurp;
16 $Kalamar::VERSION = decode_json($pkg)->{version};
17
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000018 # Add additional plugin path
19 push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin');
20
Nils Diewalda79b2682015-05-18 18:34:06 +000021 # korap.ids-mannheim.de specific path prefixing
Nils Diewalda0defc42015-05-07 23:54:17 +000022 $self->hook(
23 before_dispatch => sub {
24 my $c = shift;
25 my $host = $c->req->headers->header('X-Forwarded-Host');
26 if ($host && $host eq 'korap.ids-mannheim.de') {
27 $c->req->url->base->path('/kalamar/');
Nils Diewald705b74a2015-05-07 23:57:34 +000028 $c->stash(prefix => '/kalamar');
Nils Diewalda0defc42015-05-07 23:54:17 +000029 };
30 }) if $self->mode eq 'production';
31
Nils Diewald845282c2015-05-14 07:53:03 +000032 # Cache static assets
Nils Diewalda79b2682015-05-18 18:34:06 +000033 # (not necessary, as long as shipped by nginx or Apache)
Nils Diewald845282c2015-05-14 07:53:03 +000034 $self->hook(
35 after_static => sub {
36 my $res = shift->res;
37 if ($res->code) {
38 $res->headers->cache_control('public, max-age=172800');
39 };
40 });
41
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000042 # Set secrets for signed cookies
Nils Diewald19402142015-04-30 15:44:52 +000043 if (-e (my $secret = $self->home . '/kalamar.secret')) {
Nils Diewalda79b2682015-05-18 18:34:06 +000044
45 # Load file and split lines for multiple secrets
46 $self->secrets([b($secret)->slurp->split("\n")]);
Nils Diewald4347ee92015-05-04 20:32:48 +000047 }
48 else {
49 $self->log->warn('Please create a kalamar.secret file');
Nils Diewald19402142015-04-30 15:44:52 +000050 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000051
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000052 # Load plugins
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000053 foreach (
Nils Diewaldc46003b2015-05-07 15:55:35 +000054 'Config', # Configuration framework
55 'Localize', # Localization framework
56 'Notifications', # Client notifications
57 'Search', # Abstract Search framework
58 'CHI', # Global caching mechanism
Nils Diewalda79b2682015-05-18 18:34:06 +000059 'MailException' # Alert via Email on exception
Nils Diewaldc46003b2015-05-07 15:55:35 +000060 'TagHelpers::Pagination', # Pagination widget
61 'TagHelpers::MailToChiffre', # Obfuscate email addresses
Nils Diewalda79b2682015-05-18 18:34:06 +000062 'KalamarHelpers', # Specific Helpers for Kalamar
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000063 ) {
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000064 $self->plugin($_);
65 };
66
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000067 # Configure mail exception
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000068 $self->plugin('MailException' => $self->config('MailException'));
69
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000070 # Configure documentation navigation
Nils Diewald7148c6f2015-05-04 15:07:53 +000071 my $navi = b($self->home . '/templates/doc/navigation.json')->slurp;
72 $self->config(navi => decode_json($navi)) if $navi;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000073
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000074 # Establish routes
75 my $r = $self->routes;
76
Nils Diewalda79b2682015-05-18 18:34:06 +000077 # Base query route
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000078 $r->get('/')->to('search#query')->name('index');
79
Nils Diewalda79b2682015-05-18 18:34:06 +000080 # Documentation routes
Nils Diewald7148c6f2015-05-04 15:07:53 +000081 $r->get('/doc')->to('documentation#page', page => 'korap')->name('doc_start');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000082 $r->get('/doc/:page')->to('documentation#page', scope => undef);
83 $r->get('/doc/*scope/:page')->to('documentation#page')->name('doc');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000084
Nils Diewaldc46003b2015-05-07 15:55:35 +000085 # Contact route
86 $r->get('/contact')->to('documentation#contact');
87 $r->get('/contact')->mail_to_chiffre('documentation#contact');
88
Nils Diewald7148c6f2015-05-04 15:07:53 +000089 # Match route
Nils Diewald8f4b5da2014-12-03 22:13:39 +000090 my $corpus = $r->route('/corpus/:corpus_id');
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 my $doc = $corpus->get('/:doc_id');
92 my $text = $doc->get('/:text_id');
93 my $match = $text->get('/:match_id');
94 $match->to('search#match_info')->name('match');
Nils Diewald996aa552014-12-02 03:26:44 +000095};
96
97
981;
99
100
101__END__
Nils Diewalda898dac2015-05-06 21:04:16 +0000102
103=pod
104
Nils Diewalda0defc42015-05-07 23:54:17 +0000105The static files are generated using Grunt.
106To get started with Grunt, you need NodeJS > 0.8 ..., you'll need npm. Then you can install and run grunt:
Nils Diewalda898dac2015-05-06 21:04:16 +0000107
108sudo npm install -g grunt-cli
109npm install
110grunt
Nils Diewalda0defc42015-05-07 23:54:17 +0000111
112
113Some perl modules are not on github yet, so you need to install them from github using cpanm:
114
115 cpanm git://github.com/Akron/Mojolicious-Plugin-Localize.git
Nils Diewald652e5f42015-05-10 18:11:45 +0000116
117
118=head2 LICENSE
119
120Highlight.js is released under the BSD License.