|
|
|
|
@ -26,45 +26,6 @@ sub domain_hosted ( $c ) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#== |
|
|
|
|
# POST /blog/domain/hosted | do_blog_domain_hosted |
|
|
|
|
# subdomain | A subdomain that the user would like to use |
|
|
|
|
# hosted_domain_id | The id of a hosted_domain that the user wants to use |
|
|
|
|
# |
|
|
|
|
# This route will create a blog as a subdomain of a hosted domain. We control |
|
|
|
|
# the DNS, SSL, etc. |
|
|
|
|
#== |
|
|
|
|
sub do_domain_hosted ( $c ) { |
|
|
|
|
my $sub_domain = $c->stash->{form}->{subdomain} = $c->param('subdomain'); |
|
|
|
|
my $top_domain = $c->db->hosted_domain( $c->param('hosted_domain_id') ); |
|
|
|
|
|
|
|
|
|
# Store the hosted domain id so we can have the right domain selected on the drop down |
|
|
|
|
# for errors. |
|
|
|
|
$c->stash->{form}->{hdid} = $top_domain->id; |
|
|
|
|
|
|
|
|
|
$sub_domain = lc($sub_domain); |
|
|
|
|
|
|
|
|
|
push @{$c->stash->{errors}}, "Please select a domain from the drop down menu." |
|
|
|
|
unless $top_domain; |
|
|
|
|
|
|
|
|
|
push @{$c->stash->{errors}}, "Please enter a value for the subdomain." |
|
|
|
|
unless $sub_domain; |
|
|
|
|
|
|
|
|
|
push @{$c->stash->{errors}}, "Subdomains must start with a letter, and may use letters, numbers, dashes and hyphens." |
|
|
|
|
unless $sub_domain =~ /^[a-z]+[a-z0-9-_]*$/; |
|
|
|
|
|
|
|
|
|
push @{$c->stash->{errors}}, "That domain name is already being used." |
|
|
|
|
unless $c->db->domains( { name => $sub_domain . '.' . $top_domain->name } )->count == 0; |
|
|
|
|
|
|
|
|
|
return $c->redirect_error( 'show_blog_domain_hosted' ) |
|
|
|
|
if $c->stash->{errors}; |
|
|
|
|
|
|
|
|
|
my $domain_name = $sub_domain . '.' . $top_domain->name; |
|
|
|
|
my $domain_ssl = $top_domain->letsencrypt_challenge ne 'http' ? $top_domain->name : ''; |
|
|
|
|
|
|
|
|
|
return $c->_initialize_blog( $domain_name, 'show_blog_domain_hosted', $domain_ssl ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#== |
|
|
|
|
# GET /blog/domain/owned | show_blog_domain_owned |
|
|
|
|
# |
|
|
|
|
@ -77,54 +38,88 @@ sub domain_owned ( $c ) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#== |
|
|
|
|
# POST /blog/domain/owned | do_blog_domain_owned |
|
|
|
|
# domain_name | The domain name that the user would like to use |
|
|
|
|
# POST /blog/domain | do_blog_domain |
|
|
|
|
# domain | Host a FQDN. This will use http ssl challenge and |
|
|
|
|
# | expect the user to have already configured DNS. |
|
|
|
|
# hosted_subdomain | Host on a subdomain that the system knows about. |
|
|
|
|
# hosted_domain_id | The id of a hosted_domain that the user wants to use |
|
|
|
|
# calling_route | The route that called this (i.e. show_domain_hosted) |
|
|
|
|
# |
|
|
|
|
# This route will create a blog with whatever domain the user supplies. They will |
|
|
|
|
# need to have ensured that DNS is pointed to the correct addresses so that it works. |
|
|
|
|
#== |
|
|
|
|
sub do_domain_owned ( $c ) { |
|
|
|
|
my $domain_name = $c->stash->{form}->{domain_name} = $c->param('domain_name'); |
|
|
|
|
|
|
|
|
|
$domain_name = lc($domain_name); |
|
|
|
|
|
|
|
|
|
push @{$c->stash->{errors}}, "Please enter a value for the domain name." |
|
|
|
|
unless $domain_name; |
|
|
|
|
|
|
|
|
|
push @{$c->stash->{errors}}, "That domain name is already being used." |
|
|
|
|
unless $c->db->domains( { name => $domain_name } )->count == 0; |
|
|
|
|
|
|
|
|
|
return $c->redirect_error( 'show_blog_domain_owned' ) |
|
|
|
|
if $c->stash->{errors}; |
|
|
|
|
|
|
|
|
|
return $c->_initialize_blog( $domain_name, 'show_blog_domain_owned', '' ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#== |
|
|
|
|
# Helper Function... |
|
|
|
|
# domain OR ( hosted_subdomain AND hosted_domain_id ) is required, in addition |
|
|
|
|
# to calling_route. |
|
|
|
|
# |
|
|
|
|
# This will make the blog after domain_hosted / domain_owned figures out what |
|
|
|
|
# it's making. |
|
|
|
|
#== |
|
|
|
|
sub _initialize_blog ( $c, $domain, $from, $ssl_domain ) { |
|
|
|
|
|
|
|
|
|
# Do we already have this domain name? |
|
|
|
|
if ( $c->db->domain( { name => $domain } ) ) { |
|
|
|
|
$c->flash( errors => [ 'That domain name is already being hosted.' ] ); |
|
|
|
|
$c->redirect_to( $c->url_for( $from ) ); |
|
|
|
|
return; |
|
|
|
|
# This route will create a blog on a given domain. When domain is given, use that |
|
|
|
|
# as a FQDN with http ssl challenge. |
|
|
|
|
# |
|
|
|
|
# When hosted_subdomain is given, prepend it to the name of the domain identified by |
|
|
|
|
# hosted_domain_id, and use whatever ssl challenge method the hosted_domain is set to |
|
|
|
|
# use. |
|
|
|
|
# |
|
|
|
|
# When an error happens, the calling_route will be returned to: |
|
|
|
|
# |
|
|
|
|
# show_blog_domain_hosted |
|
|
|
|
# show_blog_domain_owned |
|
|
|
|
#== |
|
|
|
|
sub do_domain ( $c ) { |
|
|
|
|
my $domain = $c->stash->{form}->{domain} = lc($c->param('domain')); |
|
|
|
|
my $hosted_subdomain = $c->stash->{form}->{hosted_subdomain} = lc($c->param('hosted_subdomain')); |
|
|
|
|
my $hosted_domain_id = $c->stash->{form}->{hosted_domain_id} = $c->param('hosted_domain_id'); |
|
|
|
|
my $calling_route = $c->stash->{form}->{calling_route} = $c->param('calling_route'); |
|
|
|
|
|
|
|
|
|
if ( $calling_route eq 'show_blog_domain_owned' ) { |
|
|
|
|
$c->stash->{fqdn} = $domain; |
|
|
|
|
|
|
|
|
|
# Domain name defined? |
|
|
|
|
push @{$c->stash->{errors}}, "Please enter a value for the domain name." |
|
|
|
|
unless $domain; |
|
|
|
|
|
|
|
|
|
# Already exists? |
|
|
|
|
push @{$c->stash->{errors}}, "That domain name is already being used." |
|
|
|
|
unless $c->db->domains( { name => $domain } )->count == 0; |
|
|
|
|
|
|
|
|
|
} elsif ( $calling_route eq 'show_blog_domain_hosted' ) { |
|
|
|
|
$c->stash->{hosted_domain} = $c->db->hosted_domain( $hosted_domain_id ); |
|
|
|
|
$c->stash->{form}->{hdid} = $c->stash->{hosted_domain}->id; |
|
|
|
|
$c->stash->{fqdn} = $hosted_subdomain . '.' . $c->stash->{hosted_domain}->name; |
|
|
|
|
$c->stash->{ssl_domain} = $c->stash->{hosted_domain}->name; |
|
|
|
|
|
|
|
|
|
# Valid hosted_domain selected. |
|
|
|
|
push @{$c->stash->{errors}}, "Please select a domain from the drop down menu." |
|
|
|
|
unless $c->stash->{hosted_domain}; |
|
|
|
|
|
|
|
|
|
# Sub domain was answered. |
|
|
|
|
push @{$c->stash->{errors}}, "Please enter a value for the subdomain." |
|
|
|
|
unless $hosted_subdomain; |
|
|
|
|
|
|
|
|
|
# Subdomain is valid. |
|
|
|
|
push @{$c->stash->{errors}}, "Subdomains must start with a letter, and may use letters, numbers, dashes and hyphens." |
|
|
|
|
unless $hosted_subdomain =~ /^[a-z]+[a-z0-9-_]*$/; |
|
|
|
|
|
|
|
|
|
# Already exists? |
|
|
|
|
push @{$c->stash->{errors}}, "That domain name is already being used." |
|
|
|
|
unless $c->db->domains( { name => $c->stash->{fqdn} } )->count == 0; |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
# Shouldn't have gotten here. |
|
|
|
|
push @{$c->stash->{errors}}, "If you continue to experience problems, please contact support."; |
|
|
|
|
$calling_route = 'show_blog_domain_hosted'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
# Create the Jekyll repo for the site |
|
|
|
|
# This is where a job to build it on the build server should happen... |
|
|
|
|
# Bail on any errors. |
|
|
|
|
return $c->redirect_error( $calling_route ) |
|
|
|
|
if $c->stash->{errors}; |
|
|
|
|
|
|
|
|
|
my $jekyll = $c->jekyll($domain)->init; |
|
|
|
|
# Set scalars to values from stash, and initialize Jekyll blog. |
|
|
|
|
my $hosted_domain = $c->stash->{hosted_domain}; |
|
|
|
|
my $fqdn = $c->stash->{fqdn}; |
|
|
|
|
my $ssl_domain = $c->stash->{ssl_domain}; |
|
|
|
|
my $jekyll = $c->jekyll($fqdn)->init; |
|
|
|
|
|
|
|
|
|
my $blog = try { |
|
|
|
|
$c->db->storage->schema->txn_do( sub { |
|
|
|
|
# Make the domain name record. |
|
|
|
|
my $domain_record = $c->stash->{person}->create_related('domains', { |
|
|
|
|
name => $domain, |
|
|
|
|
name => $fqdn, |
|
|
|
|
( $ssl_domain ? ( ssl => $ssl_domain ) : () ), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
@ -143,11 +138,9 @@ sub _initialize_blog ( $c, $domain, $from, $ssl_domain ) { |
|
|
|
|
push @{$c->stash->{errors}}, "Blog could not be created: $_"; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if ( $c->stash->{errors} ) { |
|
|
|
|
$c->flash( errors => $c->stash->{errors} ); |
|
|
|
|
$c->redirect_to( $c->url_for( $from ) ); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
# Bail on any errors. |
|
|
|
|
return $c->redirect_error( $calling_route ) |
|
|
|
|
if $c->stash->{errors}; |
|
|
|
|
|
|
|
|
|
# Schedule a job to deploy the website |
|
|
|
|
if ( ! $ssl_domain ) { |
|
|
|
|
|