ASP4

 view release on metacpan or  search on metacpan

sbin/asphelper  view on Meta::CPAN

#!/usr/bin/perl -w

use strict;
use warnings 'all';
use Getopt::Long;
use Term::ReadKey;
use File::Path;
use DBI;
use Cwd 'cwd';

*make_path = \&File::Path::make_path;


my (
  $appName,
  $domain,
  $dbName,
  $dbHost,
  $dbUser,
  $dbPass,
  $email,
);


my $result = GetOptions(
  "app=s"     => \$appName, # Required
  "domain=s"  => \$domain,  # Required
  "email=s"   => \$email,   # Required
  "db=s"      => \$dbName,
  "user=s"    => \$dbUser,
  "host=s"    => \$dbHost,
);

$appName && $domain && $email or die "Usage: $0 --app=AppName --domain=domain.com --email=you\@your-email.com [--host=dbhost --db=dbname  --user=dbusername]\n";
$dbHost ||= "localhost";

if( $dbName && $dbUser )
{
  print STDERR "Enter your database password: ";
  ReadMode('noecho');
  chomp($dbPass = <STDIN>);
  ReadMode('restore');
  print "\n";
}# end if()

my @DSN = (
  "DBI:mysql:$dbName:$dbHost",
  $dbUser,
  $dbPass
);

my $drh = DBI->install_driver("mysql");
my $rc = $drh->func('createdb', $dbName, $dbHost, $dbUser, $dbPass, 'admin');

my $dbh = eval { DBI->connect( @DSN, {RaiseError => 1} ) };
if( $@ )
{
  (my $error = $@) =~ s/\sat\s\Q$0\E\s+line.*//;
  die "[ERROR]: $error\n";
}# end if()

# Setup folder structure:
(my $project_path = lc($appName)) =~ s{::}{_}sg;
make_path($project_path);
chdir($project_path);
my $cwd = cwd();
my $appFolder = join( '/',  split(/::/, $appName) );
make_path("common/lib/$appFolder/db");
make_path("common/sbin");
make_path('www/t/010-basic');
make_path('www/conf');
make_path('www/etc');
make_path('www/htdocs');
make_path('www/handlers');



# Write the ddl.sql file:
unless( -f "common/sbin/ddl.sql" )
{
  warn "common/sbin/ddl.sql\n";
  open my $ofh, '>', "common/sbin/ddl.sql"
    or die "Cannot open 'common/sbin/ddl.sql' for writing: $!";
  print $ofh <<"SQL";

set foreign_key_checks = 0;
drop table if exists asp_sessions;
set foreign_key_checks = 1;

create table asp_sessions (
  session_id   char(32) not null primary key,
  session_data blob,
  created_on   datetime default null,
  modified_on  datetime default null
) engine=innodb charset=utf8;

SQL
  close($ofh);
  open my $ifh, "common/sbin/ddl.sql"
    or die "Cannot open 'common/sbin/ddl.sql' for reading: $!";
  local $/ = ';';
  while( my $cmd = <$ifh> )
  {
    $cmd =~ s/^\s+//s;
    $cmd =~ s/\s+$//s;
    next unless $cmd;
    $dbh->do($cmd);
  }# end while()
  close($ifh);
}# end unless()


# Write our configs:
unless( -f "www/conf/asp4-config.json" )
{
  warn "www/conf/asp4-config.json\n";
  open my $ofh, '>', "www/conf/asp4-config.json"
    or die "Cannot open 'www/conf/asp4-config.json' for writing: $!";
  my $json = generic_config( $dbName && $dbUser );

sbin/asphelper  view on Meta::CPAN

  close($ofh);
}# end unless()

warn "="x60, "\n";
warn "    Running Initial Test Suite...\n";
warn "="x60, "\n";
chdir("www");
`asp4 /` =~ m{You have visited this page}
  or die "Warning: ASP script contents (/index.asp) not what we expected!";
system("prove -rv t");

# Talk about the config now:
warn <<"END";

!!!!!!!!!!!!!!!!!!!!!! CONGRATULATIONS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*** Update your Apache Config ***
  Include $cwd/www/conf/httpd.conf

*** Add This to your /etc/hosts File ***
  127.0.0.1     $domain

*** Restart Apache ***
  Maybe:  sudo /etc/init.d/httpd restart
  Or:     sudo /etc/init.d/apache2 restart
  Or:     service apache2 restart

