Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use Mojolicious::Lite; |
| 3 | use Mojo::ByteStream 'b'; |
| 4 | use Mojo::Date; |
| 5 | use Mojo::JSON qw/true false encode_json decode_json/; |
| 6 | use strict; |
| 7 | use warnings; |
| 8 | use Mojo::JWT; |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 9 | use Mojo::File qw/path/; |
| 10 | use Mojo::Util qw/slugify/; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 11 | |
| 12 | # This is an API fake server with fixtures |
| 13 | |
| 14 | my $secret = 's3cr3t'; |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 15 | my $fixture_path = path(Mojo::File->new(__FILE__)->dirname); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 16 | |
| 17 | helper 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 | |
| 27 | helper 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 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 34 | # Load fixture responses |
| 35 | helper 'load_response' => sub { |
| 36 | my $c = shift; |
| 37 | my $q_name = shift; |
| 38 | my $file = $fixture_path->child("response_$q_name.json"); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 39 | $c->app->log->debug("Load response from $file"); |
| 40 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 41 | unless (-f $file) { |
| 42 | return { |
| 43 | status => 500, |
| 44 | json => { |
| 45 | errors => [[0, 'Unable to load query response from ' . $file]] |
| 46 | } |
| 47 | } |
| 48 | }; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 49 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 50 | my $response = $file->slurp; |
| 51 | return decode_json($response); |
| 52 | }; |
| 53 | |
| 54 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 55 | # Base page |
| 56 | get '/' => sub { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 57 | shift->render(text => 'Fake server available'); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 60 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 61 | # Search fixtures |
| 62 | get '/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'); |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 70 | $v->optional('offset'); |
| 71 | $v->optional('cutoff')->in(qw/true false/); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 72 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 73 | $c->app->log->debug('Receive request'); |
| 74 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 75 | # 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 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 85 | if (!$v->param('q')) { |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 86 | return $c->render(%{$c->load_response('query_no_query')}); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 89 | 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 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 94 | # Get response based on query parameter |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 95 | my $response = $c->load_response('query_' . slugify(join('_', @slug_base))); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 96 | |
| 97 | # Check authentification |
| 98 | if (my $auth = $c->req->headers->header('Authorization')) { |
| 99 | if (my $jwt = $c->jwt_decode($auth)) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 100 | $response->{json}->{meta}->{authorized} = $jwt->{username} if $jwt->{username}; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 101 | }; |
| 102 | }; |
| 103 | |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 104 | # Set page parameter |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 105 | if ($v->param('page')) { |
Akron | 6d49c1f | 2018-10-11 14:22:21 +0200 | [diff] [blame] | 106 | $response->{json}->{meta}->{startIndex} = $v->param("startIndex"); |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 109 | # Simple search fixture |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 110 | $c->render(%$response); |
| 111 | |
| 112 | $c->app->log->debug('Rendered result'); |
| 113 | |
| 114 | return 1; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
Akron | 80a84b2 | 2018-10-24 17:44:24 +0200 | [diff] [blame^] | 117 | # Textinfo fixtures |
| 118 | get '/corpus/:corpusId/:docId/:textId' => sub { |
| 119 | my $c = shift; |
| 120 | |
| 121 | my $file = join('_', ( |
| 122 | 'textinfo', |
| 123 | $c->stash('corpusId'), |
| 124 | $c->stash('docId'), |
| 125 | $c->stash('textId') |
| 126 | )); |
| 127 | |
| 128 | my $slug = slugify($file); |
| 129 | |
| 130 | # Get response based on query parameter |
| 131 | my $response = $c->load_response($slug); |
| 132 | return $c->render(%$response); |
| 133 | }; |
| 134 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 135 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 136 | # Matchinfo fixtures |
| 137 | get '/corpus/:corpusId/:docId/:textId/:matchId/matchInfo' => sub { |
| 138 | my $c = shift; |
| 139 | |
| 140 | my $file = join('_', ( |
| 141 | 'matchinfo', |
| 142 | $c->stash('corpusId'), |
| 143 | $c->stash('docId'), |
| 144 | $c->stash('textId'), |
| 145 | $c->stash('matchId') |
| 146 | )); |
| 147 | |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 148 | my $slug = slugify($file); |
| 149 | |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 150 | # Get response based on query parameter |
Akron | b8d0b40 | 2018-10-18 23:51:52 +0200 | [diff] [blame] | 151 | my $response = $c->load_response($slug); |
Akron | b80341d | 2018-10-15 19:46:23 +0200 | [diff] [blame] | 152 | return $c->render(%$response); |
| 153 | }; |
| 154 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 155 | |
Akron | be61f4c | 2018-10-20 00:52:58 +0200 | [diff] [blame] | 156 | # Statistics endpoint |
| 157 | get '/statistics' => sub { |
| 158 | my $c = shift; |
| 159 | my $v = $c->validation; |
| 160 | $v->optional('corpusQuery'); |
| 161 | |
| 162 | my @list = 'corpusinfo'; |
| 163 | if ($v->param('corpusQuery')) { |
| 164 | push @list, $v->param('corpusQuery'); |
| 165 | }; |
| 166 | my $slug = slugify(join('_', @list)); |
| 167 | |
| 168 | # Get response based on query parameter |
| 169 | my $response = $c->load_response($slug); |
| 170 | return $c->render(%$response); |
| 171 | }; |
| 172 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 173 | ############ |
| 174 | # Auth API # |
| 175 | ############ |
| 176 | |
| 177 | # Request API token |
| 178 | get '/auth/logout' => sub { |
| 179 | my $c = shift; |
| 180 | |
| 181 | if (my $auth = $c->req->headers->header('Authorization')) { |
| 182 | if (my $jwt = $c->jwt_decode($auth)) { |
| 183 | my $user = $jwt->{username} if $jwt->{username}; |
| 184 | |
| 185 | $c->app->log->debug('Server-Logout: ' . $user); |
| 186 | return $c->render(json => { msg => [[0, 'Fine!']]}); |
| 187 | }; |
| 188 | }; |
| 189 | |
| 190 | return $c->render(status => 400, json => { error => [[0, 'No!']]}); |
| 191 | }; |
| 192 | |
| 193 | |
| 194 | # Request API token |
| 195 | get '/auth/apiToken' => sub { |
| 196 | my $c = shift; |
| 197 | |
| 198 | # Get auth header |
| 199 | my $auth = $c->req->headers->authorization; |
| 200 | |
| 201 | # Authorization missing or not basic |
| 202 | if (!$auth || $auth !~ s/\s*Basic\s+//gi) { |
| 203 | return $c->render( |
| 204 | json => { |
| 205 | error => [[2, 'x']] |
| 206 | } |
| 207 | ); |
| 208 | }; |
| 209 | |
| 210 | # Decode header |
| 211 | my ($username, $pwd) = @{b($auth)->b64_decode->split(':')->to_array}; |
| 212 | |
| 213 | # the password is 'pass' |
| 214 | if ($pwd) { |
| 215 | |
| 216 | # the password is 'pass' |
| 217 | if ($pwd eq 'pass') { |
| 218 | |
| 219 | # Render info with token |
| 220 | my $jwt = $c->jwt_encode(username => $username); |
| 221 | |
| 222 | # Render in the Kustvakt fashion: |
| 223 | return $c->render( |
| 224 | format => 'html', |
| 225 | text => encode_json({ |
| 226 | %{$jwt->claims}, |
| 227 | expires => $jwt->expires, |
| 228 | token => $jwt->encode, |
| 229 | token_type => 'api_token' |
| 230 | }) |
| 231 | ); |
| 232 | }; |
| 233 | |
| 234 | return $c->render( |
| 235 | json => { |
| 236 | error => [[2004, undef]] |
| 237 | } |
| 238 | ); |
| 239 | }; |
| 240 | |
| 241 | return $c->render( |
| 242 | json => { |
| 243 | error => [[2004, undef]] |
| 244 | } |
| 245 | ); |
| 246 | }; |
| 247 | |
| 248 | app->start; |
| 249 | |
| 250 | |
| 251 | __END__ |
| 252 | |
| 253 | |
| 254 | # Temporary: |
| 255 | my $collection_query = { |
| 256 | '@type' => "koral:docGroup", |
| 257 | "operation" => "operation:or", |
| 258 | "operands" => [ |
| 259 | { |
| 260 | '@type' => "koral:docGroup", |
| 261 | "operation" => "operation:and", |
| 262 | "operands" => [ |
| 263 | { |
| 264 | '@type' => "koral:doc", |
| 265 | "key" => "title", |
| 266 | "match" => "match:eq", |
| 267 | "value" => "Der Birnbaum", |
| 268 | "type" => "type:string" |
| 269 | }, |
| 270 | { |
| 271 | '@type' => "koral:doc", |
| 272 | "key" => "pubPlace", |
| 273 | "match" => "match:eq", |
| 274 | "value" => "Mannheim", |
| 275 | "type" => "type:string" |
| 276 | }, |
| 277 | { |
| 278 | '@type' => "koral:docGroup", |
| 279 | "operation" => "operation:or", |
| 280 | "operands" => [ |
| 281 | { |
| 282 | '@type' => "koral:doc", |
| 283 | "key" => "subTitle", |
| 284 | "match" => "match:eq", |
| 285 | "value" => "Aufzucht oder Pflege", |
| 286 | "type" => "type:string" |
| 287 | }, |
| 288 | { |
| 289 | '@type' => "koral:doc", |
| 290 | "key" => "subTitle", |
| 291 | "match" => "match:eq", |
| 292 | "value" => "Gedichte", |
| 293 | "type" => "type:string" |
| 294 | } |
| 295 | ] |
| 296 | } |
| 297 | ] |
| 298 | }, |
| 299 | { |
| 300 | '@type' => "koral:doc", |
| 301 | "key" => "pubDate", |
| 302 | "match" => "match:geq", |
| 303 | "value" => "2015-03-05", |
| 304 | "type" => "type:date" |
| 305 | } |
| 306 | ] |
| 307 | }; |