blob: abd4d5ecd95200b0f428df7d74e6f98e125c0e86 [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 Diewaldab4d3ca2015-04-17 01:48:43 +000016 # 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
541;
55
56
57__END__
58
59
Nils Diewald2fe12e12015-03-06 16:47:06 +000060 # Set default totle
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000061# $self->defaults(
62# layout => 'main',
63# title => 'KorAP - Corpus Analysis Platform'
64# );
Nils Diewald7cad8402014-07-08 17:06:56 +000065
Nils Diewald002e8fb2014-06-22 14:27:01 +000066 # Set secret for signed cookies
Nils Diewald2fe12e12015-03-06 16:47:06 +000067 $self->secrets([
68 b($self->home . '/kalamar.secret')->slurp->split("\n")
69 ]);
Nils Diewald33e15552014-06-13 19:38:37 +000070
Nils Diewalda944fab2015-04-08 21:02:04 +000071 $self->hook(
72 before_dispatch => sub {
Nils Diewald8462c182015-03-10 20:57:55 +000073 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 Diewalda944fab2015-04-08 21:02:04 +000078 }) if $self->mode eq 'production';
Nils Diewald8462c182015-03-10 20:57:55 +000079
Nils Diewald44e1edb2015-04-08 19:12:15 +000080 $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 Diewald002e8fb2014-06-22 14:27:01 +000089
90 # Load plugins
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000091 foreach (qw/CHI
Nils Diewald002e8fb2014-06-22 14:27:01 +000092 TagHelpers::Pagination
Nils Diewald002e8fb2014-06-22 14:27:01 +000093 Number::Commify
Nils Diewald996aa552014-12-02 03:26:44 +000094 Search
Nils Diewald2fe12e12015-03-06 16:47:06 +000095 KalamarHelpers
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000096 /) {
Nils Diewald002e8fb2014-06-22 14:27:01 +000097 $self->plugin($_);
98 };
Nils Diewald2329e1d2014-06-12 16:07:57 +000099
Nils Diewald2fe12e12015-03-06 16:47:06 +0000100 # $self->plugin(AssetPack => { minify => 1 });
Nils Diewald996aa552014-12-02 03:26:44 +0000101 $self->plugin('AssetPack');
Nils Diewald7cad8402014-07-08 17:06:56 +0000102 $self->plugin('AssetPack::LibSass');
103
Nils Diewald94bae912014-09-22 13:40:31 +0000104 # Add assets for AssetPack
Nils Diewald7cad8402014-07-08 17:06:56 +0000105 $self->asset(
Nils Diewald2fe12e12015-03-06 16:47:06 +0000106 'kalamar.css' => (
107
108 # Sass files
Nils Diewald7cad8402014-07-08 17:06:56 +0000109 '/sass/style.scss',
110 '/sass/sidebar.scss',
111 '/sass/tutorial.scss',
112 '/sass/hint.scss',
113 '/sass/query.scss',
Nils Diewaldf2e02a92014-11-12 18:31:05 +0000114 '/sass/matchinfo.scss',
Nils Diewald7cad8402014-07-08 17:06:56 +0000115 '/sass/pagination.scss',
116 '/sass/kwic-4.0.scss',
Nils Diewald2fe12e12015-03-06 16:47:06 +0000117 '/sass/alertify.scss',
118
119 # CSS files
Nils Diewald7cad8402014-07-08 17:06:56 +0000120 '/css/font-awesome.min.css',
Nils Diewald3d871842015-03-10 23:14:34 +0000121 '/css/media.css',
Nils Diewaldca01b762014-09-08 02:35:20 +0000122 '/css/highlight.css',
Nils Diewaldca01b762014-09-08 02:35:20 +0000123 $self->notifications->styles
Nils Diewald7cad8402014-07-08 17:06:56 +0000124 )
125 );
126
127 $self->asset(
Nils Diewald2fe12e12015-03-06 16:47:06 +0000128 'kalamar.js' => (
Nils Diewald8f4b5da2014-12-03 22:13:39 +0000129# '/js/d3.v3.min.js',
Nils Diewaldf2e02a92014-11-12 18:31:05 +0000130# '/js/dagre-d3.min.js',
Nils Diewald8f4b5da2014-12-03 22:13:39 +0000131# '/js/dagre-d3.js',
132# '/js/translateTree.js',
Nils Diewald4e9fbcb2014-07-15 11:45:09 +0000133 '/js/tutorialCookie.js',
Nils Diewald7cad8402014-07-08 17:06:56 +0000134 '/js/translateTable.js',
135 '/js/hint.js',
Nils Diewaldca01b762014-09-08 02:35:20 +0000136 '/js/highlight.pack.js',
Nils Diewalde99d9042014-11-20 23:36:54 +0000137 '/js/ajax.js',
Nils Diewaldca01b762014-09-08 02:35:20 +0000138 $self->notifications->scripts
Nils Diewald7cad8402014-07-08 17:06:56 +0000139 )
140 );
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000141
Nils Diewald89d88012015-03-10 21:03:36 +0000142 $self->helper(
143 date_format => sub {
144 my ($c, $date) = @_;
145 return $date;
146 }
147 );
Nils Diewald2329e1d2014-06-12 16:07:57 +0000148
Nils Diewald5d1ffb42014-05-21 17:45:34 +0000149
Nils Diewald002e8fb2014-06-22 14:27:01 +0000150 # Base search route
Nils Diewald8f4b5da2014-12-03 22:13:39 +0000151 $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 Diewald2fe12e12015-03-06 16:47:06 +0000156 my $match = $doc->route('/:match_id')
157 ->to('search#match_info')
158 ->name('match');
Nils Diewald33e15552014-06-13 19:38:37 +0000159
Nils Diewald002e8fb2014-06-22 14:27:01 +0000160 # Tutorial data
Nils Diewald4e9fbcb2014-07-15 11:45:09 +0000161 $r->get('/tutorial')->to('tutorial#page', tutorial => 'index');
162 $r->get('/tutorial/(*tutorial)')->to('tutorial#page')->name('tutorial');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000163
Nils Diewald7c8ced22015-04-15 19:21:00 +0000164
165 # Todo: The FAQ-Page has a contact form for new questions
Nils Diewald996aa552014-12-02 03:26:44 +0000166};
167
168
1691;
170
171
172__END__