]>
git.proxmox.com Git - pmg-api.git/blob - test-server.pl
7 use Socket
qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN);
13 use PVE
::Tools
qw(run_command);
15 use PVE
::APIServer
::Formatter
::Standard
;
16 use PVE
::APIServer
::Formatter
::HTML
;
20 my $nodename = PVE
::INotify
::nodename
();
23 my $cert_file = "pmg-api.pem";
25 if (! -f
$cert_file) {
26 print "generating demo server certificate\n";
27 my $cmd = ['openssl', 'req', '-batch', '-x509', '-newkey', 'rsa:4096',
28 '-nodes', '-keyout', $cert_file, '-out', $cert_file,
29 '-subj', "/CN=$nodename/",
34 my $socket = IO
::Socket
::IP-
>new(
35 LocalAddr
=> $nodename,
39 GetAddrInfoFlags
=> 0,
41 die "unable to create socket - $@\n";
43 # we often observe delays when using Nagle algorithm,
44 # so we disable that to maximize performance
45 setsockopt($socket, IPPROTO_TCP
, TCP_NODELAY
, 1);
47 my $accept_lock_fn = "simple-demo.lck";
48 my $lockfh = IO
::File-
>new(">>${accept_lock_fn}") ||
49 die "unable to open lock file '${accept_lock_fn}' - $!\n";
51 my $server = PMG
::HTTPServer-
>new(
54 lockfile
=> $accept_lock_fn,
56 title
=> 'Proxmox Mail Gateway API',
59 tls_ctx
=> { verify
=> 0, cert_file
=> $cert_file },
61 '/' => sub { get_index
($nodename, @_) },
65 # NOTE: Requests to non-API pages are not authenticated
66 # so you must be very careful here
68 my $root_page = <<__EOD__;
72 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
73 <meta http-equiv="X-UA-Compatible" content="IE=edge">
74 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
75 <title>Simple Demo Server</title>
78 <h1>Proxmox Mail Gateway API ($nodename)</h1>
80 You can browse the API <a href='/api2/html' >here</a>. Please sign
81 in with usrename <b>demo</b> and passwort <b>demo</b>.
88 my ($nodename, $server, $r, $args) = @_;
90 my $headers = HTTP
::Headers-
>new(Content_Type
=> "text/html; charset=utf-8");
91 my $resp = HTTP
::Response-
>new(200, "OK", $headers, $root_page);
95 print "demo server listens at: https://$nodename:$port/\n";