Skip to content

Commit

Permalink
Merge branch 'feature/proxy-command' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisnharvey committed Sep 29, 2020
2 parents 3610974 + fb971f4 commit ea0a37e
Show file tree
Hide file tree
Showing 5 changed files with 494 additions and 306 deletions.
24 changes: 4 additions & 20 deletions containers/nginx/config/nginx.conf
Expand Up @@ -45,30 +45,14 @@ server {
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;

server_name ~^(?<vhost>[^.]*)\.(?<version>[^.]*)\.localhost$;

set $rootdir "/projects/$vhost/public";
set $php "10.0.10.$version:9000";

root $rootdir;
server_name *.localhost;

client_max_body_size 600M;

index index.html index.php index.htm;

location ~* (\.php)$ {
fastcgi_pass $php;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 300;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
proxy_pass http://nginx;
proxy_set_header Host $host;
proxy_pass_request_headers on;
}
}

Expand Down
1 change: 1 addition & 0 deletions containers/nginx/config/ssl.ext
Expand Up @@ -10,3 +10,4 @@ DNS.3 = *.71.localhost
DNS.4 = *.72.localhost
DNS.5 = *.73.localhost
DNS.6 = *.74.localhost
DNS.7 = *.magiclamp.localhost
102 changes: 102 additions & 0 deletions containers/workspace/cli/app/Commands/ProxyCommand.php
@@ -0,0 +1,102 @@
<?php

namespace App\Commands;

use App\Docker\DockerComposeFile;
use App\DockerCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;

class ProxyCommand extends DockerCommand
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = '
proxy
{domain}
{containerPort}
{--container=workspace}
';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Proxy a domain to a port with nginx';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(DockerComposeFile $compose)
{
$container = $this->option('container');
$domain = $this->argument('domain');
$containerPort = $this->argument('containerPort');

if (! Str::endsWith($domain, '.localhost')) {
$this->warn(' The domain you supplied does not end with .magiclamp.localhost or .localhost.');
$this->warn(' Automatic DNS and Automatic SSL will be unavailable for this entry');
} elseif (! Str::endsWith($domain, 'magiclamp.localhost')) {
$this->warn(' The domain you supplied ends with .localhost, but not .magiclamp.localhost.');
$this->warn(' Automatic SSL will be unavailable for this entry');
}

$containerPath = '/etc/nginx/conf.d/' . $this->argument('domain') . '.conf';

$directory = $this->docker->getMagiclampPath().'/conf/nginx/'.trim(pathinfo($containerPath, PATHINFO_DIRNAME), '/');
$hostPath = $directory.'/'.pathinfo($containerPath, PATHINFO_BASENAME);

if (file_exists($hostPath)) {
$this->warn(" {$domain} is already proxied.");

if (! $this->confirm('Would you like to replace it?')) {
exit(0);
}
} else {
$this->info(" {$domain} will be added and proxied to port {$containerPort} on the {$container} container");
if (! $this->confirm("Do you want to continue?")) {
exit(0);
}

$this->host->createDirectory($directory);
}

$stub = file_get_contents(base_path('stubs/nginx-proxy.stub'));
$stub = str_replace(['{domain}', '{proxyUrl}'], [$domain, "http://{$container}:{$containerPort}"], $stub);

file_put_contents($hostPath, $stub);

$compose->setVolume('nginx', $containerPath, $this->docker->containerPathToHostPath($hostPath));
$compose->save();

$this->info("\n File updated.");

$this->info(" The nginx container must be restarted for your changes to take effect");

if (! $this->confirm("Would you like to restart nginx now?")) {
exit(0);
}

$this->docker->recreate('nginx');

$this->info(' Done.');
}

/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}
}

0 comments on commit ea0a37e

Please sign in to comment.