blob: 9cea0b13a6ca1e76fe4929710b32ac09097096a3 [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 Diewald709f52f2015-05-21 18:32:58 +00006# Minor version - may be patched from package.json
Akron6ed13992016-05-23 18:06:05 +02007our $VERSION = '0.20';
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
Nils Diewald709f52f2015-05-21 18:32:58 +000010# TODO: Embed query serialization
11# TODO: Embed collection statistics
12# TODO: Implement tab opener for matches and the tutorial
13# TODO: Make the tutorial ql sensitive
14# TODO: Implement a "projects" system
Nils Diewald7148c6f2015-05-04 15:07:53 +000015
Nils Diewald002e8fb2014-06-22 14:27:01 +000016# Start the application and register all routes and plugins
Nils Diewald5d1ffb42014-05-21 17:45:34 +000017sub startup {
18 my $self = shift;
19
Nils Diewalda944fab2015-04-08 21:02:04 +000020 # Set version based on package file
Nils Diewald709f52f2015-05-21 18:32:58 +000021 # This may introduce a SemVer patch number
22 my $pkg = b($self->home . '/package.json')->slurp;
23 $Kalamar::VERSION = decode_json($pkg)->{version};
Nils Diewalda944fab2015-04-08 21:02:04 +000024
Akron656c5d92015-11-13 21:17:03 +010025 # Lift maximum template cache
26 $self->renderer->cache->max_keys(200);
27
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000028 # Add additional plugin path
29 push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin');
30
Nils Diewalda79b2682015-05-18 18:34:06 +000031 # korap.ids-mannheim.de specific path prefixing
Nils Diewalda0defc42015-05-07 23:54:17 +000032 $self->hook(
33 before_dispatch => sub {
34 my $c = shift;
35 my $host = $c->req->headers->header('X-Forwarded-Host');
36 if ($host && $host eq 'korap.ids-mannheim.de') {
37 $c->req->url->base->path('/kalamar/');
Nils Diewald705b74a2015-05-07 23:57:34 +000038 $c->stash(prefix => '/kalamar');
Nils Diewalda0defc42015-05-07 23:54:17 +000039 };
40 }) if $self->mode eq 'production';
41
Nils Diewalda748b0e2015-05-19 22:54:06 +000042
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000043 # Set secrets for signed cookies
Nils Diewald19402142015-04-30 15:44:52 +000044 if (-e (my $secret = $self->home . '/kalamar.secret')) {
Nils Diewalda79b2682015-05-18 18:34:06 +000045
46 # Load file and split lines for multiple secrets
47 $self->secrets([b($secret)->slurp->split("\n")]);
Nils Diewald4347ee92015-05-04 20:32:48 +000048 }
Nils Diewald709f52f2015-05-21 18:32:58 +000049
50 # File not found ...
51 # Kalamar needs secrets in a file to be easily deployable
52 # and publishable at the same time.
Nils Diewald4347ee92015-05-04 20:32:48 +000053 else {
54 $self->log->warn('Please create a kalamar.secret file');
Nils Diewald19402142015-04-30 15:44:52 +000055 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000056
Akroncba9f322016-02-29 23:12:45 +010057 # Configuration framework
Akron09a567c2016-04-11 22:49:20 +030058 $self->plugin('Config');
Nils Diewald709f52f2015-05-21 18:32:58 +000059
Akron09a567c2016-04-11 22:49:20 +030060 # Client notifications
Akron0504a182016-04-10 21:13:42 +020061 $self->plugin(Notifications => {
62 'Kalamar::Plugin::Notifications' => 1,
63 JSON => 1
64 });
65
Akron09a567c2016-04-11 22:49:20 +030066 # Localization framework
67 $self->plugin(Localize => {
68 resources => ['kalamar.dict']
69 });
70
71 # Pagination widget
72 $self->plugin('TagHelpers::Pagination' => {
73 prev => '<span><span>&lt;</span></span>',
74 next => '<span><span>&lt;</span></span>',
75 ellipsis => '<a class="ellipsis"><span><span>...</span></span></a>',
76 separator => '',
77 current => '<span>{current}</span>',
78 page => '<span>{page}</span>'
79 });
80
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000081 # Load plugins
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000082 foreach (
Nils Diewaldc46003b2015-05-07 15:55:35 +000083 'Search', # Abstract Search framework
84 'CHI', # Global caching mechanism
Nils Diewaldc46003b2015-05-07 15:55:35 +000085 'TagHelpers::MailToChiffre', # Obfuscate email addresses
Akron778f96c2016-06-27 11:02:18 +020086 'KalamarHelpers', # Specific Helpers for Kalamar
87 'KalamarUser' # Specific Helpers for Kalamar
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000088 ) {
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000089 $self->plugin($_);
90 };
91
Nils Diewald709f52f2015-05-21 18:32:58 +000092
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000093 # Configure mail exception
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000094 $self->plugin('MailException' => $self->config('MailException'));
95
Nils Diewald709f52f2015-05-21 18:32:58 +000096
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000097 # Configure documentation navigation
Nils Diewald7148c6f2015-05-04 15:07:53 +000098 my $navi = b($self->home . '/templates/doc/navigation.json')->slurp;
99 $self->config(navi => decode_json($navi)) if $navi;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000100
Nils Diewald709f52f2015-05-21 18:32:58 +0000101
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000102 # Establish routes
103 my $r = $self->routes;
104
Nils Diewalda79b2682015-05-18 18:34:06 +0000105 # Base query route
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000106 $r->get('/')->to('search#query')->name('index');
107
Akron48b1e4d2015-06-17 18:47:01 +0200108 # Collection route
Akron778f96c2016-06-27 11:02:18 +0200109 # TODO: Probably rename to /corpus
Akron48b1e4d2015-06-17 18:47:01 +0200110 $r->get('/collection')->to('Search#collections')->name('collections');
111 $r->get('/collection/:id')->to('Search#collection')->name('collection');
112
Nils Diewalda79b2682015-05-18 18:34:06 +0000113 # Documentation routes
Nils Diewald7148c6f2015-05-04 15:07:53 +0000114 $r->get('/doc')->to('documentation#page', page => 'korap')->name('doc_start');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000115 $r->get('/doc/:page')->to('documentation#page', scope => undef);
116 $r->get('/doc/*scope/:page')->to('documentation#page')->name('doc');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000117
Nils Diewaldc46003b2015-05-07 15:55:35 +0000118 # Contact route
119 $r->get('/contact')->to('documentation#contact');
120 $r->get('/contact')->mail_to_chiffre('documentation#contact');
121
Nils Diewald7148c6f2015-05-04 15:07:53 +0000122 # Match route
Nils Diewald8f4b5da2014-12-03 22:13:39 +0000123 my $corpus = $r->route('/corpus/:corpus_id');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000124 my $doc = $corpus->get('/:doc_id');
125 my $text = $doc->get('/:text_id');
126 my $match = $text->get('/:match_id');
127 $match->to('search#match_info')->name('match');
Akroncddd1642015-06-10 21:31:53 +0200128
Akron778f96c2016-06-27 11:02:18 +0200129 # User Management
130 $r->any('/user')->to(controller => 'User');
131 $r->post('/login')->to(action => 'login')->name('login');
132 $r->get('/logout')->to(action => 'logout')->name('logout');
133 $r->any('/register')->to(action => 'register')->name('register');
134 $r->any('/forgotten')->to(action => 'pwdforgotten')->name('pwdforgotten');
Akron07c720e2016-01-04 20:56:46 +0100135
Akroncddd1642015-06-10 21:31:53 +0200136 # Default user is called 'korap'
137 # $r->route('/user/:user/:collection')
Nils Diewald996aa552014-12-02 03:26:44 +0000138};
139
140
1411;
142
143
144__END__
Nils Diewalda898dac2015-05-06 21:04:16 +0000145
146=pod
147
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000148=encoding utf8
Nils Diewalda898dac2015-05-06 21:04:16 +0000149
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000150=head1 NAME
151
152Kalamar
Nils Diewalda0defc42015-05-07 23:54:17 +0000153
154
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000155=head1 DESCRIPTION
Nils Diewalda0defc42015-05-07 23:54:17 +0000156
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000157L<Kalamar> is a L<Mojolicious|http://mojolicio.us/> based user interface
158frontend for the L<KorAP Corpus Analysis Platform|http://korap.ids-mannheim.de/>.
159
Akron456abd92015-06-02 15:07:21 +0200160B<See the README for further information!>
Nils Diewaldeb5f3072015-05-20 09:32:42 +0000161
Akron456abd92015-06-02 15:07:21 +0200162=head2 COPYRIGHT AND LICENSE
Nils Diewalda748b0e2015-05-19 22:54:06 +0000163
Akron6ed13992016-05-23 18:06:05 +0200164Copyright (C) 2015-2016, L<IDS Mannheim|http://www.ids-mannheim.de/>
Nils Diewalda748b0e2015-05-19 22:54:06 +0000165Author: L<Nils Diewald|http://nils-diewald.de/>
166
167Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/>
168Corpus Analysis Platform at the
169L<Institute for the German Language (IDS)|http://ids-mannheim.de/>,
170member of the
171L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/>
172and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project,
173funded by the
174L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>.
175
176Kalamar is free software published under the
Akron456abd92015-06-02 15:07:21 +0200177L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>.
Nils Diewalda748b0e2015-05-19 22:54:06 +0000178
179=cut