# phpman > perldoc > Net::Server::PSGI

## NAME
    [Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown) - basic [Net::Server](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer/markdown) based PSGI HTTP server class

## TEST ONE LINER
        perl -e 'use base qw([Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown)); main->run(port => 8080, ipv => "*")'
        # runs a default echo server

## SYNOPSIS
        use base qw([Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown));
        __PACKAGE__->run(app => \&my_echo_handler); # will bind IPv4 port 80

        sub my_echo_handler {
            my $env = shift;
            my $txt = qq{<form method="post" action="/bam"><input type="text" name="foo"><input type="submit"></form>\n};

            require [Data::Dumper](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ADumper/markdown);
            local $[Data::Dumper::Sortkeys](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ADumper%3A%3ASortkeys/markdown) = 1;

            require [CGI::PSGI](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3APSGI/markdown);
            my $form = {};
            my $q = [CGI::PSGI](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3APSGI/markdown)->new($env);
            $form->{$_} = $q->param($_) for $q->param;

            $txt .= "<pre>".[Data::Dumper](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ADumper/markdown)->Dump([$env, $form], ['env', 'form'])."</pre>";

            return [200, ['Content-type', 'text/html'], [$txt]];
        }

## DESCRIPTION
    If you want a more fully featured PSGI experience, it would be wise to look at the Plack and
    Starman set of modules. [Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown) is intended as an easy gateway into PSGI. But to get
    the most out of all that PSGI has to offer, you should review the Plack and [Plack::Middleware](https://www.chedong.com/phpMan.php/perldoc/Plack%3A%3AMiddleware/markdown).
    If you only need something a little more rudimentary, then [Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown) may be good for
    you.

    [Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown) takes [Net::Server::HTTP](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AHTTP/markdown) one level farther. It begins with base type MultiType
    defaulting to [Net::Server::Fork](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AFork/markdown). It is easy to change it to any of the other [Net::Server](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer/markdown) flavors
    by passing server_type => $other_flavor in the server configuration. The port has also been
    defaulted to port 80 - but could easily be changed to another through the server configuration.
    You can also very easily add ssl by including, proto=>"ssl" and provide a SSL_cert_file and
    SSL_key_file.

    For example, here is a basic server that will bind to all interfaces, will speak both HTTP on
    port 8080 as well as HTTPS on 8443, and will speak both IPv4, as well as IPv6 if it is
    available.

        use base qw([Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown));

        __PACKAGE__->run(
            port  => [8080, "8443/ssl"],
            ipv   => '*', # IPv6 if available
            SSL_key_file  => '/my/key',
            SSL_cert_file => '/my/cert',
        );

## METHODS
    "process_request"
        This method has been overridden in [Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown) - you should not use it while using
        [Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown). This overridden method parses the environment and sets up request alarms
        and handles dying failures. It calls process_psgi_request once the request is ready and
        headers have been parsed.

    "process_psgi_request"
        Used when psgi_enabled is true. During this method, find_psgi_handler will be called to
        return the appropriate psgi response handler. Once finished, print_psgi_headers and
        print_psgi_body are used to print out the response. See PSGI.

        Typically this method should not be overridden. Instead, an appropriate method for finding
        the app should be given to find_psgi_handler or app.

    "find_psgi_handler"
        Used to lookup the appropriate PSGI handler. A reference to the already parsed $env hashref
        is passed. PATH_INFO will be initialized to the full path portion of the URI. SCRIPT_NAME
        will be initialized to the empty string. This handler should set the appropriate values for
        SCRIPT_NAME and PATH_INFO depending upon the path matched. A code reference for the handler
        should be returned. The default find_psgi_handler will call the "app" method. If that fails
        a reference to the psgi_echo_handler is returned as the default application.

            sub find_psgi_handler {
                my ($self, $env) = @_;

                if ($env->{'PATH_INFO'} && $env->{'PATH_INFO'} =~ s{^ (/foo) (?= $ | /) }{}x) {
                    $env->{'SCRIPT_NAME'} = $1;
                    return \&foo_app;
                }

                return $self->[SUPER::find_psgi_handler](https://www.chedong.com/phpMan.php/perldoc/SUPER%3A%3Afindpsgihandler/markdown)($env);
            }

    "app"
        Return a reference to the application being served. This should be a valid PSGI application.
        See PSGI. By default it will look at the value of the "app" configuration option. The "app"
        method may also be used to set the "app" configuration option.

            package MyApp;
            use base qw([Net::Server::PSGI](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APSGI/markdown));

            sub default_server_type { 'Prefork' }

            sub my_app {
                my $env = shift;
                return [200, ['Content-type', 'text/html'], ["Hello world"]];
            }


            MyApp->run(app => \&my_app);


            # OR
            sub app { \&my_app }
            MyApp->run;


            # OR
            my $server = MyApp->new;
            $server->app(\&my_app);
            $server->run;

## OPTIONS
    In addition to the command line arguments of the [Net::Server::HTTP](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AHTTP/markdown) base classes you can also set
    the following options.

    app Should return a coderef of the PSGI application. Is returned by the app method.

## AUTHOR
    Paul T. Seamons <paul@seamons.com>

## SEE ALSO
    Please see also Plack, Starman,

    [Net::Server::Fork](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AFork/markdown), [Net::Server::INET](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AINET/markdown), [Net::Server::PreFork](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APreFork/markdown), [Net::Server::PreForkSimple](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3APreForkSimple/markdown),
    [Net::Server::MultiType](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AMultiType/markdown), [Net::Server::Single](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3ASingle/markdown) [Net::Server::SIG](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3ASIG/markdown) [Net::Server::Daemonize](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3ADaemonize/markdown)
    [Net::Server::Proto](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AProto/markdown) [Net::Server::HTTP](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AHTTP/markdown)

