source: main/trunk/greenstone2/perllib/cpan/Mojo/UserAgent/Server.pm@ 32205

Last change on this file since 32205 was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 3.1 KB
Line 
1package Mojo::UserAgent::Server;
2use Mojo::Base -base;
3
4use Mojo::IOLoop;
5use Mojo::Server::Daemon;
6use Scalar::Util 'weaken';
7
8has ioloop => sub { Mojo::IOLoop->singleton };
9
10sub app {
11 my ($self, $app) = @_;
12
13 # Singleton application
14 state $singleton;
15 return $singleton = $app ? $app : $singleton unless ref $self;
16
17 # Default to singleton application
18 return $self->{app} || $singleton unless $app;
19 $self->{app} = $app;
20 return $self;
21}
22
23sub nb_url { shift->_url(1, @_) }
24
25sub restart { delete @{$_[0]}{qw(nb_port nb_server port server)} }
26
27sub url { shift->_url(0, @_) }
28
29sub _url {
30 my ($self, $nb, $proto) = @_;
31
32 if (!$self->{server} || $proto) {
33 $proto = $self->{proto} = $proto || 'http';
34
35 # Blocking
36 my $server = $self->{server}
37 = Mojo::Server::Daemon->new(ioloop => $self->ioloop, silent => 1);
38 weaken $server->app($self->app)->{app};
39 my $port = $self->{port} ? ":$self->{port}" : '';
40 $self->{port}
41 = $server->listen(["$proto://127.0.0.1$port"])->start->ports->[0];
42
43 # Non-blocking
44 $server = $self->{nb_server} = Mojo::Server::Daemon->new(silent => 1);
45 weaken $server->app($self->app)->{app};
46 $port = $self->{nb_port} ? ":$self->{nb_port}" : '';
47 $self->{nb_port}
48 = $server->listen(["$proto://127.0.0.1$port"])->start->ports->[0];
49 }
50
51 my $port = $nb ? $self->{nb_port} : $self->{port};
52 return Mojo::URL->new("$self->{proto}://127.0.0.1:$port/");
53}
54
551;
56
57=encoding utf8
58
59=head1 NAME
60
61Mojo::UserAgent::Server - Application server
62
63=head1 SYNOPSIS
64
65 use Mojo::UserAgent::Server;
66
67 my $server = Mojo::UserAgent::Server->new;
68 say $server->url;
69
70=head1 DESCRIPTION
71
72L<Mojo::UserAgent::Server> is an embedded web server based on
73L<Mojo::Server::Daemon> that processes requests for L<Mojo::UserAgent>.
74
75=head1 ATTRIBUTES
76
77L<Mojo::UserAgent::Server> implements the following attributes.
78
79=head2 ioloop
80
81 my $loop = $server->ioloop;
82 $server = $server->ioloop(Mojo::IOLoop->new);
83
84Event loop object to use for I/O operations, defaults to the global
85L<Mojo::IOLoop> singleton.
86
87=head1 METHODS
88
89L<Mojo::UserAgent::Server> inherits all methods from L<Mojo::Base> and
90implements the following new ones.
91
92=head2 app
93
94 my $app = Mojo::UserAgent::Server->app;
95 Mojo::UserAgent::Server->app(Mojolicious->new);
96 my $app = $server->app;
97 $server = $server->app(Mojolicious->new);
98
99Application this server handles, instance specific applications override the
100global default.
101
102 # Change application behavior
103 $server->app->defaults(testing => 'oh yea!');
104
105=head2 nb_url
106
107 my $url = $ua->nb_url;
108 my $url = $ua->nb_url('http');
109 my $url = $ua->nb_url('https');
110
111Get absolute L<Mojo::URL> object for server processing non-blocking requests
112with L</"app"> and switch protocol if necessary.
113
114=head2 restart
115
116 $server->restart;
117
118Restart server with new port.
119
120=head2 url
121
122 my $url = $ua->url;
123 my $url = $ua->url('http');
124 my $url = $ua->url('https');
125
126Get absolute L<Mojo::URL> object for server processing blocking requests with
127L</"app"> and switch protocol if necessary.
128
129=head1 SEE ALSO
130
131L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
132
133=cut
Note: See TracBrowser for help on using the repository browser.