diff --git a/Traefik.php b/Traefik.php index 274aac272829e8c9585ef52cebd7b80ce56f10e6..4c8c09f53b6e48f951dc797eb64f6172303c6aba 100644 --- a/Traefik.php +++ b/Traefik.php @@ -19,23 +19,64 @@ class Traefik { */ protected $name; + /** + * @var string + */ + protected $domain; + + /** + * @var int + */ + protected $http_port; + + /** + * @var int + */ + protected $https_port; + + /** + * @var string + */ + protected $cert_filename; + + /** + * @var string + */ + protected $key_filename; + /** * Traefik constructor. * * @param string $name * Name of the network to be created which needs to match the container * prefix of your project you would like to handle with Traefik. + * @param string $domain + * The domain name used for all local projects. + * @param int $http_port + * Port for non secure requests. + * @param int $https_port + * Port for secure requests. + * @param string $cert_filename + * Filename of the SSL certificate. + * @param string $key_filename + * Filename of the private key for the SSL certificate. */ - public function __construct($name) { + public function __construct(string $name, $domain = 'docker.localhost', $http_port = 8000, $https_port = 8443, $cert_filename = 'cert.pem', $key_filename = 'key.pem') { $this->name = $name; + $this->domain = $domain; + $this->http_port = $http_port; + $this->https_port = $https_port; + $this->cert_filename = $cert_filename; + $this->key_filename = $key_filename; } /** * Update the Traefik container. */ - public function update() { + public function update(): void { // Update host wider traefik container. $traefikPath = $_SERVER['HOME'] . '/.traefik'; + $traefikCertPath = $_SERVER['HOME'] . '/.traefik/certs'; $traefikFile = $traefikPath . '/docker-compose.yml'; $fs = new Filesystem(); @@ -50,12 +91,14 @@ class Traefik { 'traefik' => [ 'image' => 'traefik:1.7.17', 'restart' => 'unless-stopped', - 'command' => '-c /dev/null --web --docker --logLevel=DEBUG', + 'command' => '-c /dev/null --web --docker --defaultEntryPoints="https" --defaultEntryPoints="http" --entryPoints="Name:https Address::443 TLS:/certs/' . $this->cert_filename . ',/certs/' . $this->key_filename . '" --entryPoints="Name:http Address::80"', 'networks' => [], 'ports' => [ - '8000:80', + $this->http_port . ':80', + $this->https_port . ':443', ], 'volumes' => [ + './certs:/certs/', '/var/run/docker.sock:/var/run/docker.sock', ], ], @@ -63,6 +106,9 @@ class Traefik { 'networks' => [], ]; } + if (!$fs->exists($traefikCertPath)) { + $fs->mkdir($traefikCertPath); + } if (!in_array($this->name, $traefik['services']['traefik']['networks'], TRUE)) { $traefik['services']['traefik']['networks'][] = $this->name;