llng-fastcgi-server 4 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;

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

Yadd's avatar
Yadd committed
14 15 16
$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
17 18
$opts{u} ||= $ENV{USER};
$opts{g} ||= $ENV{GROUP};
19
$opts{f} ||= $ENV{CUSTOM_FUNCTIONS_FILE};
Yadd's avatar
Yadd committed
20 21 22

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
}

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

31 32 33 34 35
if($opts{f}) {
    eval {require $opts{f}};
    die $@ if($@);
}

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

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

76
# Hook for customFunctions initialization
Yadd's avatar
Yadd committed
77 78
use Lemonldap::NG::Handler::PSGI::API;
Lemonldap::NG::Handler::PSGI::API->init();
Yadd's avatar
Yadd committed
79

Yadd's avatar
Yadd committed
80 81 82 83 84 85
my $server = Plack::Runner->new();
$server->parse_options(
    '-s'           => 'FCGI',
    '-E'           => 'deployment',
    '--pid'        => $opts{p},
    '--nproc'      => $opts{n},
Yadd's avatar
Yadd committed
86
    '--socket'     => $opts{s},
Yadd's avatar
Yadd committed
87
    '--proc-title' => 'llng-fastcgi-server',
Yadd's avatar
Yadd committed
88
    ( $opts{F} ? () : '--daemonize' ),
Yadd's avatar
Yadd committed
89 90 91 92
    '--no-default-middleware',
);

$server->run($app);
Yadd's avatar
Yadd committed
93
__END__
Yadd's avatar
Yadd committed
94

Yadd's avatar
Yadd committed
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 155 156 157 158 159 160 161
=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