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 | 5d1ffb4 | 2014-05-21 17:45:34 +0000 | [diff] [blame] | 4 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 5 | our $VERSION = '0.14'; |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 6 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 7 | # Start the application and register all routes and plugins |
Nils Diewald | 5d1ffb4 | 2014-05-21 17:45:34 +0000 | [diff] [blame] | 8 | sub startup { |
| 9 | my $self = shift; |
| 10 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 11 | # Set default totle |
| 12 | $self->defaults( |
| 13 | layout => 'default', |
Nils Diewald | d2b5737 | 2015-03-10 20:09:48 +0000 | [diff] [blame] | 14 | title => 'KorAP - Corpus Analysis Platform' |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 15 | ); |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 16 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 17 | # Set secret for signed cookies |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 18 | $self->secrets([ |
| 19 | b($self->home . '/kalamar.secret')->slurp->split("\n") |
| 20 | ]); |
Nils Diewald | 33e1555 | 2014-06-13 19:38:37 +0000 | [diff] [blame] | 21 | |
Nils Diewald | 8462c18 | 2015-03-10 20:57:55 +0000 | [diff] [blame] | 22 | $self->hook(before_dispatch => sub { |
| 23 | my $c = shift; |
| 24 | my $host = $c->req->headers->header('X-Forwarded-Host'); |
| 25 | if ($host && $host eq 'korap.ids-mannheim.de') { |
| 26 | $c->req->url->base->path('/kalamar/'); |
| 27 | }; |
| 28 | }) if $self->mode eq 'production'; |
| 29 | |
Nils Diewald | 44e1edb | 2015-04-08 19:12:15 +0000 | [diff] [blame^] | 30 | $self->hook(before_dispatch => sub { |
| 31 | my $c = shift; |
| 32 | my $h = $c->res->headers; |
| 33 | $h->header( 'Access-Control-Allow-Origin' => '*' ); |
| 34 | $h->header( 'Access-Control-Allow-Methods' => 'GET, PUT, POST, DELETE, OPTIONS' ); |
| 35 | $h->header( 'Access-Control-Max-Age' => 3600 ); |
| 36 | $h->header( 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' ); |
| 37 | }); |
| 38 | |
| 39 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 40 | # Add additional plugin path |
Nils Diewald | 5d1ffb4 | 2014-05-21 17:45:34 +0000 | [diff] [blame] | 41 | push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin'); |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 42 | |
| 43 | # Load plugins |
| 44 | foreach (qw/Config |
| 45 | CHI |
| 46 | TagHelpers::Pagination |
| 47 | Notifications |
| 48 | Number::Commify |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 49 | Search |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 50 | KalamarHelpers |
| 51 | KalamarTagHelpers/) { |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 52 | $self->plugin($_); |
| 53 | }; |
Nils Diewald | 2329e1d | 2014-06-12 16:07:57 +0000 | [diff] [blame] | 54 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 55 | # $self->plugin(AssetPack => { minify => 1 }); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 56 | $self->plugin('AssetPack'); |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 57 | $self->plugin('AssetPack::LibSass'); |
Nils Diewald | 4e9fbcb | 2014-07-15 11:45:09 +0000 | [diff] [blame] | 58 | $self->plugin('MailException' => $self->config('MailException')); |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 59 | |
Nils Diewald | 94bae91 | 2014-09-22 13:40:31 +0000 | [diff] [blame] | 60 | # Add assets for AssetPack |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 61 | $self->asset( |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 62 | 'kalamar.css' => ( |
| 63 | |
| 64 | # Sass files |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 65 | '/sass/style.scss', |
| 66 | '/sass/sidebar.scss', |
| 67 | '/sass/tutorial.scss', |
| 68 | '/sass/hint.scss', |
| 69 | '/sass/query.scss', |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 70 | '/sass/matchinfo.scss', |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 71 | '/sass/pagination.scss', |
| 72 | '/sass/kwic-4.0.scss', |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 73 | '/sass/alertify.scss', |
| 74 | |
| 75 | # CSS files |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 76 | '/css/font-awesome.min.css', |
Nils Diewald | 3d87184 | 2015-03-10 23:14:34 +0000 | [diff] [blame] | 77 | '/css/media.css', |
Nils Diewald | ca01b76 | 2014-09-08 02:35:20 +0000 | [diff] [blame] | 78 | '/css/highlight.css', |
Nils Diewald | ca01b76 | 2014-09-08 02:35:20 +0000 | [diff] [blame] | 79 | $self->notifications->styles |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 80 | ) |
| 81 | ); |
| 82 | |
| 83 | $self->asset( |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 84 | 'kalamar.js' => ( |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 85 | # '/js/d3.v3.min.js', |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 86 | # '/js/dagre-d3.min.js', |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 87 | # '/js/dagre-d3.js', |
| 88 | # '/js/translateTree.js', |
Nils Diewald | 4e9fbcb | 2014-07-15 11:45:09 +0000 | [diff] [blame] | 89 | '/js/tutorialCookie.js', |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 90 | '/js/translateTable.js', |
| 91 | '/js/hint.js', |
Nils Diewald | ca01b76 | 2014-09-08 02:35:20 +0000 | [diff] [blame] | 92 | '/js/highlight.pack.js', |
Nils Diewald | e99d904 | 2014-11-20 23:36:54 +0000 | [diff] [blame] | 93 | '/js/ajax.js', |
Nils Diewald | ca01b76 | 2014-09-08 02:35:20 +0000 | [diff] [blame] | 94 | $self->notifications->scripts |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 95 | ) |
| 96 | ); |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 97 | |
Nils Diewald | 89d8801 | 2015-03-10 21:03:36 +0000 | [diff] [blame] | 98 | $self->helper( |
| 99 | date_format => sub { |
| 100 | my ($c, $date) = @_; |
| 101 | return $date; |
| 102 | } |
| 103 | ); |
Nils Diewald | 2329e1d | 2014-06-12 16:07:57 +0000 | [diff] [blame] | 104 | |
Nils Diewald | 64bab25 | 2014-05-22 11:04:04 +0000 | [diff] [blame] | 105 | # Routes |
Nils Diewald | 5d1ffb4 | 2014-05-21 17:45:34 +0000 | [diff] [blame] | 106 | my $r = $self->routes; |
| 107 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 108 | # Base search route |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 109 | $r->get('/')->to('search#query')->name('index'); |
| 110 | |
| 111 | # Get match information |
| 112 | my $corpus = $r->route('/corpus/:corpus_id'); |
| 113 | my $doc = $corpus->route('/#doc_id'); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 114 | my $match = $doc->route('/:match_id') |
| 115 | ->to('search#match_info') |
| 116 | ->name('match'); |
Nils Diewald | 33e1555 | 2014-06-13 19:38:37 +0000 | [diff] [blame] | 117 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 118 | # Tutorial data |
Nils Diewald | 4e9fbcb | 2014-07-15 11:45:09 +0000 | [diff] [blame] | 119 | $r->get('/tutorial')->to('tutorial#page', tutorial => 'index'); |
| 120 | $r->get('/tutorial/(*tutorial)')->to('tutorial#page')->name('tutorial'); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | |
| 124 | 1; |
| 125 | |
| 126 | |
| 127 | __END__ |