Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

New UI: Use URL from window.location (#4281)

Renato Haeberlifb7377cc11c2
diff --git a/changelog/unreleased/PR#4281-use-of-current-url-in-new-ui.yml b/changelog/unreleased/PR#4281-use-of-current-url-in-new-ui.yml
new file mode 100644
index 00000000000..b7736d261f8
--- /dev/null
+++ b/changelog/unreleased/PR#4281-use-of-current-url-in-new-ui.yml
@@ -0,0 +1,10 @@
+title: using window.location.origin rather than hard coded URL
+type: fixed
+authors:
+ - name: Renato Haeberli
+
+links:
+ - name: PR#4281
+ url: https://github.com/apache/solr/pull/4281
+
+
diff --git a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/components/start/store/StartStore.kt b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/components/start/store/StartStore.kt
index b1d39632ade..3b1e85fd298 100644
--- a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/components/start/store/StartStore.kt
+++ b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/components/start/store/StartStore.kt
@@ -23,6 +23,7 @@ import org.apache.solr.ui.components.start.store.StartStore.Intent
import org.apache.solr.ui.components.start.store.StartStore.Label
import org.apache.solr.ui.components.start.store.StartStore.State
import org.apache.solr.ui.domain.AuthMethod
+import org.apache.solr.ui.utils.defaultSolrUrl
/**
* State store interface of the start screen.
diff --git a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/components/start/store/StartStoreProvider.kt b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/components/start/store/StartStoreProvider.kt
index 74709cdfa21..f5dea9c7e1c 100644
--- a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/components/start/store/StartStoreProvider.kt
+++ b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/components/start/store/StartStoreProvider.kt
@@ -33,7 +33,7 @@ import org.apache.solr.ui.components.start.store.StartStore.Intent
import org.apache.solr.ui.components.start.store.StartStore.Label
import org.apache.solr.ui.components.start.store.StartStore.State
import org.apache.solr.ui.errors.UnauthorizedException
-import org.apache.solr.ui.utils.DEFAULT_SOLR_URL
+import org.apache.solr.ui.utils.defaultSolrUrl
import org.apache.solr.ui.utils.parseError
/**
@@ -83,7 +83,7 @@ internal class StartStoreProvider(
is Intent.UpdateSolrUrl -> dispatch(Message.UrlUpdated(intent.url))
is Intent.Connect -> {
var urlValue = state().url
- if (urlValue == "") urlValue = DEFAULT_SOLR_URL // use placeholder value if empty
+ if (urlValue.isBlank()) urlValue = defaultSolrUrl()
try {
val url = parseUrl(urlValue) ?: throw URLParserException(
diff --git a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/utils/Constants.kt b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/utils/Constants.kt
index 8b2b9f8f3dc..820ed2498b6 100644
--- a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/utils/Constants.kt
+++ b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/utils/Constants.kt
@@ -21,3 +21,10 @@ package org.apache.solr.ui.utils
* The default Solr URL that may be used in various places.
*/
const val DEFAULT_SOLR_URL = "http://127.0.0.1:8983/"
+
+/**
+ * Returns the default Solr URL for the current platform. On web, this is derived from
+ * the browser's current origin so the URL is always same-origin and avoids CORS issues.
+ * On desktop, falls back to [DEFAULT_SOLR_URL].
+ */
+expect fun defaultSolrUrl(): String
diff --git a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/utils/HttpClientUtils.kt b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/utils/HttpClientUtils.kt
index 928e9e7bdec..7d62408585d 100644
--- a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/utils/HttpClientUtils.kt
+++ b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/utils/HttpClientUtils.kt
@@ -36,7 +36,7 @@ import org.apache.solr.ui.domain.AuthOption
* URL.
*/
fun getDefaultClient(
- url: Url = Url("http://127.0.0.1:8983/"),
+ url: Url = Url(DEFAULT_SOLR_URL),
block: HttpClientConfig<*>.() -> Unit = {},
) = HttpClient {
defaultRequest {
@@ -74,7 +74,7 @@ fun getHttpClientWithAuthOption(option: AuthOption) = when (option) {
fun getHttpClientWithCredentials(
username: String,
password: String,
- url: Url = Url("http://127.0.0.1:8983/"),
+ url: Url = Url(DEFAULT_SOLR_URL),
realm: String? = null,
) = getDefaultClient(url) {
install(Auth) {
@@ -91,7 +91,7 @@ fun getHttpClientWithCredentials(
fun getHttpClientWithBearerTokens(
accessToken: String,
refreshToken: String? = null,
- url: Url = Url("http://127.0.0.1:8983/"),
+ url: Url = Url(DEFAULT_SOLR_URL),
realm: String? = null,
) = getDefaultClient(url) {
install(Auth) {
diff --git a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/views/start/StartContent.kt b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/views/start/StartContent.kt
index ad08effaa21..8c593171a43 100644
--- a/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/views/start/StartContent.kt
+++ b/solr/ui/src/commonMain/kotlin/org/apache/solr/ui/views/start/StartContent.kt
@@ -44,7 +44,7 @@ import org.apache.solr.ui.generated.resources.connecting
import org.apache.solr.ui.generated.resources.desc_to_get_started
import org.apache.solr.ui.generated.resources.solr_sun
import org.apache.solr.ui.generated.resources.title_welcome_to_solr
-import org.apache.solr.ui.utils.DEFAULT_SOLR_URL
+import org.apache.solr.ui.utils.defaultSolrUrl
import org.apache.solr.ui.views.components.SolrButton
import org.apache.solr.ui.views.components.SolrCard
import org.apache.solr.ui.views.components.SolrLinearProgressIndicator
@@ -101,7 +101,7 @@ fun StartContent(
value = model.url,
singleLine = true,
onValueChange = component::onSolrUrlChange,
- placeholder = { Text(text = DEFAULT_SOLR_URL) },
+ placeholder = { Text(text = defaultSolrUrl()) },
enabled = !model.isConnecting,
supportingText = {
model.error?.let {
diff --git a/solr/ui/src/desktopMain/kotlin/org/apache/solr/ui/utils/PlatformUtils.kt b/solr/ui/src/desktopMain/kotlin/org/apache/solr/ui/utils/PlatformUtils.kt
new file mode 100644
index 00000000000..903512f61e8
--- /dev/null
+++ b/solr/ui/src/desktopMain/kotlin/org/apache/solr/ui/utils/PlatformUtils.kt
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.ui.utils
+
+actual fun defaultSolrUrl(): String = DEFAULT_SOLR_URL
diff --git a/solr/ui/src/wasmJsMain/kotlin/org/apache/solr/ui/Main.kt b/solr/ui/src/wasmJsMain/kotlin/org/apache/solr/ui/Main.kt
index a224111b4e6..dd6a54f4eb3 100644
--- a/solr/ui/src/wasmJsMain/kotlin/org/apache/solr/ui/Main.kt
+++ b/solr/ui/src/wasmJsMain/kotlin/org/apache/solr/ui/Main.kt
@@ -34,6 +34,7 @@ import kotlinx.coroutines.Dispatchers
import org.apache.solr.ui.components.root.RootComponent
import org.apache.solr.ui.components.root.integration.SimpleRootComponent
import org.apache.solr.ui.utils.DefaultAppComponentContext
+import org.apache.solr.ui.utils.defaultSolrUrl
import org.apache.solr.ui.utils.getDefaultClient
import org.apache.solr.ui.views.root.RootContent
import org.apache.solr.ui.views.theme.SolrTheme
@@ -68,8 +69,7 @@ fun main() {
return
}
- // TODO Set default request url to values from window location
- val httpClient = getDefaultClient()
+ val httpClient = getDefaultClient(url = Url(defaultSolrUrl()))
val component: RootComponent = SimpleRootComponent(
componentContext = componentContext,
diff --git a/solr/ui/src/wasmJsMain/kotlin/org/apache/solr/ui/utils/PlatformUtils.kt b/solr/ui/src/wasmJsMain/kotlin/org/apache/solr/ui/utils/PlatformUtils.kt
new file mode 100644
index 00000000000..6ed853a1a4e
--- /dev/null
+++ b/solr/ui/src/wasmJsMain/kotlin/org/apache/solr/ui/utils/PlatformUtils.kt
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.ui.utils
+
+import kotlinx.browser.window
+
+actual fun defaultSolrUrl(): String = window.location.origin + "/"