web.go

Overview

The web.go file implements a web server launcher that facilitates running the ADK (Agent Development Kit) via an HTTP web server. This launcher supports modular extensions called sublaunchers, allowing different web-based functionalities (such as REST APIs, web UIs, or custom protocol handlers) to coexist and be configured through command-line flags.

This file provides a framework to:

It acts as the core component of the Web Server Launcher described in the [REST API and Web Launchers](80564) topic, enabling a flexible and extensible web server infrastructure for agent interaction.


Key Entities

webConfig struct

Holds configuration parameters for the web server instance.

Field

Type

Description

port

int

TCP port on localhost where the server listens

writeTimeout

time.Duration

Timeout for writing HTTP response

readTimeout

time.Duration

Timeout for reading HTTP request body

idleTimeout

time.Duration

Timeout for idle keep-alive connections


webLauncher struct

The main launcher type that implements the launcher.SubLauncher interface to run the web server.

Field

Type

Description

flags

*flag.FlagSet

Flag set for parsing web server related CLI flags

config

*webConfig

Web server configuration

sublaunchers

[]Sublauncher

List of all available sublaunchers

activeSublaunchers

map[string]Sublauncher

Map of activated sublaunchers keyed by their keyword


Sublauncher interface

Defines the contract for extensions that add functionality to the web server. Each sublauncher can:

type Sublauncher interface {
    Keyword() string
    Parse(args []string) ([]string, error)
    CommandLineSyntax() string
    SimpleDescription() string
    SetupSubrouters(router *mux.Router, config *launcher.Config) error
    UserMessage(webURL string, printer func(v ...any))
}

This interface enables modular web components that integrate seamlessly with the base web server.


Functions and Methods

NewLauncher(sublaunchers ...Sublauncher) launcher.SubLauncher

Creates and returns a new webLauncher instance configured with the provided sublaunchers.

Usage Example:

apiLauncher := api.NewLauncher()
webUILauncher := webui.NewLauncher()
launcher := web.NewLauncher(apiLauncher, webUILauncher)

(*webLauncher) Execute(ctx context.Context, config *launcher.Config, args []string) error

Implements the launcher.Launcher interface.

Returns any parsing or runtime errors.


(*webLauncher) Parse(args []string) ([]string, error)

Parses the command-line arguments:

  1. Parses web server flags from args.

  2. Builds a map from sublauncher keywords to sublauncher instances.

  3. Iterates over remaining args to detect sublauncher keywords.

  4. For each detected keyword:

    • Ensures uniqueness (no duplicates).

    • Delegates parsing of that sublauncher's flags.

    • Marks the sublauncher as active.

  5. Returns any unparsed arguments that do not belong to web launcher or its sublaunchers.

Errors if:


(*webLauncher) Run(ctx context.Context, config *launcher.Config) error

Starts the HTTP server with active sublaunchers:

  1. Ensures config.SessionService is set (defaults to in-memory session service).

  2. Builds the base HTTP router (BuildBaseRouter).

  3. Validates that at least one sublauncher is active.

  4. Invokes SetupSubrouters on each active sublauncher to register routes.

  5. Logs server startup details.

  6. Starts an http.Server with configured timeouts and the composed router.

  7. Blocks serving HTTP requests until the server terminates or encounters an error.

Returns any errors encountered during setup or server startup.


(*webLauncher) CommandLineSyntax() string

Builds and returns a formatted string describing:

Useful for displaying help information on the command line.


(*webLauncher) Keyword() string

Returns the fixed keyword "web" used to identify this launcher.


(*webLauncher) SimpleDescription() string

Provides a short description:
"starts web server with additional sub-servers specified by sublaunchers"


logger(inner http.Handler) http.Handler

HTTP middleware function that logs each request's method, URI, and processing duration.

Used to enhance observability of HTTP traffic.


BuildBaseRouter() *mux.Router

Creates and returns a new mux.Router configured with:

This router is the base router onto which sublaunchers register their routes.


Important Implementation Details


Interaction with Other System Components


Visual Diagram: Web Server Launcher Structure and Workflow

flowchart TD
A[Command Line Input] --> B[Parse Web Flags]
B --> C[Identify Sublaunchers]
C --> D[Parse Sublauncher Flags]
D --> E[Build Base Router]
E --> F[Setup Sublaunchers' Routes]
F --> G[Start HTTP Server]
G --> H[Handle HTTP Requests]
H --> I[Log Requests]
classDef launcher fill:#f9f,stroke:#333,stroke-width:1px;
class A,B,C,D,E,F,G,H,I launcher;

Summary of Core Usage Pattern

  1. Create a webLauncher via NewLauncher() with desired sublaunchers.

  2. Call Execute() or Parse() followed by Run() to start the web server.

  3. The launcher parses flags, activates sublaunchers, builds routes, and runs the HTTP server.

  4. Sublaunchers extend functionality by adding their own CLI flags and HTTP handlers.

  5. Middleware logs requests, and user messages are printed on server startup.


Example Usage Outline

func main() {
    apiLauncher := api.NewLauncher()
    webUILauncher := webui.NewLauncher()

    webL := web.NewLauncher(apiLauncher, webUILauncher)

    err := webL.Execute(context.Background(), &launcher.Config{}, os.Args[1:])
    if err != nil {
        log.Fatalf("Failed to start web server: %v", err)
    }
}

This example shows how the web launcher can be composed with REST API and Web UI sublaunchers to provide a full-featured HTTP server.


This documentation describes the design, components, and workflows encapsulated in the web.go file, which implements the foundational Web Server Launcher functionality within the [REST API and Web Launchers](80564) topic.