blob: 9f066973762bf406973dc0b2c80380cdc751c132 [file] [log] [blame]
Akron0e1ed242018-10-11 13:22:00 +02001#!/usr/bin/env perl
2use Mojolicious::Lite;
3use Mojo::ByteStream 'b';
4use Mojo::Date;
5use Mojo::JSON qw/true false encode_json decode_json/;
6use strict;
7use warnings;
8use Mojo::JWT;
Akron6d49c1f2018-10-11 14:22:21 +02009use Mojo::File qw/path/;
10use Mojo::Util qw/slugify/;
Akron0e1ed242018-10-11 13:22:00 +020011
12# This is an API fake server with fixtures
13
14my $secret = 's3cr3t';
Akron6d49c1f2018-10-11 14:22:21 +020015my $fixture_path = path(Mojo::File->new(__FILE__)->dirname);
Akron0e1ed242018-10-11 13:22:00 +020016
17helper jwt_encode => sub {
18 shift;
19 return Mojo::JWT->new(
20 secret => $secret,
21 token_type => 'api_token',
22 expires => time + (3 * 34 * 60 * 60),
23 claims => { @_ }
24 );
25};
26
27helper jwt_decode => sub {
28 my ($c, $auth) = @_;
29 $auth =~ s/\s*api_token\s+//;
30 return Mojo::JWT->new(secret => $secret)->decode($auth);
31};
32
33
Akron6d49c1f2018-10-11 14:22:21 +020034# Load fixture responses
35helper 'load_response' => sub {
36 my $c = shift;
37 my $q_name = shift;
38 my $file = $fixture_path->child("response_$q_name.json");
Akron8ea84292018-10-24 13:41:52 +020039 $c->app->log->debug("Load response from $file");
40
Akron6d49c1f2018-10-11 14:22:21 +020041 unless (-f $file) {
42 return {
43 status => 500,
44 json => {
45 errors => [[0, 'Unable to load query response from ' . $file]]
46 }
47 }
48 };
Akron8ea84292018-10-24 13:41:52 +020049
Akron6d49c1f2018-10-11 14:22:21 +020050 my $response = $file->slurp;
51 return decode_json($response);
52};
53
54
Akron0e1ed242018-10-11 13:22:00 +020055# Base page
56get '/' => sub {
Akron6d49c1f2018-10-11 14:22:21 +020057 shift->render(text => 'Fake server available');
Akron0e1ed242018-10-11 13:22:00 +020058};
59
Akron32396632018-10-11 17:08:37 +020060
Akron0e1ed242018-10-11 13:22:00 +020061# Search fixtures
62get '/search' => sub {
63 my $c = shift;
64 my $v = $c->validation;
65 $v->optional('q');
66 $v->optional('page');
67 $v->optional('ql');
68 $v->optional('count');
69 $v->optional('context');
Akron8ea84292018-10-24 13:41:52 +020070 $v->optional('offset');
71 $v->optional('cutoff')->in(qw/true false/);
Akron0e1ed242018-10-11 13:22:00 +020072
Akron32396632018-10-11 17:08:37 +020073 $c->app->log->debug('Receive request');
74
Akron0e1ed242018-10-11 13:22:00 +020075 # Response q=x&ql=cosmas3
76 if ($v->param('ql') && $v->param('ql') eq 'cosmas3') {
77 return $c->render(
78 status => 400,
79 json => {
80 "\@context" => "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
81 "errors" => [[307,"cosmas3 is not a supported query language!"]]
82 });
83 };
84
Akron6d49c1f2018-10-11 14:22:21 +020085 if (!$v->param('q')) {
Akron8ea84292018-10-24 13:41:52 +020086 return $c->render(%{$c->load_response('query_no_query')});
Akron0e1ed242018-10-11 13:22:00 +020087 };
88
Akron8ea84292018-10-24 13:41:52 +020089 my @slug_base = ($v->param('q'));
90 push @slug_base, 'o' . $v->param('offset') if defined $v->param('offset');
91 push @slug_base, 'c' . $v->param('count') if defined $v->param('count');
92 push @slug_base, 'co' . $v->param('cutoff') if defined $v->param('cutoff');
93
Akron6d49c1f2018-10-11 14:22:21 +020094 # Get response based on query parameter
Akron8ea84292018-10-24 13:41:52 +020095 my $response = $c->load_response('query_' . slugify(join('_', @slug_base)));
Akron0e1ed242018-10-11 13:22:00 +020096
97 # Check authentification
98 if (my $auth = $c->req->headers->header('Authorization')) {
99 if (my $jwt = $c->jwt_decode($auth)) {
Akron6d49c1f2018-10-11 14:22:21 +0200100 $response->{json}->{meta}->{authorized} = $jwt->{username} if $jwt->{username};
Akron0e1ed242018-10-11 13:22:00 +0200101 };
102 };
103
Akron6d49c1f2018-10-11 14:22:21 +0200104 # Set page parameter
Akron0e1ed242018-10-11 13:22:00 +0200105 if ($v->param('page')) {
Akron6d49c1f2018-10-11 14:22:21 +0200106 $response->{json}->{meta}->{startIndex} = $v->param("startIndex");
Akron0e1ed242018-10-11 13:22:00 +0200107 };
108
Akron0e1ed242018-10-11 13:22:00 +0200109 # Simple search fixture
Akron32396632018-10-11 17:08:37 +0200110 $c->render(%$response);
111
112 $c->app->log->debug('Rendered result');
113
114 return 1;
Akron0e1ed242018-10-11 13:22:00 +0200115};
116
117
Akronb80341d2018-10-15 19:46:23 +0200118# Matchinfo fixtures
119get '/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub {
120 my $c = shift;
121
122 my $file = join('_', (
123 'matchinfo',
124 $c->stash('corpusId'),
125 $c->stash('docId'),
126 $c->stash('textId'),
127 $c->stash('matchId')
128 ));
129
Akronb8d0b402018-10-18 23:51:52 +0200130 my $slug = slugify($file);
131
Akronb80341d2018-10-15 19:46:23 +0200132 # Get response based on query parameter
Akronb8d0b402018-10-18 23:51:52 +0200133 my $response = $c->load_response($slug);
Akronb80341d2018-10-15 19:46:23 +0200134 return $c->render(%$response);
135};
136
Akron0e1ed242018-10-11 13:22:00 +0200137
Akronbe61f4c2018-10-20 00:52:58 +0200138# Statistics endpoint
139get '/statistics' => sub {
140 my $c = shift;
141 my $v = $c->validation;
142 $v->optional('corpusQuery');
143
144 my @list = 'corpusinfo';
145 if ($v->param('corpusQuery')) {
146 push @list, $v->param('corpusQuery');
147 };
148 my $slug = slugify(join('_', @list));
149
150 # Get response based on query parameter
151 my $response = $c->load_response($slug);
152 return $c->render(%$response);
153};
154
Akron0e1ed242018-10-11 13:22:00 +0200155############
156# Auth API #
157############
158
159# Request API token
160get '/auth/logout' => sub {
161 my $c = shift;
162
163 if (my $auth = $c->req->headers->header('Authorization')) {
164 if (my $jwt = $c->jwt_decode($auth)) {
165 my $user = $jwt->{username} if $jwt->{username};
166
167 $c->app->log->debug('Server-Logout: ' . $user);
168 return $c->render(json => { msg => [[0, 'Fine!']]});
169 };
170 };
171
172 return $c->render(status => 400, json => { error => [[0, 'No!']]});
173};
174
175
176# Request API token
177get '/auth/apiToken' => sub {
178 my $c = shift;
179
180 # Get auth header
181 my $auth = $c->req->headers->authorization;
182
183 # Authorization missing or not basic
184 if (!$auth || $auth !~ s/\s*Basic\s+//gi) {
185 return $c->render(
186 json => {
187 error => [[2, 'x']]
188 }
189 );
190 };
191
192 # Decode header
193 my ($username, $pwd) = @{b($auth)->b64_decode->split(':')->to_array};
194
195 # the password is 'pass'
196 if ($pwd) {
197
198 # the password is 'pass'
199 if ($pwd eq 'pass') {
200
201 # Render info with token
202 my $jwt = $c->jwt_encode(username => $username);
203
204 # Render in the Kustvakt fashion:
205 return $c->render(
206 format => 'html',
207 text => encode_json({
208 %{$jwt->claims},
209 expires => $jwt->expires,
210 token => $jwt->encode,
211 token_type => 'api_token'
212 })
213 );
214 };
215
216 return $c->render(
217 json => {
218 error => [[2004, undef]]
219 }
220 );
221 };
222
223 return $c->render(
224 json => {
225 error => [[2004, undef]]
226 }
227 );
228};
229
230app->start;
231
232
233__END__
234
235
236 # Temporary:
237 my $collection_query = {
238 '@type' => "koral:docGroup",
239 "operation" => "operation:or",
240 "operands" => [
241 {
242 '@type' => "koral:docGroup",
243 "operation" => "operation:and",
244 "operands" => [
245 {
246 '@type' => "koral:doc",
247 "key" => "title",
248 "match" => "match:eq",
249 "value" => "Der Birnbaum",
250 "type" => "type:string"
251 },
252 {
253 '@type' => "koral:doc",
254 "key" => "pubPlace",
255 "match" => "match:eq",
256 "value" => "Mannheim",
257 "type" => "type:string"
258 },
259 {
260 '@type' => "koral:docGroup",
261 "operation" => "operation:or",
262 "operands" => [
263 {
264 '@type' => "koral:doc",
265 "key" => "subTitle",
266 "match" => "match:eq",
267 "value" => "Aufzucht oder Pflege",
268 "type" => "type:string"
269 },
270 {
271 '@type' => "koral:doc",
272 "key" => "subTitle",
273 "match" => "match:eq",
274 "value" => "Gedichte",
275 "type" => "type:string"
276 }
277 ]
278 }
279 ]
280 },
281 {
282 '@type' => "koral:doc",
283 "key" => "pubDate",
284 "match" => "match:geq",
285 "value" => "2015-03-05",
286 "type" => "type:date"
287 }
288 ]
289 };