blob: 64220aab4cf3c619d8f81b3022be268f737c5db9 [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';
Akronf65ad6c2017-02-01 14:36:38 +01004use Mojo::File;
Nils Diewalda944fab2015-04-08 21:02:04 +00005use Mojo::JSON 'decode_json';
Akronbe9d5b32017-04-05 20:48:24 +02006use Mojo::Util qw/url_escape/;
7use File::Temp qw/tmpnam/;
Nils Diewald5d1ffb42014-05-21 17:45:34 +00008
Nils Diewald709f52f2015-05-21 18:32:58 +00009# Minor version - may be patched from package.json
Akron0a6768f2016-07-13 18:00:43 +020010our $VERSION = '0.21';
Nils Diewald7cad8402014-07-08 17:06:56 +000011
Nils Diewald7148c6f2015-05-04 15:07:53 +000012# TODO: The FAQ-Page has a contact form for new questions
Nils Diewald709f52f2015-05-21 18:32:58 +000013# TODO: Embed query serialization
14# TODO: Embed collection statistics
15# TODO: Implement tab opener for matches and the tutorial
16# TODO: Make the tutorial ql sensitive
17# TODO: Implement a "projects" system
Nils Diewald7148c6f2015-05-04 15:07:53 +000018
Nils Diewald002e8fb2014-06-22 14:27:01 +000019# Start the application and register all routes and plugins
Nils Diewald5d1ffb42014-05-21 17:45:34 +000020sub startup {
21 my $self = shift;
22
Nils Diewalda944fab2015-04-08 21:02:04 +000023 # Set version based on package file
Nils Diewald709f52f2015-05-21 18:32:58 +000024 # This may introduce a SemVer patch number
Akronf65ad6c2017-02-01 14:36:38 +010025 my $pkg_path = $self->home->child('package.json');
26 if (-e $pkg_path->to_abs) {
27 my $pkg = $pkg_path->slurp;
28 $Kalamar::VERSION = decode_json($pkg)->{version};
29 };
Nils Diewalda944fab2015-04-08 21:02:04 +000030
Akron656c5d92015-11-13 21:17:03 +010031 # Lift maximum template cache
32 $self->renderer->cache->max_keys(200);
33
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000034 # Add additional plugin path
35 push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin');
36
Nils Diewalda79b2682015-05-18 18:34:06 +000037 # korap.ids-mannheim.de specific path prefixing
Nils Diewalda0defc42015-05-07 23:54:17 +000038 $self->hook(
39 before_dispatch => sub {
40 my $c = shift;
41 my $host = $c->req->headers->header('X-Forwarded-Host');
42 if ($host && $host eq 'korap.ids-mannheim.de') {
Akronf65ad6c2017-02-01 14:36:38 +010043 $c->req->url->base->path('/kalamar/');
44 $c->stash(prefix => '/kalamar');
Nils Diewalda0defc42015-05-07 23:54:17 +000045 };
46 }) if $self->mode eq 'production';
47
Nils Diewalda748b0e2015-05-19 22:54:06 +000048
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000049 # Set secrets for signed cookies
Akronf65ad6c2017-02-01 14:36:38 +010050 if (-e (my $secret = $self->home->child('kalamar.secret'))) {
Nils Diewalda79b2682015-05-18 18:34:06 +000051
52 # Load file and split lines for multiple secrets
Akronf65ad6c2017-02-01 14:36:38 +010053 $self->secrets([b($secret->slurp)->split("\n")]);
Nils Diewald4347ee92015-05-04 20:32:48 +000054 }
Nils Diewald709f52f2015-05-21 18:32:58 +000055
56 # File not found ...
57 # Kalamar needs secrets in a file to be easily deployable
58 # and publishable at the same time.
Nils Diewald4347ee92015-05-04 20:32:48 +000059 else {
60 $self->log->warn('Please create a kalamar.secret file');
Nils Diewald19402142015-04-30 15:44:52 +000061 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000062
Akroncba9f322016-02-29 23:12:45 +010063 # Configuration framework
Akron09a567c2016-04-11 22:49:20 +030064 $self->plugin('Config');
Nils Diewald709f52f2015-05-21 18:32:58 +000065
Akronbe9d5b32017-04-05 20:48:24 +020066 # Start fixture server
67 if ($self->mode eq 'test') {
68 $self->plugin(Mount => {
69 '/api/v0.1' => $self->home->child('lib/Kalamar/Apps/test_backend.pl')
70 });
71
72 $self->config('Kalamar')->{api} = "/api/v0.1/";
73 };
74
Akron09a567c2016-04-11 22:49:20 +030075 # Client notifications
Akron0504a182016-04-10 21:13:42 +020076 $self->plugin(Notifications => {
77 'Kalamar::Plugin::Notifications' => 1,
78 JSON => 1
79 });
80
Akron09a567c2016-04-11 22:49:20 +030081 # Localization framework
82 $self->plugin(Localize => {
83 resources => ['kalamar.dict']
84 });
85
86 # Pagination widget
87 $self->plugin('TagHelpers::Pagination' => {
88 prev => '<span><span>&lt;</span></span>',
89 next => '<span><span>&lt;</span></span>',
90 ellipsis => '<a class="ellipsis"><span><span>...</span></span></a>',
91 separator => '',
92 current => '<span>{current}</span>',
93 page => '<span>{page}</span>'
94 });
95
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000096 # Load plugins
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000097 foreach (
Nils Diewaldc46003b2015-05-07 15:55:35 +000098 'Search', # Abstract Search framework
99 'CHI', # Global caching mechanism
Nils Diewaldc46003b2015-05-07 15:55:35 +0000100 'TagHelpers::MailToChiffre', # Obfuscate email addresses
Akrone8235be2016-06-27 11:02:18 +0200101 'KalamarHelpers', # Specific Helpers for Kalamar
102 'KalamarUser' # Specific Helpers for Kalamar
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000103 ) {
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000104 $self->plugin($_);
105 };
106
Nils Diewald709f52f2015-05-21 18:32:58 +0000107
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000108 # Configure mail exception
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000109 $self->plugin('MailException' => $self->config('MailException'));
110
Akron80f731d2017-04-03 20:10:58 +0200111
Nils Diewald709f52f2015-05-21 18:32:58 +0000112
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000113 # Configure documentation navigation
Akronf65ad6c2017-02-01 14:36:38 +0100114 my $navi = Mojo::File->new($self->home->child('templates','doc','navigation.json'))->slurp;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000115 $self->config(navi => decode_json($navi)) if $navi;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000116
Akron3cd391e2017-03-29 23:42:54 +0200117 $self->log->info('API expected at ' . $self->config->{Kalamar}->{api});
Nils Diewald709f52f2015-05-21 18:32:58 +0000118
Akron3cd391e2017-03-29 23:42:54 +0200119 # Establish routes with authentification
120 my $r = $self->routes->under(
121 '/' => sub {
122 my $c = shift;
123
124 if ($c->session('auth')) {
125 $c->stash(auth => $c->session('auth'));
126 };
127 return 1;
128 }
129 );
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000130
Nils Diewalda79b2682015-05-18 18:34:06 +0000131 # Base query route
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000132 $r->get('/')->to('search#query')->name('index');
133
Akron48b1e4d2015-06-17 18:47:01 +0200134 # Collection route
Akrone8235be2016-06-27 11:02:18 +0200135 # TODO: Probably rename to /corpus
Akron48b1e4d2015-06-17 18:47:01 +0200136 $r->get('/collection')->to('Search#collections')->name('collections');
137 $r->get('/collection/:id')->to('Search#collection')->name('collection');
138
Nils Diewalda79b2682015-05-18 18:34:06 +0000139 # Documentation routes
Nils Diewald7148c6f2015-05-04 15:07:53 +0000140 $r->get('/doc')->to('documentation#page', page => 'korap')->name('doc_start');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000141 $r->get('/doc/:page')->to('documentation#page', scope => undef);
142 $r->get('/doc/*scope/:page')->to('documentation#page')->name('doc');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000143
Nils Diewaldc46003b2015-05-07 15:55:35 +0000144 # Contact route
145 $r->get('/contact')->to('documentation#contact');
146 $r->get('/contact')->mail_to_chiffre('documentation#contact');
147
Nils Diewald7148c6f2015-05-04 15:07:53 +0000148 # Match route
Nils Diewald8f4b5da2014-12-03 22:13:39 +0000149 my $corpus = $r->route('/corpus/:corpus_id');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000150 my $doc = $corpus->get('/:doc_id');
151 my $text = $doc->get('/:text_id');
152 my $match = $text->get('/:match_id');
153 $match->to('search#match_info')->name('match');
Akroncddd1642015-06-10 21:31:53 +0200154
Akrone8235be2016-06-27 11:02:18 +0200155 # User Management
Akron3cd391e2017-03-29 23:42:54 +0200156 my $user = $r->any('/user')->to(controller => 'User');
157 $user->post('/login')->to(action => 'login')->name('login');
158# $r->get('/logout')->to(action => 'logout')->name('logout');
159# $r->any('/register')->to(action => 'register')->name('register');
160# $r->any('/forgotten')->to(action => 'pwdforgotten')->name('pwdforgotten');
Akron189b3592016-01-04 20:56:46 +0100161
Akroncddd1642015-06-10 21:31:53 +0200162 # Default user is called 'korap'
163 # $r->route('/user/:user/:collection')
Nils Diewald996aa552014-12-02 03:26:44 +0000164};
165
166
1671;
168
169
170__END__
Nils Diewalda898dac2015-05-06 21:04:16 +0000171
172=pod
173
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000174=encoding utf8
Nils Diewalda898dac2015-05-06 21:04:16 +0000175
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000176=head1 NAME
177
178Kalamar
Nils Diewalda0defc42015-05-07 23:54:17 +0000179
180
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000181=head1 DESCRIPTION
Nils Diewalda0defc42015-05-07 23:54:17 +0000182
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000183L<Kalamar> is a L<Mojolicious|http://mojolicio.us/> based user interface
184frontend for the L<KorAP Corpus Analysis Platform|http://korap.ids-mannheim.de/>.
185
Akron456abd92015-06-02 15:07:21 +0200186B<See the README for further information!>
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000187
Akron456abd92015-06-02 15:07:21 +0200188=head2 COPYRIGHT AND LICENSE
Nils Diewalda748b0e2015-05-19 22:54:06 +0000189
Akronf65ad6c2017-02-01 14:36:38 +0100190Copyright (C) 2015-2017, L<IDS Mannheim|http://www.ids-mannheim.de/>
Nils Diewalda748b0e2015-05-19 22:54:06 +0000191Author: L<Nils Diewald|http://nils-diewald.de/>
192
193Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/>
194Corpus Analysis Platform at the
195L<Institute for the German Language (IDS)|http://ids-mannheim.de/>,
196member of the
197L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/>
198and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project,
199funded by the
200L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>.
201
202Kalamar is free software published under the
Akron456abd92015-06-02 15:07:21 +0200203L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>.
Nils Diewalda748b0e2015-05-19 22:54:06 +0000204
205=cut