| 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 | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 21 | # korap.ids-mannheim.de specific | 
|  | 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 | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 32 | # Set secrets for signed cookies | 
| Nils Diewald | 1940214 | 2015-04-30 15:44:52 +0000 | [diff] [blame] | 33 | if (-e (my $secret = $self->home . '/kalamar.secret')) { | 
|  | 34 | $self->secrets([ | 
|  | 35 | b($secret)->slurp->split("\n") | 
|  | 36 | ]); | 
| Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 37 | } | 
|  | 38 | else { | 
|  | 39 | $self->log->warn('Please create a kalamar.secret file'); | 
| Nils Diewald | 1940214 | 2015-04-30 15:44:52 +0000 | [diff] [blame] | 40 | }; | 
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 41 |  | 
| Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 42 | # Load plugins | 
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 43 | foreach ( | 
| Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 44 | 'Config',                    # Configuration framework | 
|  | 45 | 'Localize',                  # Localization framework | 
|  | 46 | 'Notifications',             # Client notifications | 
|  | 47 | 'Search',                    # Abstract Search framework | 
|  | 48 | 'CHI',                       # Global caching mechanism | 
|  | 49 | 'TagHelpers::Pagination',    # Pagination widget | 
|  | 50 | 'TagHelpers::MailToChiffre', # Obfuscate email addresses | 
|  | 51 | 'KalamarHelpers'             # Specific Helpers for Kalamar | 
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 52 | ) { | 
| Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 53 | $self->plugin($_); | 
|  | 54 | }; | 
|  | 55 |  | 
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 56 | # Configure mail exception | 
| Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 57 | $self->plugin('MailException' => $self->config('MailException')); | 
|  | 58 |  | 
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 59 | # Configure documentation navigation | 
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 60 | my $navi = b($self->home . '/templates/doc/navigation.json')->slurp; | 
|  | 61 | $self->config(navi => decode_json($navi)) if $navi; | 
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 62 |  | 
| Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 63 | # Establish routes | 
|  | 64 | my $r = $self->routes; | 
|  | 65 |  | 
| Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 66 | # Base query page | 
|  | 67 | $r->get('/')->to('search#query')->name('index'); | 
|  | 68 |  | 
| Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 69 | # Documentation | 
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 70 | $r->get('/doc')->to('documentation#page', page => 'korap')->name('doc_start'); | 
| Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 71 | $r->get('/doc/:page')->to('documentation#page', scope => undef); | 
|  | 72 | $r->get('/doc/*scope/:page')->to('documentation#page')->name('doc'); | 
| Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 73 |  | 
| Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 74 | # Contact route | 
|  | 75 | $r->get('/contact')->to('documentation#contact'); | 
|  | 76 | $r->get('/contact')->mail_to_chiffre('documentation#contact'); | 
|  | 77 |  | 
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 78 | # Match route | 
| Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 79 | my $corpus = $r->route('/corpus/:corpus_id'); | 
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 80 | my $doc    = $corpus->get('/:doc_id'); | 
|  | 81 | my $text   = $doc->get('/:text_id'); | 
|  | 82 | my $match  = $text->get('/:match_id'); | 
|  | 83 | $match->to('search#match_info')->name('match'); | 
| Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 84 | }; | 
|  | 85 |  | 
|  | 86 |  | 
|  | 87 | 1; | 
|  | 88 |  | 
|  | 89 |  | 
|  | 90 | __END__ | 
| Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame] | 91 |  | 
|  | 92 | =pod | 
|  | 93 |  | 
| Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 94 | The static files are generated using Grunt. | 
|  | 95 | 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] | 96 |  | 
|  | 97 | sudo npm install -g grunt-cli | 
|  | 98 | npm install | 
|  | 99 | grunt | 
| Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 100 |  | 
|  | 101 |  | 
|  | 102 | Some perl modules are not on github yet, so you need to install them from github using cpanm: | 
|  | 103 |  | 
|  | 104 | cpanm git://github.com/Akron/Mojolicious-Plugin-Localize.git |