]> git.proxmox.com Git - pmg-api.git/blame - test-server.pl
cleanup and install SMTP server class
[pmg-api.git] / test-server.pl
CommitLineData
1360e6f0
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use lib '.';
6
7use Socket qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN);
8use IO::Socket::IP;
9use HTTP::Headers;
10use HTTP::Response;
11use Data::Dumper;
12
1360e6f0
DM
13use PVE::INotify;
14use PVE::APIServer::Formatter::Standard;
15use PVE::APIServer::Formatter::HTML;
16
17use PMG::HTTPServer;
9d7f54a3 18use PMG::Ticket;
1360e6f0
DM
19
20my $nodename = PVE::INotify::nodename();
21my $port = 9999;
9d7f54a3 22my $cert_file = PMG::Ticket::generate_api_cert($nodename);
d883fac2 23PMG::Ticket::generate_csrf_key();
c151b711 24PMG::Ticket::generate_auth_key();
1360e6f0
DM
25
26my $socket = IO::Socket::IP->new(
27 LocalAddr => $nodename,
28 LocalPort => $port,
29 Listen => SOMAXCONN,
30 Proto => 'tcp',
31 GetAddrInfoFlags => 0,
32 ReuseAddr => 1) ||
33 die "unable to create socket - $@\n";
34
35# we often observe delays when using Nagle algorithm,
36# so we disable that to maximize performance
37setsockopt($socket, IPPROTO_TCP, TCP_NODELAY, 1);
38
39my $accept_lock_fn = "simple-demo.lck";
40my $lockfh = IO::File->new(">>${accept_lock_fn}") ||
41 die "unable to open lock file '${accept_lock_fn}' - $!\n";
42
43my $server = PMG::HTTPServer->new(
44 debug => 1,
45 socket => $socket,
46 lockfile => $accept_lock_fn,
47 lockfh => $lockfh,
48 title => 'Proxmox Mail Gateway API',
49 cookie_name => 'PMG',
50 logfh => \*STDOUT,
51 tls_ctx => { verify => 0, cert_file => $cert_file },
52 pages => {
53 '/' => sub { get_index($nodename, @_) },
54 },
55);
56
57# NOTE: Requests to non-API pages are not authenticated
58# so you must be very careful here
59
60my $root_page = <<__EOD__;
61<!DOCTYPE html>
62<html>
63 <head>
64 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
65 <meta http-equiv="X-UA-Compatible" content="IE=edge">
66 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
67 <title>Simple Demo Server</title>
68 </head>
69 <body>
70 <h1>Proxmox Mail Gateway API ($nodename)</h1>
71
72 You can browse the API <a href='/api2/html' >here</a>. Please sign
73 in with usrename <b>demo</b> and passwort <b>demo</b>.
74
75 </body>
76</html>
77__EOD__
78
79sub get_index {
80 my ($nodename, $server, $r, $args) = @_;
81
82 my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
83 my $resp = HTTP::Response->new(200, "OK", $headers, $root_page);
84
85}
86
87print "demo server listens at: https://$nodename:$port/\n";
88
89$server->run();