*** To Test it in Your Browser ***
  Navigate to http://$domain

*** To Run the Unit Tests ***
  cd $cwd/www
  prove -rv t

*** To Run an ASP Script From the Command Line ***
  cd $cwd/www
  asp4 /index.asp

END


sub generic_config
{
  my ($has_db) = @_;

  my $str = <<'EOF';
{
  "system": {
    "post_processors": [
    ],
    "libs": [
      "@ServerRoot@/lib",
      "@ProjectRoot@/common/lib"
    ],
    "load_modules": [
    ],
    "env_vars": {
    },
    "settings": {
    }
  },
  "errors": {
    "error_handler":    "ASP4::ErrorHandler",
    "mail_errors_to":   "%email%",
    "mail_errors_from": "root@localhost",
    "smtp_server":      "localhost"
  },
  "web": {
    "application_name": "%appName%",
    "application_root": "@ServerRoot@",
    "www_root":         "@ServerRoot@/htdocs",
    "handler_root":     "@ServerRoot@/handlers",
    "page_cache_root":  "/tmp/PAGE_CACHE",
    "handler_resolver": "ASP4::HandlerResolver",
    "handler_runner":   "ASP4::HandlerRunner",
    "filter_resolver":  "ASP4::FilterResolver",
    "request_filters": [
    ],
    "routes": [
    ],
    "disable_persistence": [
    ]
  },
  "data_connections": {
EOF

  if( $has_db )
  {
    $str .= <<'EOF';
    "session": {
      "manager":          "ASP4::SessionStateManager",
      "cookie_name":      "session-id",
      "cookie_domain":    "*",
      "session_timeout":  "*",
      "dsn":              "DBI:mysql:%dbName%:%dbHost%",
      "username":         "%dbUser%",
      "password":         "%dbPass%"
    },
    "main": {
      "dsn":              "DBI:mysql:%dbName%:%dbHost%",
      "username":         "%dbUser%",
      "password":         "%dbPass%"
    }
EOF
  }
  else
  {
    $str .= <<'EOF';
    "session": {
      "manager":          "ASP4::SessionStateManager::NonPersisted",
      "cookie_name":      "session-id",
      "cookie_domain":    "*",
      "session_timeout":  "*",
      "dsn":              "DBI:mysql:dbname:hostname",
      "username":         "admin",
      "password":         "swordfish"
    },
    "main": {
      "dsn":              "DBI:mysql:dbname:hostname",
      "username":         "admin",
      "password":         "swordfish"
    }
EOF
  }# end if()

$str .= <<'EOF';
  }
}
EOF

  return $str;
}


sub generic_httpconf {
  <<'EOF';

# Load up some important modules:
PerlModule DBI
PerlModule DBD::mysql
PerlModule ASP4::ModPerl

# Apache2::Reload does not play well with ASP4.
# Uncomment the following line if you get Apache2::Reload errors:
#PerlSetVar ReloadAll Off

# Admin website:
<VirtualHost *:80>

  ServerName    %domain%
  DocumentRoot  %CWD%/www/htdocs
  
  # Set the directory index:
  DirectoryIndex index.asp
  
  # All *.asp files are handled by ASP4::ModPerl
  <Files ~ (\.asp$)>
    SetHandler  perl-script
    PerlResponseHandler ASP4::ModPerl
    SetEnv no-gzip dont-vary
  </Files>
  
  # !IMPORTANT! Prevent anyone from viewing your *.pm files:
  <Files ~ (\.pm$)>
    Order allow,deny
    Deny from all
  </Files>
  
  # All requests to /handlers/* will be handled by their respective handler:
  <Location /handlers>
    SetHandler  perl-script
    PerlResponseHandler ASP4::ModPerl
    SetEnv no-gzip dont-vary
  </Location>
  
</VirtualHost>

EOF
}





=pod

=head1 NAME

asphelper - Generate an ASP4 skeleton web application

=head1 USAGE

  asphelper --app=AppName --domain=example.com --email=you@your-email.com [--host=dbhost --db=dbname  --user=dbusername]

If you specify C<--dbname> and C<--dbuser> it will ask you for a database password - completely optional.

=head1 DESCRIPTION

The C<asphelper> program offers a way to get up-and-running quickly with a new ASP4 web application.

After successfully answering its questions, C<asphelper> will generate a skeleton web application
including config files, full directory structure and a simple unit test.

Use the resulting application as a starting-point for your own development.



( run in 2.272 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )