[Bio] / FigWebServices / rast_submit_server.cgi Repository:
ViewVC logotype

View of /FigWebServices/rast_submit_server.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (annotate)
Wed Oct 21 20:05:51 2009 UTC (10 years, 7 months ago) by olson
Branch: MAIN
Changes since 1.2: +1 -0 lines
Add support for get_contig_ids_in_project_from_entrez

use strict;
use FIG;

my $have_fcgi;
eval {
    require CGI::Fast;
    $have_fcgi = 1;
};

use Data::Dumper;
use DBMaster;
use FIG_Config;
use RAST_submission;

#
# This is the list of supported methods.
#

my @methods = qw(
		 get_contig_ids_in_project_from_entrez
		 submit_RAST_job
		 status_of_RAST_job
		 retrieve_RAST_job
		);

my %methods = map { $_ => 1 } @methods;

use YAML;

$| = 1;

my $header = "Content-type: text/plain\n\n";

my $max_requests = 5000;

#
# If no CGI vars, assume we are invoked as a fastcgi service.
#
my $n_requests = 0;
if ($have_fcgi && $ENV{REQUEST_METHOD} eq '')
{
    while ((my $cgi = new CGI::Fast()) &&
	   ($max_requests == 0 || $n_requests++ < $max_requests))
    {
	eval {
	    &process_request($cgi);
	};
	if ($@)
	{
	    if (ref($@) ne 'ARRAY')
	    {
		warn "code died, cgi=$cgi returning error\n";
		print $cgi->header(-status => '500 error in body of cgi processing');
		print $@;
	    }
	}
    endloop:
    }
}
else
{
    my $cgi = new CGI();
    &process_request($cgi);
}

exit;

sub process_request
{
    my($cgi) = @_;

    my $function = $cgi->param('function');
#    print STDERR "got function=$function\n";

    my $arg_str = $cgi->param('args');
    my @args;
    if ($arg_str)
    {
	eval {
	    @args = YAML::Load($arg_str);
	};
	if ($@)
	{
	    myerror($cgi, "500 bad YAML parse", "YAML parse failed");
	    next;
	}
    }

    $function or myerror($cgi, "500 missing argument", "missing function argument");

    #
    # Pull username & password from the arguments and authenticate.
    #

    my $rast_user = $cgi->param('username');
    my $rast_password = $cgi->param('password');

    if ($rast_user eq '')
    {
	&myerror($cgi, '500 missing username', 'RAST username is missing');
    }

    #
    # Connect to the authentication database.
    #

    my $dbmaster;
    eval {
      $dbmaster = DBMaster->new(-database => $FIG_Config::webapplication_db || "WebAppBackend",
				-host     => $FIG_Config::webapplication_host || "localhost",
				-user     => $FIG_Config::webapplication_user || "root",
				-password => $FIG_Config::webapplication_password || "");
    };

    #
    # And evaluate username and password.
    #

    my $user_obj = $dbmaster->User->init( { login => $rast_user });
    if (!ref($user_obj) || !$user_obj->active)
    {
	&myerror($cgi, '500 invalid login', 'Invalid RAST login');
    }

    if (crypt($rast_password, $user_obj->password) ne $user_obj->password)
    {
	&myerror($cgi, '500 invalid login', 'Invalid RAST login');
    }
    warn "Authenticated $rast_user\n";

    # Connect to the RAST job cache
    my $rast_dbmaster = DBMaster->new(-backend => 'MySQL',
				      -database  => $FIG_Config::rast_jobcache_db,
				      -host     => $FIG_Config::rast_jobcache_host,
				      -user     => $FIG_Config::rast_jobcache_user,
				      -password => $FIG_Config::rast_jobcache_password );
    
    my $rast_obj = new RAST_submission($rast_dbmaster, $dbmaster, $user_obj);

    if ($methods{$function})
    {

	my @results;
	eval {
	    @results = $rast_obj->$function(@args);
	};

	if ($@)
	{
	    myerror($cgi, '500 error in method invocation', $@);
	}

	print $cgi->header();
	print YAML::Dump(@results);

    } else {
	myerror($cgi,  "500 invalid function", "invalid function $function\n");
    }
}

exit;

sub get_string_param
{
    my($cgi, $name) = @_;

    my $str = $cgi->param($name);
    if ($str =~ /^(\S+)/)
    {
	return $1;
    }
    else
    {
	return undef;
    }
    
}


sub myerror
{
    my($cgi, $stat, $msg) = @_;
    print $cgi->header(-status =>  $stat);
    print "$msg\n";
    goto endloop;
}





MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3