Ability to configure a custom domain

parent f6eecd90
v3.1.4
======
* FEATURE: Ability to configure a custom domain in SASS mode (if the server uses nginx)
v3.0.0
======
* MAJOR: Upgrade to php7 (mandatory) and symfony 4.4.
......
#!/bin/bash
# Exple : sh configure_custom_domain.sh my-domain.fr mymap.gogocarto.fr contact@email.com
CUSTOM_URL=$1 # no http(s):// inside
GOGO_URL=$2 # no http(s):// inside
CONTACT_EMAIL=$3
echo "
server {
server_name www.$CUSTOM_URL;
return 301 \$scheme://$CUSTOM_URL\$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name $CUSTOM_URL;
location ~ .* {
proxy_set_header Host $GOGO_URL;
proxy_pass https://$GOGO_URL;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
}
" | tee /etc/nginx/sites-available/$CUSTOM_URL
ln -s /etc/nginx/sites-available/$CUSTOM_URL /etc/nginx/sites-enabled/$CUSTOM_URL
service nginx reload
# Creat ceertificate
certbot certonly --rsa-key-size 4096 --webroot -w /var/www/certbot/ --email $CONTACT_EMAIL --agree-tos --text --renew-hook "/usr/sbin/nginx -s reload" -d $CUSTOM_URL
# Edit
echo "
server {
server_name www.$CUSTOM_URL;
return 301 \$scheme://$CUSTOM_URL\$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name $CUSTOM_URL;
return 301 https://\$server_name\$request_uri;
}
server {
listen 443 ssl http2;
server_name $CUSTOM_URL;
ssl_certificate /etc/letsencrypt/live/$CUSTOM_URL/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$CUSTOM_URL/privkey.pem;
location ~ .* {
proxy_set_header Host $GOGO_URL;
proxy_pass https://$GOGO_URL;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
}" | tee /etc/nginx/sites-available/$CUSTOM_URL
service nginx reload
#!/bin/bash
# Exple : sh remove_custom_domain_config.sh my-domain.fr
CUSTOM_URL=$1
rm /etc/nginx/sites-available/$CUSTOM_URL /etc/nginx/sites-enabled/$CUSTOM_URL
\ No newline at end of file
web_profiler:
toolbar: true
intercept_redirects: false
intercept_redirects: true
framework:
profiler: { only_exceptions: false }
......@@ -110,6 +110,11 @@ services:
- { name: doctrine_mongodb.odm.event_listener, event: postPersist }
App\EventListener\ConfigurationListener:
arguments:
$basePath: '%base_path%'
$baseUrl: '%base_url%'
$contactEmail: '%contact_email%'
$projectDir: '%kernel.project_dir%'
tags:
- { name: doctrine_mongodb.odm.event_listener, event: preUpdate }
......
......@@ -10,6 +10,7 @@ namespace App\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
class ConfigurationAdmin extends ConfigurationAbstractAdmin
{
......@@ -44,6 +45,7 @@ class ConfigurationAdmin extends ConfigurationAbstractAdmin
->add('appNameShort', null, ['label' => 'Nom Court (utilisé par les téléphones, 12 caractères max.)', 'required' => false])
->add('appBaseline', null, ['label' => 'Description du site (baseline)', 'required' => false])
->add('appTags', null, ['label' => 'Mot clés pour le référencement (séparés par une virgule)', 'required' => false])
->add('customDomain', UrlType::class, ['label' => "Utiliser un nom de domaine personnalisé (exple macarte.org au lieu de macarte.gogocarto.fr). Après avoir acheté le nom de domaine macarte.org, vous devez d'abords le faire pointer sur l'adresse IP du serveur GoGoCarto (" . $_SERVER['SERVER_ADDR'] ."). Ensuite renseignez ici le nom de votre domaine", 'required' => false])
->add('dataLicenseUrl', null, ['label' => 'Url de la licence qui protège vos données', 'required' => false])
->end()
->with('Images générales', ['class' => 'col-md-6'])
......
......@@ -27,6 +27,9 @@ class Configuration implements \JsonSerializable
/** @MongoDB\Field(type="string") */
protected $dbName = 'gogocarto_default';
/** @MongoDB\Field(type="string") */
protected $customDomain = null;
// ----------------------------
// --------- BASICS -----------
// ----------------------------
......@@ -3400,4 +3403,14 @@ class Configuration implements \JsonSerializable
$this->publishOnSaasPage = $bool;
return $this;
}
public function getCustomDomain()
{
return $this->customDomain;
}
public function setCustomDomain($domain)
{
$this->customDomain = $domain;
return $this;
}
}
......@@ -12,9 +12,13 @@ class ConfigurationListener
{
protected $asyncService;
public function __construct(AsyncService $asyncService)
public function __construct(AsyncService $asyncService, $baseUrl, $basePath, $contactEmail, $projectDir)
{
$this->asyncService = $asyncService;
$this->baseUrl = $baseUrl;
$this->basePath = $basePath;
$this->contactEmail = $contactEmail;
$this->projectDir = $projectDir;
}
public function preUpdate(\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs $args)
......@@ -80,6 +84,28 @@ class ConfigurationListener
$oldFormFields,
$newFormFields);
}
if (array_key_exists('customDomain', $changeset)) {
$customDomainChanged = $changeset['customDomain'];
$oldCustomDomain = preg_replace('/https?:\/\//', '', $customDomainChanged[0]);
$newCustomDomain = preg_replace('/https?:\/\//', '', $customDomainChanged[1]);
if ($oldCustomDomain) {
$process = new Process(['sh', "$this->projectDir/bin/remove_custom_domain_config.sh", $oldCustomDomain]);
$process->run();
}
if ($newCustomDomain) {
$gogo_url = $document->getDbName() . '.' . $this->baseUrl . $this->basePath;
$process = new Process(['sh', "$this->projectDir/bin/configure_custom_domain.sh", $newCustomDomain, $gogo_url, $this->contactEmail]);
$process->start();
// For debuging :
// foreach ($process as $type => $data) {
// if ($process::OUT === $type) {
// echo "\nRead from stdout: ".$data;
// } else { // $process::ERR === $type
// echo "\nRead from stderr: ".$data;
// }
// }
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment