llng-fastcgi-server 3.61 KB
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#!/usr/bin/env perl

use Plack::Runner;
use strict;
use warnings;
use POSIX;
use Getopt::Std;

our %opts;
my %_apps;

getopts( 'n:p:s:u:g:', \%opts );

$opts{n} ||= $ENV{NPROC}       || 7;
$opts{p} ||= $ENV{PID}         || '/run/llng-fastcgi.pid';
$opts{s} ||= $ENV{SOCKET}      || '/run/llng-fastcgi.sock';
$opts{u} ||= $ENV{USER};
$opts{g} ||= $ENV{GROUP};

if ( $opts{g} ) {
    my $grp = getgrnam( $opts{g} ) or warn "Can't change uid to $opts{g}";
    POSIX::setuid($grp);
}

if ( $opts{u} ) {
    my $uid = getpwnam( $opts{u} ) or warn "Can't change uid to $opts{u}";
    POSIX::setuid($uid);
}

my %builder = (
    handler => sub {
        require Lemonldap::NG::Handler::Nginx;
        return Lemonldap::NG::Handler::Nginx->run( {} );
    },
    manager => sub {
        require Lemonldap::NG::Manager;
        return Lemonldap::NG::Manager->run( {} );
    },
    auth => sub {
        require CGI::Emulate::PSGI;
        require CGI::Compile;
42 43 44 45 46 47 48
        return sub {
            my $script = $_[0]->{SCRIPTNAME};
            return $_apps{$script}->(@_) if ( $_apps{$script} );
            $_apps{$script} =
              CGI::Emulate::PSGI->handler( CGI::Compile->compile($script) );
            return $_app{$script}->(@_);
        };
Yadd's avatar
Yadd committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    },
);

unless ($>) {
    die "Refuse to run as root. Aborting";
}

my $app = sub {
    my $type = $_[0]->{LLTYPE} || 'handler';
    return $_apps{$type}->(@_) if ( defined $_apps{$type} );
    if ( defined $builder{$type} ) {
        $_apps{$type} = $builder{$type}->();
        return $_apps{$type}->(@_);
    }
    die "Unknown PSGI type $type";
};

my $server = Plack::Runner->new();
$server->parse_options(
    '-s'           => 'FCGI',
    '-E'           => 'deployment',
    '--pid'        => $opts{p},
    '--nproc'      => $opts{n},
    '--listen'     => [ $opts{s} ],
    '--proc-title' => 'llng-fastcgi-server',
    '--daemonize',
    '--no-default-middleware',
);

$server->run($app);
Yadd's avatar
Yadd committed
79
__END__
Yadd's avatar
Yadd committed
80

Yadd's avatar
Yadd committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
=head1 NAME

=encoding utf8

llng-fastcgi-server - FastCGI server used to provide Lemonldap::NG services to
Nginx

=head1 SYNOPSIS

  # Start server listening to /run/llng.sock with 10 process
  llng-fastcgi-server -u nobody -g nobody -s /run/llng.sock -n 10

=head1 DESCRIPTION

llng-fastcgi-server has been designed provides Lemonldap::NG services to Nginx.
Portal, manager and handler will be compiled only is used. So this FastCGI
server can be used on every Lemonldap::NG server even if it needs only some
parts (isolated handlers, portal,...).

=head1 SEE ALSO

L<http://lemonldap-ng.org/>

=head1 AUTHORS

=over

=item Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>

=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=back

=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
L<http://jira.ow2.org>

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>

=head1 COPYRIGHT AND LICENSE

=over

=item Copyright (C) 2008-2016 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=item Copyright (C) 2008-2016 by Clément Oudot, E<lt>clem.oudot@gmail.comE<gt>

=back

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<http://www.gnu.org/licenses/>.

=cut