api.go

Overview

The api.go source file defines a sublauncher component that integrates the ADK REST API into the web server framework under the /api/ URL path. This sublauncher provides HTTP endpoints for interacting with AI agents and related services via the ADK REST API. It handles command-line parsing for configuration, sets up HTTP routing with CORS support, and exposes the API handler to serve client requests.

This file is part of the broader API and A2A Sublaunchers and REST API and Web Launchers topics, focusing on enabling REST API access to the agent framework within a modular web server launcher architecture.


Types and Their Functionality

apiConfig

type apiConfig struct {
	frontendAddress string
}

apiLauncher

type apiLauncher struct {
	flags  *flag.FlagSet
	config *apiConfig
}

Functions and Methods

NewLauncher() weblauncher.Sublauncher

func NewLauncher() weblauncher.Sublauncher
launcher := api.NewLauncher()

(a *apiLauncher) Keyword() string

func (a *apiLauncher) Keyword() string

(a *apiLauncher) SimpleDescription() string

func (a *apiLauncher) SimpleDescription() string

(a *apiLauncher) CommandLineSyntax() string

func (a *apiLauncher) CommandLineSyntax() string

(a *apiLauncher) Parse(args []string) ([]string, error)

func (a *apiLauncher) Parse(args []string) ([]string, error)

(a *apiLauncher) UserMessage(webURL string, printer func(v ...any))

func (a *apiLauncher) UserMessage(webURL string, printer func(v ...any))

(a *apiLauncher) SetupSubrouters(router *mux.Router, config *launcher.Config) error

func (a *apiLauncher) SetupSubrouters(router *mux.Router, config *launcher.Config) error

corsWithArgs(frontendAddress string) func(next http.Handler) http.Handler

func corsWithArgs(frontendAddress string) func(next http.Handler) http.Handler

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: apiLauncher Structure and Workflow

classDiagram
class apiLauncher {
-flags: FlagSet
-config: apiConfig
+Keyword()
+SimpleDescription()
+CommandLineSyntax()
+Parse(args)
+UserMessage(webURL, printer)
+SetupSubrouters(router, config)
}
class apiConfig {
-frontendAddress: string
}
apiLauncher --> apiConfig : contains

Workflow Flowchart of Request Handling and Setup

flowchart TD
Start[Launch apiLauncher]
ParseFlags[Parse Command-line Flags]
SetupRouter[SetupSubrouters]
CreateAPIHandler[adkrest.NewHandler]
WrapCORS[CORS Middleware]
RegisterRoute[Register /api/ route]
HTTPRequest[Incoming HTTP Request /api/...]
StripPrefix[Strip /api prefix]
CORSHandler[CORS Middleware Handler]
APIHandler[ADK REST API Handler]
Start --> ParseFlags --> SetupRouter
SetupRouter --> CreateAPIHandler --> WrapCORS --> RegisterRoute
RegisterRoute --> HTTPRequest --> StripPrefix --> CORSHandler --> APIHandler

Usage Example

To use the API sublauncher within the web server launcher:

./adk_launcher api -webui_address=localhost:8080

This will:


References to Related Topics


This file plays a crucial role in bridging HTTP REST API requests from clients (such as the ADK Web UI) to the underlying agent framework, providing a configured, routable, and secure API surface within the web server launcher environment.