Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 1 | package Kalamar; |
Nils Diewald | 5d1ffb4 | 2014-05-21 17:45:34 +0000 | [diff] [blame] | 2 | use Mojo::Base 'Mojolicious'; |
Nils Diewald | e2c8381 | 2014-11-11 21:13:18 +0000 | [diff] [blame] | 3 | use Mojo::ByteStream 'b'; |
Nils Diewald | a944fab | 2015-04-08 21:02:04 +0000 | [diff] [blame] | 4 | use Mojo::JSON 'decode_json'; |
Nils Diewald | 5d1ffb4 | 2014-05-21 17:45:34 +0000 | [diff] [blame] | 5 | |
Nils Diewald | a944fab | 2015-04-08 21:02:04 +0000 | [diff] [blame] | 6 | our $VERSION; |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 7 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 8 | # TODO: The FAQ-Page has a contact form for new questions |
| 9 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 10 | # Start the application and register all routes and plugins |
Nils Diewald | 5d1ffb4 | 2014-05-21 17:45:34 +0000 | [diff] [blame] | 11 | sub startup { |
| 12 | my $self = shift; |
| 13 | |
Nils Diewald | a944fab | 2015-04-08 21:02:04 +0000 | [diff] [blame] | 14 | # Set version based on package file |
| 15 | my $pkg = b($self->home . '/package.json')->slurp; |
| 16 | $Kalamar::VERSION = decode_json($pkg)->{version}; |
| 17 | |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 18 | # Add additional plugin path |
| 19 | push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin'); |
| 20 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 21 | # korap.ids-mannheim.de specific path prefixing |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 22 | $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 Diewald | 705b74a | 2015-05-07 23:57:34 +0000 | [diff] [blame] | 28 | $c->stash(prefix => '/kalamar'); |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 29 | }; |
| 30 | }) if $self->mode eq 'production'; |
| 31 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 32 | # Cache static assets |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 33 | # (not necessary, as long as shipped by nginx or Apache) |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 34 | $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 Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 42 | # Set secrets for signed cookies |
Nils Diewald | 1940214 | 2015-04-30 15:44:52 +0000 | [diff] [blame] | 43 | if (-e (my $secret = $self->home . '/kalamar.secret')) { |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 44 | |
| 45 | # Load file and split lines for multiple secrets |
| 46 | $self->secrets([b($secret)->slurp->split("\n")]); |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 47 | } |
| 48 | else { |
| 49 | $self->log->warn('Please create a kalamar.secret file'); |
Nils Diewald | 1940214 | 2015-04-30 15:44:52 +0000 | [diff] [blame] | 50 | }; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 51 | |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 52 | # Load plugins |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 53 | foreach ( |
Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 54 | 'Config', # Configuration framework |
| 55 | 'Localize', # Localization framework |
| 56 | 'Notifications', # Client notifications |
| 57 | 'Search', # Abstract Search framework |
| 58 | 'CHI', # Global caching mechanism |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 59 | 'MailException' # Alert via Email on exception |
Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 60 | 'TagHelpers::Pagination', # Pagination widget |
| 61 | 'TagHelpers::MailToChiffre', # Obfuscate email addresses |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 62 | 'KalamarHelpers', # Specific Helpers for Kalamar |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 63 | ) { |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 64 | $self->plugin($_); |
| 65 | }; |
| 66 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 67 | # Configure mail exception |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 68 | $self->plugin('MailException' => $self->config('MailException')); |
| 69 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 70 | # Configure documentation navigation |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 71 | my $navi = b($self->home . '/templates/doc/navigation.json')->slurp; |
| 72 | $self->config(navi => decode_json($navi)) if $navi; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 73 | |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 74 | # Establish routes |
| 75 | my $r = $self->routes; |
| 76 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 77 | # Base query route |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 78 | $r->get('/')->to('search#query')->name('index'); |
| 79 | |
Nils Diewald | a79b268 | 2015-05-18 18:34:06 +0000 | [diff] [blame] | 80 | # Documentation routes |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 81 | $r->get('/doc')->to('documentation#page', page => 'korap')->name('doc_start'); |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 82 | $r->get('/doc/:page')->to('documentation#page', scope => undef); |
| 83 | $r->get('/doc/*scope/:page')->to('documentation#page')->name('doc'); |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 84 | |
Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 85 | # Contact route |
| 86 | $r->get('/contact')->to('documentation#contact'); |
| 87 | $r->get('/contact')->mail_to_chiffre('documentation#contact'); |
| 88 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 89 | # Match route |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 90 | my $corpus = $r->route('/corpus/:corpus_id'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 91 | 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 Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | |
| 98 | 1; |
| 99 | |
| 100 | |
| 101 | __END__ |
Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame] | 102 | |
| 103 | =pod |
| 104 | |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 105 | The static files are generated using Grunt. |
| 106 | To get started with Grunt, you need NodeJS > 0.8 ..., you'll need npm. Then you can install and run grunt: |
Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame] | 107 | |
| 108 | sudo npm install -g grunt-cli |
| 109 | npm install |
| 110 | grunt |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | Some 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 Diewald | 652e5f4 | 2015-05-10 18:11:45 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | =head2 LICENSE |
| 119 | |
| 120 | Highlight.js is released under the BSD License. |