<commit_before>package SGN::Apache2::Startup;

use strict;
use warnings;

use English;
use Carp;
use FindBin;
use File::Spec::Functions;
use File::Basename;
use File::Path;
use Path::Class;

use autodie qw/:all/;

###############

# reuse database connections rather than getting new ones every
# time a new query is made
# THIS MUST BE LOADED BEFORE DBI OR ANY OTHER MODULE THAT USES DBI
# otherwise, you get ticket #184, where the number of open databaseconnections
# you have inexplicably explodes
use Apache::DBI;

#use CXGN::Apache::Registry for request dispatch to our cgi-style scripts
use CXGN::Apache::Registry;

################

use Apache2::ServerUtil ();
use Apache2::ServerRec ();

sub import {
    my $class = shift;
    my %args = @_;

    require SGN::Context;
    my $vhost = SGN::Context->new;

    exists $args{vhost}
        or die "FATAL: vhost argument required for $class, e.g. 'use $class vhost => 1;'\n";

    # preload a large number of modules at server startup
    unless( exists $args{module_preload} && ! $args{module_preload} ) {
	require SGN::Apache2::Startup::Preload;
    }

    my %paths;

    # find the sgn/ basepath
    $paths{basepath} = catdir( $vhost->path_to() );

    # path to the root of the shipwright vessel, if we are in one
    $paths{shipwright_vessel}   = catdir($paths{basepath},updir());

    # resolve any .. or . in %paths
    $_ = Cwd::realpath($_) for values %paths;

    # flag, true if we are running under a shipwright vessel
    my $running_under_shipwright = -f catfile( $paths{shipwright_vessel}, 'etc', 'shipwright-perl-wrapper' );

    if( $running_under_shipwright ) {
        # use the shipwright lib dirs if under shipwright
        unshift @INC,
            catdir( $paths{shipwright_vessel}, 'lib', 'perl5', 'site_perl' ),
            catdir( $paths{shipwright_vessel}, 'lib', 'perl5' );
    }

    ##### now that we have set up the proper paths for perl modules,
    ##### go on with the rest of our server configuration

    if( $running_under_shipwright ) {
        my $v = $paths{shipwright_vessel};
        $ENV{PATH} = "$v/sbin:$v/bin:$v/usr/sbin:$v/usr/bin:$ENV{PATH}";
        $ENV{PROJECT_NAME} = 'SGN';
    }

    # add some other configuration to the web server
    my $root_server = my $server =  Apache2::ServerUtil->server;
    $server = $server->next if $args{mod_perl_vhost};
    $server->add_config( $_ ) for generate_apache_config( \%paths, $vhost );

    # make and chown the tempfiles dir.
    # note that this mkdir and chown is going to be running as root.

    umask 000002; #< all files written by web server will be group-writable by default

    # the tempfiles_subdir() function makes and chmods the given
    # directory.  with no arguments, will make and chmod the main
    # tempfiles directory
    my $temp_subdir = $vhost->path_to( $vhost->tempfiles_subdir() );
    $vhost->chown_generated_dir( $temp_subdir ); #< force-set the
                                                 # permissions on the
                                                 # main tempfiles dir

    # also chown any subdirs that are in the temp dir.
    # this line should be removed eventually, the application itself should take
    # care of creating temp dirs if it wants.
    $vhost->chown_generated_dir( $_ ) for glob "$temp_subdir/*/";
}

