Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List traefik urls #126

Open
wants to merge 1 commit into
base: 0.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ services:
public: true

my127\Workspace\Path\Path: '@my127\Workspace\Path\Composite'

my127\Workspace\File\FileLoader\FileGetContentsLoader: ~
my127\Workspace\File\FileLoader:
alias: 'my127\Workspace\File\FileLoader\FileGetContentsLoader'

8 changes: 8 additions & 0 deletions src/File/ArrayLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace my127\Workspace\File;

interface ArrayLoader
{
public function loadArray(string $url): array;
}
16 changes: 2 additions & 14 deletions src/File/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@

namespace my127\Workspace\File;

use function file_get_contents;
use my127\Workspace\File\Exception\CouldNotLoadFile;

final class FileLoader
interface FileLoader
{
public function load(string $url): string
{
$contents = @file_get_contents($url);

if ($contents === false) {
throw new CouldNotLoadFile(sprintf('Could not load file at "%s"', $url));
}

return $contents;
}
public function load(string $url): string;
}
21 changes: 21 additions & 0 deletions src/File/FileLoader/FileGetContentsLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace my127\Workspace\File\FileLoader;

use function file_get_contents;
use my127\Workspace\File\Exception\CouldNotLoadFile;
use my127\Workspace\File\FileLoader;

final class FileGetContentsLoader implements FileLoader
{
public function load(string $url): string
{
$contents = @file_get_contents($url);

if ($contents === false) {
throw new CouldNotLoadFile(sprintf('Could not load file at "%s"', $url));
}

return $contents;
}
}
23 changes: 23 additions & 0 deletions src/File/FileLoader/TestFileLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace my127\Workspace\File\FileLoader;

use my127\Workspace\File\FileLoader;

class TestFileLoader implements FileLoader
{
/**
* @var string
*/
private $contents;

public function __construct(string $contents)
{
$this->contents = $contents;
}

public function load(string $url): string
{
return $this->contents;
}
}
42 changes: 42 additions & 0 deletions src/Plugin/TraefikEndpoints/TraefikEndpointProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace my127\Workspace\Plugin\TraefikEndpoints;

use my127\Workspace\File\JsonLoader;

class TraefikEndpointProvider
{
public const TRAEFIK_BASE_URL = 'https://my127.site';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be good to get this from the env somehow than assume it exists, but for now probably "fine"


/**
* @var JsonLoader
*/
private $loader;

public function __construct(JsonLoader $loader)
{
$this->loader = $loader;
}

/**
* @return string[]
*/
public function links(): array
{
$providers = $this->loader->loadArray(sprintf(
'%s/api/providers',
self::TRAEFIK_BASE_URL
));

$links = [];
foreach ($providers['docker']['frontends'] as $frontend) {
foreach ($frontend['routes'] as $route) {
foreach (explode(',', $route['rule']) as $host) {
$links[] = sprintf('https://%s', preg_replace('{^Host:(.*)$}', '\1', $host));
}
}
}

return $links;
}
}
47 changes: 47 additions & 0 deletions src/Plugin/TraefikEndpoints/TraefikEndpointsPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace my127\Workspace\Plugin\TraefikEndpoints;

use Closure;
use my127\Console\Application\Application;
use my127\Console\Application\Plugin\Plugin;
use my127\Console\Usage\Input;
use Symfony\Component\Console\Output\ConsoleOutput;

class TraefikEndpointsPlugin implements Plugin
{
/**
* @var TraefikEndpointProvider
*/
private $provider;

/**
* @var ConsoleOutput
*/
private $output;

public function __construct(TraefikEndpointProvider $provider, ConsoleOutput $output)
{
$this->provider = $provider;
$this->output = $output;
}

public function setup(Application $application): void
{
$application->section('traefik urls')
->description('List the Traefik endpoints')
->action($this->action());
}

/**
* @return Closure(): <missing>
*/
private function action(): Closure
{
return function (Input $args) {
foreach ($this->provider->links() as $link) {
$this->output->writeln($link);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace my127\Workspace\Tests\Unit\File;
namespace my127\Workspace\Tests\Test\File\FileLoader;

use my127\Workspace\File\Exception\CouldNotLoadFile;
use my127\Workspace\File\FileLoader;
use my127\Workspace\File\FileLoader\FileGetContentsLoader;
use my127\Workspace\Tests\IntegrationTestCase;

class FileLoaderTest extends IntegrationTestCase
class FileGetContentsLoaderTest extends IntegrationTestCase
{
/** @test */
public function testItLoadsFile(): void
Expand All @@ -24,6 +24,6 @@ public function testThrowsAnExceptionIfTheFileCannotBeLoaded(): void

private function load(string $url): string
{
return (new FileLoader())->load($url);
return (new FileGetContentsLoader())->load($url);
}
}
4 changes: 2 additions & 2 deletions tests/Test/File/JsonLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace my127\Workspace\Tests\Test\File;

use my127\Workspace\File\Exception\CouldNotDecodeJson;
use my127\Workspace\File\FileLoader;
use my127\Workspace\File\FileLoader\FileGetContentsLoader;
use my127\Workspace\File\JsonLoader;
use my127\Workspace\Tests\IntegrationTestCase;

Expand All @@ -26,6 +26,6 @@ public function testItThrowsAnExceptionIfTheJsonCannotBeDecoded(): void

private function load(string $url): array
{
return (new JsonLoader(new FileLoader()))->loadArray($url);
return (new JsonLoader(new FileGetContentsLoader()))->loadArray($url);
}
}
29 changes: 29 additions & 0 deletions tests/Test/Plugin/TraefikEndpoints/TraefikEndpointProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace my127\Workspace\Tests\Test\Plugin\TraefikEndpoints;

use my127\Workspace\File\FileLoader\TestFileLoader;
use my127\Workspace\File\JsonLoader;
use my127\Workspace\Plugin\TraefikEndpoints\TraefikEndpointProvider;
use PHPUnit\Framework\TestCase;

class TraefikEndpointProviderTest extends TestCase
{
public function testListEndpoints(): void
{
$loader = new JsonLoader(new TestFileLoader((string) file_get_contents(__DIR__ . '/fixture/traefik-providers.json')));
self::assertEquals([
'https://kafka-ui.projectx-notify',
'https://kafka.projectx-ecomm',
'https://mail.my127.site',
'https://my127.site',
'https://search-project.my127.site',
'https://projectx-ecomm.my127.site',
'https://hub-project.my127.site',
'https://a1-project.my127.site',
'https://a2-project.my127.site',
'https://projectx-example.my127.site',
'https://zookeeper.projectx-ecomm',
], (new TraefikEndpointProvider($loader))->links());
}
}
Loading