-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.pl
executable file
·97 lines (88 loc) · 2.8 KB
/
api.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env perl
use Dir::Self;
use strict;
use warnings;
use Data::Dumper;
use Mojolicious::Lite;
use lib __DIR__ . "/lib";
use ILMT::Translator qw(get_translator get_langpairs);
use ILMT::HIN::PAN;
use ILMT::HIN::URD;
use ILMT::PAN::HIN;
use ILMT::URD::HIN;
use ILMT::TEL::HIN;
plugin qw(Mojolicious::Plugin::ForkCall);
any '/:src/:tgt/translate' => sub {
my $c = shift->render_later;
$c->inactivity_timeout(3600);
my %args = %{$c->req->params->to_hash};
$args{'src_lang'} = $c->param('src');
$args{'tgt_lang'} = $c->param('tgt');
$args{'data'} = $args{'input'} = $args{'data'} // $args{'input'};
$c->fork_call(
sub {
my (%args) = @_;
my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
return $translator->translate(%args);
},
[%args],
sub {
my ($c, $final_result) = @_;
if (exists $args{"pretty"}) {
my $final_string = join "\n", map { "$_:\n$final_result->{$_}" } keys %$final_result;
$c->render(template => 'pretty', result => $final_string);
} else {
$c->render(json => $final_result);
}
}
);
};
any '/:src/:tgt/:start/:end' => sub {
my $c = shift->render_later;
$c->inactivity_timeout(3600);
my %args = %{$c->req->params->to_hash};
$args{'src_lang'} = $c->param('src');
$args{'tgt_lang'} = $c->param('tgt');
$args{'data'} = $args{'input'} = $args{'data'} // $args{'input'};
$c->fork_call(
sub {
my (%args) = @_;
my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
return $translator->partial_p($c->param('start'), $c->param('end'), %args);
},
[%args],
sub {
my ($c, $final_result) = @_;
if (exists $args{"pretty"}) {
my $final_string = join "\n", map { "$_:\n$final_result->{$_}" } keys %$final_result;
$c->render(template => 'pretty', result => $final_string);
} else {
$c->render(json => $final_result);
}
}
);
};
any '/:src/:tgt/' => sub {
my $c = shift;
my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
$c->render(text => scalar @{$translator->{seq}});
};
any '/:src/:tgt/modules' => sub {
my $c = shift;
my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
my @modules = map { lc($_) } @{$translator->{seq}};
$c->render(json => \@modules);
};
any '/langpairs' => sub {
my $c = shift;
my %langpairs = get_langpairs();
$c->render(json => \%langpairs);
};
get '/' => sub {
my $c = shift;
$c->reply->static('index.html');
};
app->start;
__DATA__
@@ pretty.html.ep
<pre><%= $result %></pre>