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 | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 16 | # Load documentation navigation |
| 17 | my $navi = b($self->home . '/templates/doc/_nav.json')->slurp; |
| 18 | |
| 19 | # Add additional plugin path |
| 20 | push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin'); |
| 21 | |
| 22 | # Load plugins |
| 23 | foreach (qw/Config |
| 24 | Localize |
| 25 | Notifications |
| 26 | DocNavi |
| 27 | KalamarTagHelpers/) { |
| 28 | $self->plugin($_); |
| 29 | }; |
| 30 | |
| 31 | $self->config(navi => decode_json($navi)); |
| 32 | |
| 33 | $self->plugin('MailException' => $self->config('MailException')); |
| 34 | |
| 35 | # Establish routes |
| 36 | my $r = $self->routes; |
| 37 | |
| 38 | $r->get('/')->to( |
| 39 | cb => sub { |
| 40 | return shift->render(template => 'intro'); |
| 41 | }); |
| 42 | |
| 43 | # Base query page |
| 44 | $r->get('/')->to('search#query')->name('index'); |
| 45 | |
| 46 | |
| 47 | # Documentation |
| 48 | $r->get('/doc')->to('documentation#page', page => 'korap'); |
| 49 | $r->get('/doc/:page')->to('documentation#page', scope => undef); |
| 50 | $r->get('/doc/*scope/:page')->to('documentation#page')->name('doc'); |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | 1; |
| 55 | |
| 56 | |
| 57 | __END__ |
| 58 | |
| 59 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 60 | # Set default totle |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 61 | # $self->defaults( |
| 62 | # layout => 'main', |
| 63 | # title => 'KorAP - Corpus Analysis Platform' |
| 64 | # ); |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 65 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 66 | # Set secret for signed cookies |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 67 | $self->secrets([ |
| 68 | b($self->home . '/kalamar.secret')->slurp->split("\n") |
| 69 | ]); |
Nils Diewald | 33e1555 | 2014-06-13 19:38:37 +0000 | [diff] [blame] | 70 | |
Nils Diewald | a944fab | 2015-04-08 21:02:04 +0000 | [diff] [blame] | 71 | $self->hook( |
| 72 | before_dispatch => sub { |
Nils Diewald | 8462c18 | 2015-03-10 20:57:55 +0000 | [diff] [blame] | 73 | my $c = shift; |
| 74 | my $host = $c->req->headers->header('X-Forwarded-Host'); |
| 75 | if ($host && $host eq 'korap.ids-mannheim.de') { |
| 76 | $c->req->url->base->path('/kalamar/'); |
| 77 | }; |
Nils Diewald | a944fab | 2015-04-08 21:02:04 +0000 | [diff] [blame] | 78 | }) if $self->mode eq 'production'; |
Nils Diewald | 8462c18 | 2015-03-10 20:57:55 +0000 | [diff] [blame] | 79 | |
Nils Diewald | 44e1edb | 2015-04-08 19:12:15 +0000 | [diff] [blame] | 80 | $self->hook(before_dispatch => sub { |
| 81 | my $c = shift; |
| 82 | my $h = $c->res->headers; |
| 83 | $h->header( 'Access-Control-Allow-Origin' => '*' ); |
| 84 | $h->header( 'Access-Control-Allow-Methods' => 'GET, PUT, POST, DELETE, OPTIONS' ); |
| 85 | $h->header( 'Access-Control-Max-Age' => 3600 ); |
| 86 | $h->header( 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' ); |
| 87 | }); |
| 88 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 89 | |
| 90 | # Load plugins |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 91 | foreach (qw/CHI |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 92 | TagHelpers::Pagination |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 93 | Number::Commify |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 94 | Search |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 95 | KalamarHelpers |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 96 | /) { |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 97 | $self->plugin($_); |
| 98 | }; |
Nils Diewald | 2329e1d | 2014-06-12 16:07:57 +0000 | [diff] [blame] | 99 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 100 | # $self->plugin(AssetPack => { minify => 1 }); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 101 | $self->plugin('AssetPack'); |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 102 | $self->plugin('AssetPack::LibSass'); |
| 103 | |
Nils Diewald | 94bae91 | 2014-09-22 13:40:31 +0000 | [diff] [blame] | 104 | # Add assets for AssetPack |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 105 | $self->asset( |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 106 | 'kalamar.css' => ( |
| 107 | |
| 108 | # Sass files |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 109 | '/sass/style.scss', |
| 110 | '/sass/sidebar.scss', |
| 111 | '/sass/tutorial.scss', |
| 112 | '/sass/hint.scss', |
| 113 | '/sass/query.scss', |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 114 | '/sass/matchinfo.scss', |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 115 | '/sass/pagination.scss', |
| 116 | '/sass/kwic-4.0.scss', |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 117 | '/sass/alertify.scss', |
| 118 | |
| 119 | # CSS files |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 120 | '/css/font-awesome.min.css', |
Nils Diewald | 3d87184 | 2015-03-10 23:14:34 +0000 | [diff] [blame] | 121 | '/css/media.css', |
Nils Diewald | ca01b76 | 2014-09-08 02:35:20 +0000 | [diff] [blame] | 122 | '/css/highlight.css', |
Nils Diewald | ca01b76 | 2014-09-08 02:35:20 +0000 | [diff] [blame] | 123 | $self->notifications->styles |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 124 | ) |
| 125 | ); |
| 126 | |
| 127 | $self->asset( |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 128 | 'kalamar.js' => ( |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 129 | # '/js/d3.v3.min.js', |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 130 | # '/js/dagre-d3.min.js', |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 131 | # '/js/dagre-d3.js', |
| 132 | # '/js/translateTree.js', |
Nils Diewald | 4e9fbcb | 2014-07-15 11:45:09 +0000 | [diff] [blame] | 133 | '/js/tutorialCookie.js', |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 134 | '/js/translateTable.js', |
| 135 | '/js/hint.js', |
Nils Diewald | ca01b76 | 2014-09-08 02:35:20 +0000 | [diff] [blame] | 136 | '/js/highlight.pack.js', |
Nils Diewald | e99d904 | 2014-11-20 23:36:54 +0000 | [diff] [blame] | 137 | '/js/ajax.js', |
Nils Diewald | ca01b76 | 2014-09-08 02:35:20 +0000 | [diff] [blame] | 138 | $self->notifications->scripts |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 139 | ) |
| 140 | ); |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 141 | |
Nils Diewald | 89d8801 | 2015-03-10 21:03:36 +0000 | [diff] [blame] | 142 | $self->helper( |
| 143 | date_format => sub { |
| 144 | my ($c, $date) = @_; |
| 145 | return $date; |
| 146 | } |
| 147 | ); |
Nils Diewald | 2329e1d | 2014-06-12 16:07:57 +0000 | [diff] [blame] | 148 | |
Nils Diewald | 5d1ffb4 | 2014-05-21 17:45:34 +0000 | [diff] [blame] | 149 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 150 | # Base search route |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 151 | $r->get('/')->to('search#query')->name('index'); |
| 152 | |
| 153 | # Get match information |
| 154 | my $corpus = $r->route('/corpus/:corpus_id'); |
| 155 | my $doc = $corpus->route('/#doc_id'); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 156 | my $match = $doc->route('/:match_id') |
| 157 | ->to('search#match_info') |
| 158 | ->name('match'); |
Nils Diewald | 33e1555 | 2014-06-13 19:38:37 +0000 | [diff] [blame] | 159 | |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 160 | # Tutorial data |
Nils Diewald | 4e9fbcb | 2014-07-15 11:45:09 +0000 | [diff] [blame] | 161 | $r->get('/tutorial')->to('tutorial#page', tutorial => 'index'); |
| 162 | $r->get('/tutorial/(*tutorial)')->to('tutorial#page')->name('tutorial'); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 163 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 164 | |
| 165 | # Todo: The FAQ-Page has a contact form for new questions |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | |
| 169 | 1; |
| 170 | |
| 171 | |
| 172 | __END__ |