line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/local/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}' |
4
|
|
|
|
|
|
|
if 0; # not running under some shell |
5
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
6
|
|
|
|
|
|
|
# A Mojo Web App as a NNexus entry point |
7
|
|
|
|
|
|
|
# Deyan Ginev, |
8
|
|
|
|
|
|
|
# GPLv3 code |
9
|
|
|
|
|
|
|
# ---------------------------------------------------------------- |
10
|
1
|
|
|
1
|
|
530
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
41
|
|
11
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
31
|
|
12
|
1
|
|
|
1
|
|
4
|
use Data::Dumper; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
56
|
|
13
|
1
|
|
|
1
|
|
6
|
use File::Basename; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
75
|
|
14
|
|
|
|
|
|
|
my $FILE_BASE; |
15
|
|
|
|
|
|
|
BEGIN { |
16
|
1
|
|
|
1
|
|
84
|
$FILE_BASE = dirname(__FILE__); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
if (-e $FILE_BASE."/../lib") { |
19
|
1
|
|
|
1
|
|
445
|
use lib $FILE_BASE."/../lib"; |
|
1
|
|
|
|
|
777
|
|
|
1
|
|
|
|
|
6
|
|
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
|
748
|
use Mojolicious::Lite; |
|
1
|
|
|
|
|
1343
|
|
|
1
|
|
|
|
|
6
|
|
23
|
1
|
|
|
1
|
|
15927
|
use Mojo::JSON; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
24
|
1
|
|
|
1
|
|
4
|
use Mojo::IOLoop; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
8
|
|
25
|
1
|
|
|
1
|
|
20
|
use Mojo::ByteStream qw(b); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
43
|
|
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
1
|
|
5
|
use Encode; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
59
|
|
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
1
|
|
440
|
use NNexus::Job; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use NNexus::DB; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our $modern_mojo = ($Mojolicious::VERSION >= 4); |
33
|
|
|
|
|
|
|
# Configuration is server-level |
34
|
|
|
|
|
|
|
my $opts; |
35
|
|
|
|
|
|
|
if ($ARGV[0] && -e $ARGV[0]) { |
36
|
|
|
|
|
|
|
print STDERR "\nBase Configuration to be read from: $ARGV[0]\n"; |
37
|
|
|
|
|
|
|
$opts = read_json_file($ARGV[0]); |
38
|
|
|
|
|
|
|
} else { |
39
|
|
|
|
|
|
|
my ($INSTALLDIR) = grep(-d $_, map("$_/NNexus", @INC)); |
40
|
|
|
|
|
|
|
die "NNexus installation directory is not in Perl's \@INC!\n" unless $INSTALLDIR; |
41
|
|
|
|
|
|
|
$opts = { |
42
|
|
|
|
|
|
|
"dbms" => "SQLite", |
43
|
|
|
|
|
|
|
"dbname" => "$INSTALLDIR/resources/database/snapshot.db", |
44
|
|
|
|
|
|
|
"dbuser" => "nnexus", |
45
|
|
|
|
|
|
|
"dbpass" => "nnexus", |
46
|
|
|
|
|
|
|
"dbhost" => "localhost", |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
our $db = NNexus::DB->new(%$opts); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$ENV{MOJO_HOME} = '.' unless defined $ENV{MOJO_HOME}; |
52
|
|
|
|
|
|
|
$ENV{MOJO_MAX_MESSAGE_SIZE} = 10485760; # 10 MB file upload limit |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Make signed cookies secure |
55
|
|
|
|
|
|
|
app->secrets(['NNexus auto-linking for the win!']); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
helper prepare_job => sub { |
58
|
|
|
|
|
|
|
my ($self,%override) = @_; |
59
|
|
|
|
|
|
|
my $get_params = $self->req->url->query->params || []; |
60
|
|
|
|
|
|
|
my $post_params = $self->req->body_params->params || []; |
61
|
|
|
|
|
|
|
if (scalar(@$post_params) == 1) { |
62
|
|
|
|
|
|
|
$post_params = ['body' , $post_params->[0]]; |
63
|
|
|
|
|
|
|
} elsif (scalar(@$post_params) == 2 && ($post_params->[0] ne 'body')) { |
64
|
|
|
|
|
|
|
$post_params = ['body' , $post_params->[0].$post_params->[1]]; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
my $parameters = { @$get_params, @$post_params }; |
67
|
|
|
|
|
|
|
# Currently , we only support : |
68
|
|
|
|
|
|
|
$parameters->{format} //= 'html'; |
69
|
|
|
|
|
|
|
$parameters->{embed} //= 1; |
70
|
|
|
|
|
|
|
$parameters->{function} //= 'linkentry'; |
71
|
|
|
|
|
|
|
$parameters->{domain} //= 'all'; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
foreach my $key(keys %override) { |
74
|
|
|
|
|
|
|
$parameters->{$key} = $override{$key}; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
$parameters->{'db'} = $db; |
77
|
|
|
|
|
|
|
return NNexus::Job->new(%$parameters); |
78
|
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
post '/' => sub { |
81
|
|
|
|
|
|
|
my $job = $_[0]->prepare_job; |
82
|
|
|
|
|
|
|
$job->execute; |
83
|
|
|
|
|
|
|
$_[0]->render(json=>$job->response); |
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Shortcut paths: |
87
|
|
|
|
|
|
|
post '/autolink' => sub { |
88
|
|
|
|
|
|
|
my $job = $_[0]->prepare_job(function=>'linkentry'); |
89
|
|
|
|
|
|
|
$job->execute; |
90
|
|
|
|
|
|
|
$_[0]->res->headers->header('Access-Control-Allow-Origin' => '*'); |
91
|
|
|
|
|
|
|
$modern_mojo ? |
92
|
|
|
|
|
|
|
$_[0]->render(json=>$job->response) : |
93
|
|
|
|
|
|
|
$_[0]->render_json($job->response,status=>200); |
94
|
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
post '/linkentry' => sub { |
96
|
|
|
|
|
|
|
my $job = $_[0]->prepare_job(function=>'linkentry'); |
97
|
|
|
|
|
|
|
$job->execute; |
98
|
|
|
|
|
|
|
$_[0]->res->headers->header('Access-Control-Allow-Origin' => '*'); |
99
|
|
|
|
|
|
|
$modern_mojo ? |
100
|
|
|
|
|
|
|
$_[0]->render(json=>$job->response) : |
101
|
|
|
|
|
|
|
$_[0]->render_json($job->response,status=>200); |
102
|
|
|
|
|
|
|
}; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
post '/indexentry' => sub { |
105
|
|
|
|
|
|
|
my $job = $_[0]->prepare_job(function=>'indexentry'); |
106
|
|
|
|
|
|
|
$job->execute; |
107
|
|
|
|
|
|
|
$_[0]->res->headers->header('Access-Control-Allow-Origin' => '*'); |
108
|
|
|
|
|
|
|
$modern_mojo ? |
109
|
|
|
|
|
|
|
$_[0]->render(json=>$job->response) : |
110
|
|
|
|
|
|
|
$_[0]->render_json($job->response,status=>200); |
111
|
|
|
|
|
|
|
}; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
app->start; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |