llng-fastcgi-server 3.85 KB
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4 5 6 7
#!/usr/bin/env perl

use Plack::Runner;
use strict;
use warnings;
use POSIX;
use Getopt::Std;
Yadd's avatar
Yadd committed
8
use Lemonldap::NG::Handler::SharedConf;
Yadd's avatar
Yadd committed
9 10 11 12

our %opts;
my %_apps;

Yadd's avatar
Yadd committed
13
getopts( 'Fn:p:s:u:g:', \%opts );
Yadd's avatar
Yadd committed
14

Yadd's avatar
Yadd committed
15 16 17
$opts{n} ||= $ENV{NPROC}  || 7;
$opts{p} ||= $ENV{PID}    || '__FASTCGISOCKDIR__/llng-fastcgi.pid';
$opts{s} ||= $ENV{SOCKET} || '__FASTCGISOCKDIR__/llng-fastcgi.sock';
Yadd's avatar
Yadd committed
18 19 20 21 22
$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}";
Yadd's avatar
Yadd committed
23
    POSIX::setgid($grp);
Yadd's avatar
Yadd committed
24 25 26 27 28 29 30 31 32 33 34 35
}

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( {} );
    },
Yadd's avatar
Yadd committed
36 37 38 39
    status => sub {
        require Lemonldap::NG::Handler::Nginx;
        return Lemonldap::NG::Handler::Nginx->status();
    },
Yadd's avatar
Yadd committed
40 41 42 43
    manager => sub {
        require Lemonldap::NG::Manager;
        return Lemonldap::NG::Manager->run( {} );
    },
44
    cgi => sub {
Yadd's avatar
Yadd committed
45 46
        require CGI::Emulate::PSGI;
        require CGI::Compile;
47
        return sub {
48
            my $script = $_[0]->{SCRIPT_FILENAME};
49 50 51
            return $_apps{$script}->(@_) if ( $_apps{$script} );
            $_apps{$script} =
              CGI::Emulate::PSGI->handler( CGI::Compile->compile($script) );
52
            return $_apps{$script}->(@_);
53
        };
Yadd's avatar
Yadd committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    },
);

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";
};

Yadd's avatar
Yadd committed
71 72
Lemonldap::NG::Handler::SharedConf->init();

Yadd's avatar
Yadd committed
73 74 75 76 77 78
my $server = Plack::Runner->new();
$server->parse_options(
    '-s'           => 'FCGI',
    '-E'           => 'deployment',
    '--pid'        => $opts{p},
    '--nproc'      => $opts{n},
Yadd's avatar
Yadd committed
79
    '--socket'     => $opts{s},
Yadd's avatar
Yadd committed
80
    '--proc-title' => 'llng-fastcgi-server',
Yadd's avatar
Yadd committed
81
    ( $opts{F} ? () : '--daemonize' ),
Yadd's avatar
Yadd committed
82 83 84 85
    '--no-default-middleware',
);

$server->run($app);
Yadd's avatar
Yadd committed
86
__END__
Yadd's avatar
Yadd committed
87

Yadd's avatar
Yadd committed
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 148 149 150 151 152 153 154
=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