|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.web.controller; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import com.google.adk.web.config.DevUiAssets; |
| 22 | +import jakarta.servlet.http.HttpServletRequest; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.util.LinkedHashMap; |
| 26 | +import java.util.Map; |
| 27 | +import org.jspecify.annotations.Nullable; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.beans.factory.annotation.Value; |
| 32 | +import org.springframework.core.io.Resource; |
| 33 | +import org.springframework.core.io.ResourceLoader; |
| 34 | +import org.springframework.http.CacheControl; |
| 35 | +import org.springframework.http.MediaType; |
| 36 | +import org.springframework.http.ResponseEntity; |
| 37 | +import org.springframework.web.bind.annotation.GetMapping; |
| 38 | +import org.springframework.web.bind.annotation.RestController; |
| 39 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
| 40 | + |
| 41 | +/** |
| 42 | + * Serves the dev UI's runtime configuration, shadowing the copy bundled in the static assets so |
| 43 | + * {@code backendUrl} can name the address the browser actually reached this server on. The bundled |
| 44 | + * document is merged rather than replaced, so keys the UI gains in a later bundle survive. |
| 45 | + */ |
| 46 | +@RestController |
| 47 | +public class RuntimeConfigController { |
| 48 | + |
| 49 | + private static final Logger log = LoggerFactory.getLogger(RuntimeConfigController.class); |
| 50 | + |
| 51 | + private final ResourceLoader resourceLoader; |
| 52 | + private final ObjectMapper objectMapper; |
| 53 | + private final @Nullable String webUiDir; |
| 54 | + |
| 55 | + @Autowired |
| 56 | + public RuntimeConfigController( |
| 57 | + ResourceLoader resourceLoader, |
| 58 | + ObjectMapper objectMapper, |
| 59 | + @Value("${adk.web.ui.dir:#{null}}") @Nullable String webUiDir) { |
| 60 | + this.resourceLoader = resourceLoader; |
| 61 | + this.objectMapper = objectMapper; |
| 62 | + this.webUiDir = webUiDir; |
| 63 | + } |
| 64 | + |
| 65 | + @GetMapping( |
| 66 | + value = "/dev-ui/" + DevUiAssets.RUNTIME_CONFIG_PATH, |
| 67 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 68 | + public ResponseEntity<Map<String, Object>> runtimeConfig(HttpServletRequest request) { |
| 69 | + Map<String, Object> config = readBundledConfig(); |
| 70 | + config.put("backendUrl", backendUrlFor(request)); |
| 71 | + // The static handler this shadows is registered with setCachePeriod(0), and the value now |
| 72 | + // varies by request, so it must not be cached. |
| 73 | + return ResponseEntity.ok().cacheControl(CacheControl.noStore()).body(config); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Where the browser should send API calls: empty when this server is at the root, else the |
| 78 | + * absolute base URL it was reached on. Behind a proxy that strips a path prefix, {@code |
| 79 | + * ForwardedHeaderFilter} puts that prefix in the context path, so this recovers it without the |
| 80 | + * deployment configuring anything — provided {@code server.forward-headers-strategy=framework} is |
| 81 | + * set and the proxy sends {@code X-Forwarded-Prefix}. |
| 82 | + * |
| 83 | + * <p>Absolute rather than just the prefix because the UI reads a value without a scheme as the |
| 84 | + * host of its live/websocket connection. |
| 85 | + */ |
| 86 | + private static String backendUrlFor(HttpServletRequest request) { |
| 87 | + String contextPath = request.getContextPath(); |
| 88 | + if (contextPath == null || contextPath.isEmpty()) { |
| 89 | + return ""; |
| 90 | + } |
| 91 | + return ServletUriComponentsBuilder.fromContextPath(request).toUriString(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * The bundled config, or an empty document when it is absent or unreadable. A dev UI that cannot |
| 96 | + * read its own config is worse than one whose extra keys defaulted, so this never fails the |
| 97 | + * request. |
| 98 | + */ |
| 99 | + private Map<String, Object> readBundledConfig() { |
| 100 | + Resource resource = |
| 101 | + resourceLoader.getResource( |
| 102 | + DevUiAssets.assetLocation(webUiDir, DevUiAssets.RUNTIME_CONFIG_PATH)); |
| 103 | + if (!resource.exists()) { |
| 104 | + log.debug("No bundled dev UI runtime config at {}; serving backendUrl only.", resource); |
| 105 | + return new LinkedHashMap<>(); |
| 106 | + } |
| 107 | + try (InputStream in = resource.getInputStream()) { |
| 108 | + Map<String, Object> parsed = objectMapper.readValue(in, new TypeReference<>() {}); |
| 109 | + return parsed == null ? new LinkedHashMap<>() : new LinkedHashMap<>(parsed); |
| 110 | + } catch (IOException e) { |
| 111 | + log.warn("Could not read the bundled dev UI runtime config at {}.", resource, e); |
| 112 | + return new LinkedHashMap<>(); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments