webui.go
Overview
The webui.go file implements a Web UI Launcher as a sublauncher component that integrates an embedded Angular-based web user interface (UI) into the ADK web server. It serves the UI assets statically from within the executable binary at a configurable HTTP path prefix (default /ui/) and dynamically generates runtime configuration data (such as the backend REST API URL) for the UI to consume.
This launcher enables users to interact visually with the ADK REST API backend through a browser without requiring a separately deployed frontend server. It integrates seamlessly with the main HTTP router and the launcher framework, handling command-line flags, HTTP routing, static asset delivery, and user messages.
The file belongs to the broader REST API and Web Launchers system that exposes AI agents via HTTP interfaces, complementing REST API endpoints and other sublaunchers such as A2A.
Key Types
webUIConfig
type webUIConfig struct {
backendAddress string
pathPrefix string
}
Purpose: Holds configurable parameters for the Web UI launcher.
Fields:
backendAddress(string): The full URL of the ADK REST API server as accessible from the web browser.pathPrefix(string): The HTTP path prefix where the Web UI is served, e.g.,/ui/.
webUILauncher
type webUILauncher struct {
flags *flag.FlagSet
config *webUIConfig
}
Purpose: Implements the
weblauncher.Sublauncherinterface to launch and serve the ADK Web UI.Fields:
flags: Command-line flag set to parse Web UI specific flags.config: Configuration parameters encapsulated inwebUIConfig.
Functions and Methods
NewLauncher() weblauncher.Sublauncher
Description: Constructs and returns a new instance of
webUILauncherinitialized with default configuration and CLI flags.Details:
Creates a
flag.FlagSetnamed"webui"to parse flags.Defines the
api_server_addressflag for specifying the backend REST API URL, defaulting to"http://localhost:8080/api".Sets the default
pathPrefixto"/ui/".
Returns: A
weblauncher.Sublauncherinterface implemented by the newly createdwebUILauncher.Usage:
launcher := NewLauncher()
(w *webUILauncher) CommandLineSyntax() string
Implements:
weblauncher.SublauncherDescription: Returns a formatted string describing the command-line syntax and available flags for the Web UI launcher.
Uses the utility method
util.FormatFlagUsageto generate the usage text.Returns: A string suitable for display in help messages.
(w *webUILauncher) Keyword() string
Implements:
weblauncher.SublauncherDescription: Returns the command keyword
"webui"used to identify this sublauncher in the command-line interface.Returns:
"webui"
(w *webUILauncher) Parse(args []string) ([]string, error)
Implements:
weblauncher.SublauncherDescription: Parses command-line arguments relevant to the Web UI launcher.
Parameters:
args []string: Arguments slice to parse.
Returns:
Remaining unparsed arguments (
[]string).Error if parsing fails.
Details: Uses the internal
flag.FlagSetto parse flags and returns the leftover arguments.
(w *webUILauncher) SetupSubrouters(router *mux.Router, config *launcher.Config) error
Implements:
weblauncher.SublauncherDescription: Adds the Web UI subrouter to the provided main HTTP router.
Parameters:
router *mux.Router: The main HTTP router to augment.config *launcher.Config: Launcher configuration (not used directly here).
Returns:
error(alwaysnilin this implementation).Details: Calls
AddSubrouterwith configured path prefix and backend address to mount the Web UI routes.
(w *webUILauncher) AddSubrouter(router *mux.Router, pathPrefix, backendAddress string)
Description: Adds a subrouter to serve the embedded ADK Web UI and related endpoints.
Parameters:
router *mux.Router: The parent HTTP router.pathPrefix string: URL path prefix where the UI is served (e.g.,/ui/).backendAddress string: Backend REST API server URL to be embedded in runtime config.
Functionality:
Creates a subrouter for HTTP GET requests under
pathPrefix.Defines a dynamic endpoint
/assets/config/runtime-config.jsonthat returns JSON with thebackendUrlfield.Registers a redirect handler from
/to thepathPrefix.Serves static UI files embedded in the binary from the
distrdirectory via Go'sembedfilesystem.
Error Handling: If embedded UI files cannot be loaded, logs a fatal error terminating the program.
Usage Example:
w.AddSubrouter(router, "/ui/", "http://localhost:8080/api")
(w *webUILauncher) SimpleDescription() string
Implements:
weblauncher.SublauncherDescription: Returns a brief description of the Web UI launcher.
Returns:
"starts ADK Web UI server which provides UI for interacting with ADK REST API"
(w *webUILauncher) UserMessage(webURL string, printer func(v ...any))
Implements:
weblauncher.SublauncherDescription: Prints a user-friendly message indicating the URL where the Web UI can be accessed.
Parameters:
webURL string: Base URL of the web server.printer func(v ...any): Function to output messages (e.g.,fmt.Println).
Example Output:
webui: you can access API using http://localhost:8080/ui/
Embedded Static Assets
//go:embed distr/*
var content embed.FS
The
distr/directory contains the compiled Angular Web UI distribution files.These files are embedded at build time into the executable binary using Go 1.16+
embedpackage.The embedded filesystem
contentis used to serve static assets over HTTP.
Important Implementation Details
Dynamic Runtime Configuration Endpoint:
The launcher provides/ui/assets/config/runtime-config.jsonthat returns JSON with the backend REST API URL. This allows the UI to be fully functional without hardcoded backend paths, supporting flexible deployment environments.HTTP Redirect:
Requests to/(root) are redirected (HTTP 302) to the UI path prefix, ensuring users accessing the base URL are directed to the UI.Static File Serving:
Useshttp.FileServerwrapped withhttp.StripPrefixto serve the embedded UI files without exposing the full internal path structure.Command-Line Integration:
The Web UI launcher supports CLI flags independently from other sublaunchers, allowing configuration of backend API address and enabling composition with other launchers.Use of
gorilla/muxRouter:
The launcher adds its routes as a subrouter under the main router, enabling clean modular HTTP route management and coexistence with other handlers such as REST API.
Interaction with Other System Components
Web Server Launcher:
ThewebUILauncheris designed to be plugged into the broader web server launcher framework (Web Server Launcher), which manages sublaunchers, HTTP server lifecycle, and command-line parsing.REST API Backend:
The UI interacts primarily with the ADK REST API server, whose address is provided via CLI flag and exposed to the frontend through the dynamic runtime-config JSON endpoint.HTTP Routing Infrastructure:
Integrates with the common HTTP routing setup usinggorilla/mux(Router Setup), sharing the router instance.REST API and Web Launchers Topic:
As a component of the web launchers system, it complements API launchers and A2A launchers by providing a graphical frontend (REST API and Web Launchers, Web UI Launcher).Embedded Assets:
The static UI files are embedded at build time, allowing deployment of a single binary without external dependencies for the UI.
Usage Example
A typical usage scenario:
Create a Web UI launcher instance:
launcher := webui.NewLauncher()
Parse command-line arguments (e.g.,
-api_server_address).Setup HTTP server and add subrouters:
router := mux.NewRouter()
launcher.SetupSubrouters(router, nil)
Start HTTP server serving the router.
User accesses
http://<host>:<port>/ui/in a browser.The UI loads static assets from embedded files and obtains runtime configuration from
/ui/assets/config/runtime-config.json.UI communicates with the backend API at the specified
api_server_address.
Visual Diagram: Web UI Launcher HTTP Routing Flow
flowchart TD
User[User Browser]
Router[HTTP Router]
Redirect[Redirect Handler]
ConfigEndpoint[Runtime Config JSON]
StaticServer[Embedded UI File Server]
UIApp[Angular UI]
BackendAPI[ADK REST API Server]
User -->|GET / or /ui/*| Router
Router -->|Redirect / to /ui/| Redirect
Router -->|Serve /ui/assets/config/runtime-config.json| ConfigEndpoint
Router -->|Serve static files under /ui/| StaticServer
ConfigEndpoint --> UIApp
StaticServer --> UIApp
UIApp -->|REST API calls| BackendAPI
Explanation:
User requests to
/are redirected to/ui/.Requests to
/ui/assets/config/runtime-config.jsonprovide the UI with backend API URL.Static assets are served from embedded filesystem under
/ui/.The Angular UI loaded in the browser uses the backend API address to make REST calls.
References
gorilla/muxHTTP routing packageGo
embedpackage for embedding static files