| 1 | package Smolder::Server; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'CGI::Application::Server'; | ||
| 5 | use File::Spec::Functions qw(catdir devnull catfile); | ||
| 6 | use File::Path qw(mkpath); | ||
| 7 | use Smolder::Conf qw(Port HostName LogFile HtdocsDir DataDir PidFile); | ||
| 8 | use Smolder::DB; | ||
| 9 | use Net::Server::PreFork; | ||
| 10 | use Carp; | ||
| 11 | $SIG{__DIE__} = \*Carp::confess; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my ($class, %args) = @_; | ||
| 15 | my $server = $class->SUPER::new(@_); | ||
| 16 | $server->host(HostName); | ||
| 17 | $server->port(HostName . ':' . Port); | ||
| 18 | |||
| 19 | $server->entry_points( | ||
| 20 | { | ||
| 21 | '/' => 'Smolder::Redirect', | ||
| 22 | '/app' => 'Smolder::Dispatch', | ||
| 23 | '/js' => HtdocsDir, | ||
| 24 | '/style' => HtdocsDir, | ||
| 25 | '/images' => HtdocsDir, | ||
| 26 | '/robots.txt' => HtdocsDir, | ||
| 27 | }, | ||
| 28 | ); | ||
| 29 | $server->{"__smolder_$_"} = $args{$_} foreach keys %args; | ||
| 30 | return $server; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub net_server { 'Net::Server::PreFork' } | ||
| 34 | |||
| 35 | sub print_banner { | ||
| 36 | my $banner = "Smolder is running on " . HostName . ':' . Port . ", pid $$"; | ||
| 37 | my $line = '#' x length $banner; | ||
| 38 | print "$line\n$banner\n"; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub start { | ||
| 42 | my $self = shift; | ||
| 43 | |||
| 44 | if (not -e DataDir) { | ||
| 45 | mkpath(DataDir) or die sprintf("Could not create %s: $!", DataDir); | ||
| 46 | } | ||
| 47 | |||
| 48 | # do we have a database? If not then create one | ||
| 49 | unless (-e Smolder::DB->db_file) { | ||
| 50 | Smolder::DB->create_database; | ||
| 51 | } else { | ||
| 52 | # upgrade if we need to | ||
| 53 | require Smolder::Upgrade; | ||
| 54 | Smolder::Upgrade->new->upgrade(); | ||
| 55 | } | ||
| 56 | |||
| 57 | # preload our perl modules | ||
| 58 | require Smolder::Dispatch; | ||
| 59 | require Smolder::Control; | ||
| 60 | require Smolder::Control::Admin; | ||
| 61 | require Smolder::Control::Admin::Developers; | ||
| 62 | require Smolder::Control::Admin::Projects; | ||
| 63 | require Smolder::Control::Developer; | ||
| 64 | require Smolder::Control::Developer::Prefs; | ||
| 65 | require Smolder::Control::Projects; | ||
| 66 | require Smolder::Control::Graphs; | ||
| 67 | require Smolder::Control::Public; | ||
| 68 | require Smolder::Control::Public::Auth; | ||
| 69 | require Smolder::Redirect; | ||
| 70 | |||
| 71 | my @net_server_args = (pid_file => PidFile); | ||
| 72 | if( $self->{__smolder_daemon} ) { | ||
| 73 | return $self->background(@net_server_args); | ||
| 74 | } else { | ||
| 75 | return $self->run(@net_server_args); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | sub run { | ||
| 80 | my $self = shift; | ||
| 81 | |||
| 82 | # send warnings to our logs | ||
| 83 | my $log_file = LogFile || devnull(); | ||
| 84 | my $ok = open(STDERR, '>>', $log_file); | ||
| 85 | if (!$ok) { | ||
| 86 | warn "Could not open logfile $log_file for appending: $!"; | ||
| 87 | exit(1); | ||
| 88 | } | ||
| 89 | |||
| 90 | $self->SUPER::run(@_); | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; |