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

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

our %opts;
my %_apps;

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

Yadd's avatar
Yadd committed
14 15 16
$SIG{'PIPE'} = 'IGNORE';

$opts{e} ||= $ENV{ENGINE} || 'FCGI';
Yadd's avatar
Yadd committed
17 18 19
$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
20 21
$opts{u} ||= $ENV{USER};
$opts{g} ||= $ENV{GROUP};
22
$opts{f} ||= $ENV{CUSTOM_FUNCTIONS_FILE};
Yadd's avatar
Yadd committed
23 24 25

if ( $opts{g} ) {
    my $grp = getgrnam( $opts{g} ) or warn "Can't change uid to $opts{g}";
Yadd's avatar
Yadd committed
26
    POSIX::setgid($grp);
Yadd's avatar
Yadd committed
27 28 29 30 31 32 33
}

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

Yadd's avatar
Yadd committed
34 35 36
if ( $opts{f} ) {
    eval { require $opts{f} };
    die $@ if ($@);
37 38
}

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

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

83
# Hook for customFunctions initialization
Yadd's avatar
Yadd committed
84 85
use Lemonldap::NG::Handler::PSGI::API;
Lemonldap::NG::Handler::PSGI::API->init();
Yadd's avatar
Yadd committed
86

Yadd's avatar
Yadd committed
87 88
my $server = Plack::Runner->new();
$server->parse_options(
Yadd's avatar
Yadd committed
89
    '-s'           => $opts{e},
Yadd's avatar
Yadd committed
90 91 92
    '-E'           => 'deployment',
    '--pid'        => $opts{p},
    '--nproc'      => $opts{n},
Yadd's avatar
Yadd committed
93
    '--socket'     => $opts{s},
Yadd's avatar
Yadd committed
94
    '--proc-title' => 'llng-fastcgi-server',
Yadd's avatar
Yadd committed
95
    ( $opts{F} ? () : '--daemonize' ),
Yadd's avatar
Yadd committed
96 97 98 99
    '--no-default-middleware',
);

$server->run($app);
Yadd's avatar
Yadd committed
100
__END__
Yadd's avatar
Yadd committed
101

Yadd's avatar
Yadd committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
=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,...).

Yadd's avatar
Yadd committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
=head1 PARAMETERS

Each parameter can be set by an option or a environment variable.

=over

=item -p ($ENV{PID}):                   pid file

=item -u ($ENV{USER}):                  user

=item -g ($ENV{GROUP}):                 group

=item -n ($ENV{NPROC}):                 Number of processus for FCGI

=item -e ($ENV{ENGINE}):                Plack::Handler engine, default to FCGI

=item -s ($ENV{SOCKET}):                Unix socket

=item -f ($ENV{CUSTOM_FUNCTIONS_FILE}): file to load for custom functions

=back

Yadd's avatar
Yadd committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
=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