blob: 1f55517a57a582442857a0666c7fdfb133edb109 [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 Diewalda944fab2015-04-08 21:02:04 +00004use Mojo::JSON 'decode_json';
Nils Diewald5d1ffb42014-05-21 17:45:34 +00005
Nils Diewalda944fab2015-04-08 21:02:04 +00006our $VERSION;
Nils Diewald7cad8402014-07-08 17:06:56 +00007
Nils Diewald002e8fb2014-06-22 14:27:01 +00008# Start the application and register all routes and plugins
Nils Diewald5d1ffb42014-05-21 17:45:34 +00009sub startup {
10 my $self = shift;
11
Nils Diewalda944fab2015-04-08 21:02:04 +000012 # Set version based on package file
13 my $pkg = b($self->home . '/package.json')->slurp;
14 $Kalamar::VERSION = decode_json($pkg)->{version};
15
Nils Diewald2fe12e12015-03-06 16:47:06 +000016 # Set default totle
17 $self->defaults(
Nils Diewald7c8ced22015-04-15 19:21:00 +000018 layout => 'main',
Nils Diewaldd2b57372015-03-10 20:09:48 +000019 title => 'KorAP - Corpus Analysis Platform'
Nils Diewald2fe12e12015-03-06 16:47:06 +000020 );
Nils Diewald7cad8402014-07-08 17:06:56 +000021
Nils Diewald002e8fb2014-06-22 14:27:01 +000022 # Set secret for signed cookies
Nils Diewald2fe12e12015-03-06 16:47:06 +000023 $self->secrets([
24 b($self->home . '/kalamar.secret')->slurp->split("\n")
25 ]);
Nils Diewald33e15552014-06-13 19:38:37 +000026
Nils Diewalda944fab2015-04-08 21:02:04 +000027 $self->hook(
28 before_dispatch => sub {
Nils Diewald8462c182015-03-10 20:57:55 +000029 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 Diewalda944fab2015-04-08 21:02:04 +000034 }) if $self->mode eq 'production';
Nils Diewald8462c182015-03-10 20:57:55 +000035
Nils Diewald44e1edb2015-04-08 19:12:15 +000036 $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 Diewald002e8fb2014-06-22 14:27:01 +000045 # Add additional plugin path
Nils Diewald5d1ffb42014-05-21 17:45:34 +000046 push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin');
Nils Diewald002e8fb2014-06-22 14:27:01 +000047
48 # Load plugins
49 foreach (qw/Config
50 CHI
51 TagHelpers::Pagination
52 Notifications
53 Number::Commify
Nils Diewald996aa552014-12-02 03:26:44 +000054 Search
Nils Diewald2fe12e12015-03-06 16:47:06 +000055 KalamarHelpers
56 KalamarTagHelpers/) {
Nils Diewald002e8fb2014-06-22 14:27:01 +000057 $self->plugin($_);
58 };
Nils Diewald2329e1d2014-06-12 16:07:57 +000059
Nils Diewald2fe12e12015-03-06 16:47:06 +000060 # $self->plugin(AssetPack => { minify => 1 });
Nils Diewald996aa552014-12-02 03:26:44 +000061 $self->plugin('AssetPack');
Nils Diewald7cad8402014-07-08 17:06:56 +000062 $self->plugin('AssetPack::LibSass');
Nils Diewald4e9fbcb2014-07-15 11:45:09 +000063 $self->plugin('MailException' => $self->config('MailException'));
Nils Diewald7cad8402014-07-08 17:06:56 +000064
Nils Diewald94bae912014-09-22 13:40:31 +000065 # Add assets for AssetPack
Nils Diewald7cad8402014-07-08 17:06:56 +000066 $self->asset(
Nils Diewald2fe12e12015-03-06 16:47:06 +000067 'kalamar.css' => (
68
69 # Sass files
Nils Diewald7cad8402014-07-08 17:06:56 +000070 '/sass/style.scss',
71 '/sass/sidebar.scss',
72 '/sass/tutorial.scss',
73 '/sass/hint.scss',
74 '/sass/query.scss',
Nils Diewaldf2e02a92014-11-12 18:31:05 +000075 '/sass/matchinfo.scss',
Nils Diewald7cad8402014-07-08 17:06:56 +000076 '/sass/pagination.scss',
77 '/sass/kwic-4.0.scss',
Nils Diewald2fe12e12015-03-06 16:47:06 +000078 '/sass/alertify.scss',
79
80 # CSS files
Nils Diewald7cad8402014-07-08 17:06:56 +000081 '/css/font-awesome.min.css',
Nils Diewald3d871842015-03-10 23:14:34 +000082 '/css/media.css',
Nils Diewaldca01b762014-09-08 02:35:20 +000083 '/css/highlight.css',
Nils Diewaldca01b762014-09-08 02:35:20 +000084 $self->notifications->styles
Nils Diewald7cad8402014-07-08 17:06:56 +000085 )
86 );
87
88 $self->asset(
Nils Diewald2fe12e12015-03-06 16:47:06 +000089 'kalamar.js' => (
Nils Diewald8f4b5da2014-12-03 22:13:39 +000090# '/js/d3.v3.min.js',
Nils Diewaldf2e02a92014-11-12 18:31:05 +000091# '/js/dagre-d3.min.js',
Nils Diewald8f4b5da2014-12-03 22:13:39 +000092# '/js/dagre-d3.js',
93# '/js/translateTree.js',
Nils Diewald4e9fbcb2014-07-15 11:45:09 +000094 '/js/tutorialCookie.js',
Nils Diewald7cad8402014-07-08 17:06:56 +000095 '/js/translateTable.js',
96 '/js/hint.js',
Nils Diewaldca01b762014-09-08 02:35:20 +000097 '/js/highlight.pack.js',
Nils Diewalde99d9042014-11-20 23:36:54 +000098 '/js/ajax.js',
Nils Diewaldca01b762014-09-08 02:35:20 +000099 $self->notifications->scripts
Nils Diewald7cad8402014-07-08 17:06:56 +0000100 )
101 );
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000102
Nils Diewald89d88012015-03-10 21:03:36 +0000103 $self->helper(
104 date_format => sub {
105 my ($c, $date) = @_;
106 return $date;
107 }
108 );
Nils Diewald2329e1d2014-06-12 16:07:57 +0000109
Nils Diewald64bab252014-05-22 11:04:04 +0000110 # Routes
Nils Diewald5d1ffb42014-05-21 17:45:34 +0000111 my $r = $self->routes;
112
Nils Diewald002e8fb2014-06-22 14:27:01 +0000113 # Base search route
Nils Diewald8f4b5da2014-12-03 22:13:39 +0000114 $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 Diewald2fe12e12015-03-06 16:47:06 +0000119 my $match = $doc->route('/:match_id')
120 ->to('search#match_info')
121 ->name('match');
Nils Diewald33e15552014-06-13 19:38:37 +0000122
Nils Diewald002e8fb2014-06-22 14:27:01 +0000123 # Tutorial data
Nils Diewald4e9fbcb2014-07-15 11:45:09 +0000124 $r->get('/tutorial')->to('tutorial#page', tutorial => 'index');
125 $r->get('/tutorial/(*tutorial)')->to('tutorial#page')->name('tutorial');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000126
127 $r->get('/doc/korap')->to(
128 cb => sub {
129 return shift->render(template => 'doc/korap');
130 });
131
132 # Todo: The FAQ-Page has a contact form for new questions
Nils Diewald996aa552014-12-02 03:26:44 +0000133};
134
135
1361;
137
138
139__END__