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