Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
39 changes: 39 additions & 0 deletions app/Http/Controllers/Backend/MediaWikiHostController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Services\MediaWikiHostResolver;
use App\Services\UnknownDBVersionException;
use App\Services\UnknownWikiDomainException;
use Illuminate\Http\Request;

class MediaWikiHostController extends Controller {
public function getWikiHostForDomain(Request $request): \Illuminate\Http\JsonResponse {
Comment thread
outdooracorn marked this conversation as resolved.
Outdated
$mediawikiHostResolver = new MediaWikiHostResolver;
$domain = $request->query('domain');
try {
$backendHost = $mediawikiHostResolver->getBackendHostForDomain($domain);
$webHost = $mediawikiHostResolver->getWebHostForDomain($domain);
$apiHost = $mediawikiHostResolver->getApiHostForDomain($domain);
$alphaHost = $mediawikiHostResolver->getAlphaHostForDomain($domain);
} catch (UnknownWikiDomainException $e) {
Comment thread
outdooracorn marked this conversation as resolved.
Outdated
return response()->json(['error' => 'Domain not found.'], 404);
} catch (UnknownDBVersionException $e) {
return response()->json(['error' => 'Unknown database version.'], 500);
}

return response()
->json([
'domain' => $domain,
'backend-host' => $backendHost,
'web-host' => $webHost,
'api-host' => $apiHost,
'alpha-host' => $alphaHost,
])
->header('x-backend-host', $backendHost)
->header('x-web-host', $webHost)
->header('x-api-host', $apiHost)
->header('x-alpha-host', $alphaHost);
}
}
15 changes: 15 additions & 0 deletions app/Services/MediaWikiHostResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ public function getBackendHostForDomain(string $domain): string {
return sprintf('mediawiki-%s-app-backend.default.svc.cluster.local', $this->getMwVersionForDomain($domain));
}

public function getWebHostForDomain(string $domain): string {
Comment thread
outdooracorn marked this conversation as resolved.
Outdated
// TODO: Move 'web.default.svc.cluster.local' to an env variable (e.g. PLATFORM_MW_WEB_HOST_SUFFIX) for flexibility.
return sprintf('mediawiki-%s-app-web.default.svc.cluster.local', $this->getMwVersionForDomain($domain));
}

public function getApiHostForDomain(string $domain): string {
// TODO: Move 'api.default.svc.cluster.local' to an env variable (e.g. PLATFORM_MW_API_HOST_SUFFIX) for flexibility.
return sprintf('mediawiki-%s-app-api.default.svc.cluster.local', $this->getMwVersionForDomain($domain));
}

public function getAlphaHostForDomain(string $domain): string {
// TODO: Move 'alpha.default.svc.cluster.local' to an env variable (e.g. PLATFORM_MW_ALPHA_HOST_SUFFIX) for flexibility.
return sprintf('mediawiki-%s-app-alpha.default.svc.cluster.local', $this->getMwVersionForDomain($domain));
}

public function getMwVersionForDomain(string $domain): string {
$wiki = Wiki::where('domain', $domain)->first();

Expand Down
2 changes: 2 additions & 0 deletions routes/backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function () {
$router->get('getWikiVersionForDomain', ['uses' => 'IngressController@getWikiVersionForDomain']);
});

$router->get('getWikiHostForDomain', ['uses' => 'MediaWikiHostController@getWikiHostForDomain']);

$router->group(['prefix' => 'wiki'], function () use ($router) {
$router->get('getWikiForDomain', ['uses' => 'WikiController@getWikiForDomain']);
$router->patch('updateEntityImport', ['uses' => '\App\Http\Controllers\WikiEntityImportController@update']);
Expand Down
59 changes: 59 additions & 0 deletions tests/Routes/Backend/MediaWikiHostControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Routes\Backend;

use App\Wiki;
use App\WikiDb;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class MediaWikiHostControllerTest extends TestCase {
use RefreshDatabase;

private function createWiki(string $domain, string $version) {
$wiki = Wiki::factory()->create(['domain' => $domain]);
WikiDb::create([
'name' => $domain,
'user' => 'someUser',
'password' => 'somePassword',
'version' => $version,
'prefix' => 'somePrefix',
'wiki_id' => $wiki->id,
]);
}

public function testDomainNotfound() {
$this->getJson('/backend/getWikiHostForDomain?domain=notfound.wikibase.cloud')
->assertStatus(404);
}

public function testDbVersionNotfound() {
$this->createWiki('noversion.wikibase.cloud', 'unknownVersion');
$this->getJson('/backend/getWikiHostForDomain?domain=noversion.wikibase.cloud')
->assertStatus(500);
}

public function testFoundHost() {
$expectedHosts = [
'backend' => 'mediawiki-143-app-backend.default.svc.cluster.local',
'web' => 'mediawiki-143-app-web.default.svc.cluster.local',
'api' => 'mediawiki-143-app-api.default.svc.cluster.local',
'alpha' => 'mediawiki-143-app-alpha.default.svc.cluster.local',
];
$this->createWiki('found.wikibase.cloud', 'mw1.43-wbs1');
$this->createWiki('other.wikibase.cloud', 'otherVersion');
$this->getJson('/backend/getWikiHostForDomain?domain=found.wikibase.cloud')
->assertStatus(200)
->assertHeader('x-backend-host', $expectedHosts['backend'])
->assertHeader('x-web-host', $expectedHosts['web'])
->assertHeader('x-api-host', $expectedHosts['api'])
->assertHeader('x-alpha-host', $expectedHosts['alpha'])
->assertJson([
'backend-host' => $expectedHosts['backend'],
'web-host' => $expectedHosts['web'],
'api-host' => $expectedHosts['api'],
'alpha-host' => $expectedHosts['alpha'],
'domain' => 'found.wikibase.cloud',
]);
}
}
Loading