blob: 902934b5a6064dbe894e51c011f3a71b268ec99d [file] [log] [blame]
Nils Diewald2fe12e12015-03-06 16:47:06 +00001package Kalamar;
Nils Diewald5d1ffb42014-05-21 17:45:34 +00002use Mojo::Base 'Mojolicious';
Nils Diewalde2c83812014-11-11 21:13:18 +00003use Mojo::ByteStream 'b';
Nils Diewald5d1ffb42014-05-21 17:45:34 +00004
Nils Diewalda297f062015-04-02 00:23:46 +00005our $VERSION = '0.14';
Nils Diewald7cad8402014-07-08 17:06:56 +00006
Nils Diewald002e8fb2014-06-22 14:27:01 +00007# Start the application and register all routes and plugins
Nils Diewald5d1ffb42014-05-21 17:45:34 +00008sub startup {
9 my $self = shift;
10
Nils Diewald2fe12e12015-03-06 16:47:06 +000011 # Set default totle
12 $self->defaults(
13 layout => 'default',
Nils Diewaldd2b57372015-03-10 20:09:48 +000014 title => 'KorAP - Corpus Analysis Platform'
Nils Diewald2fe12e12015-03-06 16:47:06 +000015 );
Nils Diewald7cad8402014-07-08 17:06:56 +000016
Nils Diewald002e8fb2014-06-22 14:27:01 +000017 # Set secret for signed cookies
Nils Diewald2fe12e12015-03-06 16:47:06 +000018 $self->secrets([
19 b($self->home . '/kalamar.secret')->slurp->split("\n")
20 ]);
Nils Diewald33e15552014-06-13 19:38:37 +000021
Nils Diewald8462c182015-03-10 20:57:55 +000022 $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 Diewald44e1edb2015-04-08 19:12:15 +000030 $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 Diewald002e8fb2014-06-22 14:27:01 +000040 # Add additional plugin path
Nils Diewald5d1ffb42014-05-21 17:45:34 +000041 push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin');
Nils Diewald002e8fb2014-06-22 14:27:01 +000042
43 # Load plugins
44 foreach (qw/Config
45 CHI
46 TagHelpers::Pagination
47 Notifications
48 Number::Commify
Nils Diewald996aa552014-12-02 03:26:44 +000049 Search
Nils Diewald2fe12e12015-03-06 16:47:06 +000050 KalamarHelpers
51 KalamarTagHelpers/) {
Nils Diewald002e8fb2014-06-22 14:27:01 +000052 $self->plugin($_);
53 };
Nils Diewald2329e1d2014-06-12 16:07:57 +000054
Nils Diewald2fe12e12015-03-06 16:47:06 +000055 # $self->plugin(AssetPack => { minify => 1 });
Nils Diewald996aa552014-12-02 03:26:44 +000056 $self->plugin('AssetPack');
Nils Diewald7cad8402014-07-08 17:06:56 +000057 $self->plugin('AssetPack::LibSass');
Nils Diewald4e9fbcb2014-07-15 11:45:09 +000058 $self->plugin('MailException' => $self->config('MailException'));
Nils Diewald7cad8402014-07-08 17:06:56 +000059
Nils Diewald94bae912014-09-22 13:40:31 +000060 # Add assets for AssetPack
Nils Diewald7cad8402014-07-08 17:06:56 +000061 $self->asset(
Nils Diewald2fe12e12015-03-06 16:47:06 +000062 'kalamar.css' => (
63
64 # Sass files
Nils Diewald7cad8402014-07-08 17:06:56 +000065 '/sass/style.scss',
66 '/sass/sidebar.scss',
67 '/sass/tutorial.scss',
68 '/sass/hint.scss',
69 '/sass/query.scss',
Nils Diewaldf2e02a92014-11-12 18:31:05 +000070 '/sass/matchinfo.scss',
Nils Diewald7cad8402014-07-08 17:06:56 +000071 '/sass/pagination.scss',
72 '/sass/kwic-4.0.scss',
Nils Diewald2fe12e12015-03-06 16:47:06 +000073 '/sass/alertify.scss',
74
75 # CSS files
Nils Diewald7cad8402014-07-08 17:06:56 +000076 '/css/font-awesome.min.css',
Nils Diewald3d871842015-03-10 23:14:34 +000077 '/css/media.css',
Nils Diewaldca01b762014-09-08 02:35:20 +000078 '/css/highlight.css',
Nils Diewaldca01b762014-09-08 02:35:20 +000079 $self->notifications->styles
Nils Diewald7cad8402014-07-08 17:06:56 +000080 )
81 );
82
83 $self->asset(
Nils Diewald2fe12e12015-03-06 16:47:06 +000084 'kalamar.js' => (
Nils Diewald8f4b5da2014-12-03 22:13:39 +000085# '/js/d3.v3.min.js',
Nils Diewaldf2e02a92014-11-12 18:31:05 +000086# '/js/dagre-d3.min.js',
Nils Diewald8f4b5da2014-12-03 22:13:39 +000087# '/js/dagre-d3.js',
88# '/js/translateTree.js',
Nils Diewald4e9fbcb2014-07-15 11:45:09 +000089 '/js/tutorialCookie.js',
Nils Diewald7cad8402014-07-08 17:06:56 +000090 '/js/translateTable.js',
91 '/js/hint.js',
Nils Diewaldca01b762014-09-08 02:35:20 +000092 '/js/highlight.pack.js',
Nils Diewalde99d9042014-11-20 23:36:54 +000093 '/js/ajax.js',
Nils Diewaldca01b762014-09-08 02:35:20 +000094 $self->notifications->scripts
Nils Diewald7cad8402014-07-08 17:06:56 +000095 )
96 );
Nils Diewald4af3f0b2014-06-25 01:43:17 +000097
Nils Diewald89d88012015-03-10 21:03:36 +000098 $self->helper(
99 date_format => sub {
100 my ($c, $date) = @_;
101 return $date;
102 }
103 );
Nils Diewald2329e1d2014-06-12 16:07:57 +0000104
Nils Diewald64bab252014-05-22 11:04:04 +0000105 # Routes
Nils Diewald5d1ffb42014-05-21 17:45:34 +0000106 my $r = $self->routes;
107
Nils Diewald002e8fb2014-06-22 14:27:01 +0000108 # Base search route
Nils Diewald8f4b5da2014-12-03 22:13:39 +0000109 $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 Diewald2fe12e12015-03-06 16:47:06 +0000114 my $match = $doc->route('/:match_id')
115 ->to('search#match_info')
116 ->name('match');
Nils Diewald33e15552014-06-13 19:38:37 +0000117
Nils Diewald002e8fb2014-06-22 14:27:01 +0000118 # Tutorial data
Nils Diewald4e9fbcb2014-07-15 11:45:09 +0000119 $r->get('/tutorial')->to('tutorial#page', tutorial => 'index');
120 $r->get('/tutorial/(*tutorial)')->to('tutorial#page')->name('tutorial');
Nils Diewald996aa552014-12-02 03:26:44 +0000121};
122
123
1241;
125
126
127__END__