-
Notifications
You must be signed in to change notification settings - Fork 3
Add getWikiHostsForDomain endpoint #1001
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
Changes from all commits
5f13a86
316b73e
b4af51d
46cea56
4b4eb9c
8ec6721
5f58b90
0e57f16
e2a5403
b94d2d6
6a3ce63
527282e
7a90ab3
6c7d2f5
d0eda48
864792e
87b3475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?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 MediaWikiHostsController extends Controller { | ||
| public function getWikiHostsForDomain(Request $request): \Illuminate\Http\JsonResponse { | ||
| $mediawikiHostResolver = new MediaWikiHostResolver; | ||
| $domain = $request->query('domain'); | ||
| try { | ||
| $hosts = $mediawikiHostResolver->getHostsForDomain($domain); | ||
| } catch (UnknownWikiDomainException $e) { | ||
| return response()->json(['error' => 'Domain not found.'], 404); | ||
| } catch (UnknownDBVersionException $e) { | ||
| return response()->json(['error' => 'Unknown database version.'], 500); | ||
AndrewKostka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return response() | ||
| ->json([ | ||
| 'domain' => $domain, | ||
| 'backend-host' => $hosts['backend'], | ||
| 'web-host' => $hosts['web'], | ||
| 'api-host' => $hosts['api'], | ||
| 'alpha-host' => $hosts['alpha'], | ||
| ]) | ||
| ->header('x-backend-host', $hosts['backend']) | ||
| ->header('x-web-host', $hosts['web']) | ||
| ->header('x-api-host', $hosts['api']) | ||
| ->header('x-alpha-host', $hosts['alpha']); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,8 +29,24 @@ class MediaWikiHostResolver { | |
| // return $this->getBackendHostForDomain($wiki->domain); | ||
| // } | ||
|
|
||
| /** | ||
| * @throws UnknownDBVersionException | ||
| * @throws UnknownWikiDomainException | ||
| */ | ||
| public function getHostsForDomain(string $domain): array { | ||
| $mwVersionForDomain = $this->getMwVersionForDomain($domain); | ||
|
|
||
| // TODO: Make hosts format configurable for flexibility | ||
| return [ | ||
| 'web' => sprintf('mediawiki-%s-app-web.default.svc.cluster.local', $mwVersionForDomain), | ||
| 'backend' => sprintf('mediawiki-%s-app-backend.default.svc.cluster.local', $mwVersionForDomain), | ||
| 'api' => sprintf('mediawiki-%s-app-api.default.svc.cluster.local', $mwVersionForDomain), | ||
| 'alpha' => sprintf('mediawiki-%s-app-alpha.default.svc.cluster.local', $mwVersionForDomain), | ||
| ]; | ||
| } | ||
|
|
||
| public function getBackendHostForDomain(string $domain): string { | ||
| // TODO: Move 'backend.default.svc.cluster.local' to an env variable (e.g. PLATFORM_MW_BACKEND_HOST_SUFFIX) for flexibility. | ||
| // TODO: Make host format configurable for flexibility | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually found the previous comment clearer but I am also fine with updating it as this now
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I changed it is because I don't actually know if |
||
| return sprintf('mediawiki-%s-app-backend.default.svc.cluster.local', $this->getMwVersionForDomain($domain)); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Routes\Backend; | ||
|
|
||
| use App\Wiki; | ||
| use App\WikiDb; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Tests\TestCase; | ||
|
|
||
| class MediaWikiHostsControllerTest extends TestCase { | ||
| use RefreshDatabase; | ||
|
|
||
| protected $route = '/backend/getWikiHostsForDomain'; | ||
|
|
||
| public function testSuccess() { | ||
| $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('test139.wikibase.cloud', 'mw1.39-wbs1'); | ||
| $this->createWiki('test143.wikibase.cloud', 'mw1.43-wbs1'); | ||
|
|
||
| $this->getJson("$this->route?domain=test143.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([ | ||
| 'domain' => 'test143.wikibase.cloud', | ||
| 'backend-host' => $expectedHosts['backend'], | ||
| 'web-host' => $expectedHosts['web'], | ||
| 'api-host' => $expectedHosts['api'], | ||
| 'alpha-host' => $expectedHosts['alpha'], | ||
| ]); | ||
| } | ||
|
|
||
| public function testDomainNotfound() { | ||
| $this->getJson("$this->route?domain=notfound.wikibase.cloud") | ||
| ->assertStatus(404); | ||
| } | ||
|
|
||
| public function testUnknownDbVersion() { | ||
| $this->createWiki('test.wikibase.cloud', 'unknownVersion'); | ||
|
|
||
| $this->getJson("$this->route?domain=test.wikibase.cloud") | ||
| ->assertStatus(500); | ||
| } | ||
|
|
||
| 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, | ||
| ]); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.