sub generate_apache_config {
    my $paths = shift;
    my $c = shift;
    my $cfg = $c->config;

    my $rewrite_logfile = check_logfile($c, $cfg->{rewrite_log} );

    return map {[split /\n/, $_]}

            'PerlSetEnv PROJECT_NAME SGN',

            # don't output a server signature, which gives potential intruders
            # a bit too much information
            'ServerSignature Off',

            # set an environmental variable which will allow modules which are
            # used by multiple virtual hosts (for instance, CXGN::Page) to
            # find out which virtual host they are running on

            'PerlSetEnv PROJECT_NAME SGN',<commit_msg>call setup() on site feature objects at server restart<commit_after>package SGN::Apache2::Startup;

use strict;
use warnings;

use English;
use Carp;
use FindBin;
use File::Spec::Functions;
use File::Basename;
use File::Path;
use Path::Class;

use autodie qw/:all/;

###############

# reuse database connections rather than getting new ones every
# time a new query is made
# THIS MUST BE LOADED BEFORE DBI OR ANY OTHER MODULE THAT USES DBI
# otherwise, you get ticket #184, where the number of open databaseconnections
# you have inexplicably explodes
use Apache::DBI;

#use CXGN::Apache::Registry for request dispatch to our cgi-style scripts
use CXGN::Apache::Registry;

################

use Apache2::ServerUtil ();
use Apache2::ServerRec ();

sub import {
    my $class = shift;
    my %args = @_;

    require SGN::Context;
    my $c = SGN::Context->new;

    exists $args{vhost}
        or die "FATAL: vhost argument required for $class, e.g. 'use $class vhost => 1;'\n";

    # preload a large number of modules at server startup
    unless( exists $args{module_preload} && ! $args{module_preload} ) {
	require SGN::Apache2::Startup::Preload;
    }

    my %paths;

    # find the sgn/ basepath
    $paths{basepath} = catdir( $c->path_to() );

    # path to the root of the shipwright vessel, if we are in one
    $paths{shipwright_vessel}   = catdir($paths{basepath},updir());

    # resolve any .. or . in %paths
    $_ = Cwd::realpath($_) for values %paths;

    # flag, true if we are running under a shipwright vessel
    my $running_under_shipwright = -f catfile( $paths{shipwright_vessel}, 'etc', 'shipwright-perl-wrapper' );

    if( $running_under_shipwright ) {
        # use the shipwright lib dirs if under shipwright
        unshift @INC,
            catdir( $paths{shipwright_vessel}, 'lib', 'perl5', 'site_perl' ),
            catdir( $paths{shipwright_vessel}, 'lib', 'perl5' );
    }

    ##### now that we have set up the proper paths for perl modules,
    ##### go on with the rest of our server configuration

    if( $running_under_shipwright ) {
        my $v = $paths{shipwright_vessel};
        $ENV{PATH} = "$v/sbin:$v/bin:$v/usr/sbin:$v/usr/bin:$ENV{PATH}";
        $ENV{PROJECT_NAME} = 'SGN';
    }

    # run setup() on each of our site features
    $_->setup( $c ) for @{ $c->enabled_features };

    # add some other configuration to the web server
    my $root_server = my $server =  Apache2::ServerUtil->server;
    $server = $server->next if $args{mod_perl_vhost};
    $server->add_config( $_ ) for generate_apache_config( \%paths, $c );

    # make and chown the tempfiles dir.
    # note that this mkdir and chown is going to be running as root.

    umask 000002; #< all files written by web server will be group-writable by default

    # the tempfiles_subdir() function makes and chmods the given
    # directory.  with no arguments, will make and chmod the main
    # tempfiles directory
    my $temp_subdir = $c->path_to( $c->tempfiles_subdir() );
    $c->chown_generated_dir( $temp_subdir ); #< force-set the
                                             # permissions on the
                                             # main tempfiles dir

    # also chown any subdirs that are in the temp dir.
    # this line should be removed eventually, the application itself should take
    # care of creating temp dirs if it wants.
    $c->chown_generated_dir( $_ ) for glob "$temp_subdir/*/";
}

sub generate_apache_config {
    my $paths = shift;
    my $c = shift;
    my $cfg = $c->config;

    my $rewrite_logfile = check_logfile($c, $cfg->{rewrite_log} );

    return map {[split /\n/, $_]}

            'PerlSetEnv PROJECT_NAME SGN',

            # don't output a server signature, which gives potential intruders
            # a bit too much information
            'ServerSignature Off',

            # set an environmental variable which will allow modules which are
            # used by multiple virtual hosts (for instance, CXGN::Page) to
            # find out which virtual host they are running on

            'PerlSetEnv PROJECT_NAME SGN',
{'fraction_non_alphanumeric': 0.1134173461823573, 'fraction_numerical': 0.004200642451198419, 'mean_word_length': 2.906853281853282, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1848931', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2425, 'n_words': 851}