Skip to content

Commit 855a8bb

Browse files
authored
Merge pull request #20 from wbstack/de/backend-api-query
Mediawiki backend routing
2 parents e3ddd87 + 95546e3 commit 855a8bb

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

public_html/php/WbstackMagnusOauth.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class WbstackMagnusOauth {
44

55
public const platformIngressHostAndPort = "platform-nginx.default.svc.cluster.local:8080";
6+
public const platformApiBackendHost = "api-app-backend.default.svc.cluster.local";
67

78
/**
89
* @var bool
@@ -29,6 +30,36 @@ class WbstackMagnusOauth {
2930
*/
3031
private static $callbackUrlTail;
3132

33+
public static function getVersionedMediawikiBackendHost() {
34+
$host = getenv('PLATFORM_MW_BACKEND_HOST'); // default fallback
35+
36+
$requestUrl = 'http://'
37+
.self::platformApiBackendHost
38+
.'/backend/ingress/getWikiVersionForDomain?domain='
39+
.$_SERVER['SERVER_NAME'];
40+
41+
$headers = get_headers($requestUrl, true);
42+
43+
if (is_array($headers)) {
44+
if (isset($headers['x-version'])) {
45+
$wikiVersion = $headers['x-version'];
46+
47+
// mapping like in https://github.com/wmde/wbaas-deploy/blob/main/k8s/helmfile/env/local/platform-nginx.nginx.conf#L4
48+
// TODO https://phabricator.wikimedia.org/T409078
49+
switch ($wikiVersion) {
50+
case 'mw1.39-wbs1':
51+
$host = 'mediawiki-139-app-backend.default.svc.cluster.local';
52+
break;
53+
case 'mw1.43-wbs1':
54+
$host = 'mediawiki-143-app-backend.default.svc.cluster.local';
55+
break;
56+
}
57+
}
58+
}
59+
60+
return $host;
61+
}
62+
3263
/**
3364
* Calling this means that oauth.php will just do the right thing in terms of wbstack.
3465
* Not calling this before MW_OAuth is used in a magnus tool will result in an error.
@@ -104,7 +135,7 @@ public static function parse_ini_file($filename) {
104135
'callbackUrlTail' => self::$callbackUrlTail,
105136
];
106137

107-
$client = curl_init('http://' . getenv( 'PLATFORM_MW_BACKEND_HOST' ) . '/w/api.php?action=wbstackPlatformOauthGet&format=json');
138+
$client = curl_init('http://' . self::getVersionedMediawikiBackendHost() . '/w/api.php?action=wbstackPlatformOauthGet&format=json');
108139
curl_setopt($client, CURLOPT_HTTPHEADER, $headers);
109140
curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - " . self::$consumerName . " - WbstackMagnusOauth::parse_ini_file" );
110141
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);

0 commit comments

Comments
 (0)