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