pub struct ServerBuilder { /* private fields */ }Expand description
Builder for constructing an ADK server with custom routes.
ServerBuilder allows registering additional Axum routers alongside the
built-in REST, A2A, and UI routes. Custom routes benefit from the same
middleware stack (auth, CORS, tracing, timeout, security headers) as the
built-in routes.
§Example
use adk_server::{ServerBuilder, ServerConfig};
use axum::{Router, routing::get};
let config = ServerConfig::new(agent, session_service);
let app = ServerBuilder::new(config)
.add_api_routes(
Router::new()
.route("/projects", get(list_projects))
.route("/projects/{id}", get(get_project))
)
.add_api_routes(
Router::new()
.route("/automations", get(list_automations))
)
.with_a2a("http://localhost:8080")
.build();
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
axum::serve(listener, app).await?;Implementations§
Source§impl ServerBuilder
impl ServerBuilder
Sourcepub fn new(config: ServerConfig) -> Self
pub fn new(config: ServerConfig) -> Self
Create a new server builder with the given configuration.
Sourcepub fn add_api_routes(self, routes: Router) -> Self
pub fn add_api_routes(self, routes: Router) -> Self
Sourcepub fn add_root_routes(self, routes: Router) -> Self
pub fn add_root_routes(self, routes: Router) -> Self
Add custom routes at the root level (not nested under /api).
These routes are merged at the top level of the application, alongside the UI and A2A routes. They receive the full middleware stack (CORS, tracing, timeout, security headers) but NOT the auth middleware.
Use this for routes that need their own auth handling or public endpoints.
Sourcepub fn with_a2a(self, base_url: impl Into<String>) -> Self
pub fn with_a2a(self, base_url: impl Into<String>) -> Self
Enable A2A protocol support at the specified base URL.
The base URL is used to construct the agent card’s endpoint URL.
Sourcepub fn enable_shutdown_endpoint(self) -> Self
pub fn enable_shutdown_endpoint(self) -> Self
Enable the POST /api/shutdown endpoint for graceful shutdown.
When enabled, the server exposes a shutdown endpoint that triggers
graceful shutdown: stops accepting new connections, completes in-flight
requests, and then exits. Use build_with_shutdown
to get the ShutdownHandle for wiring into axum::serve().with_graceful_shutdown().
The endpoint is protected by the auth middleware when a
RequestContextExtractor is configured.
Sourcepub fn build(self) -> Router
pub fn build(self) -> Router
Build the final Axum router with all routes and middleware applied.
Sourcepub fn build_with_shutdown(self) -> (Router, ShutdownHandle)
pub fn build_with_shutdown(self) -> (Router, ShutdownHandle)
Build the final Axum router and return a ShutdownHandle.
Use this when enable_shutdown_endpoint() is set.
Pass the handle’s signal to axum::serve().with_graceful_shutdown().
§Example
let (app, shutdown_handle) = ServerBuilder::new(config)
.enable_shutdown_endpoint()
.build_with_shutdown();
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_handle.signal())
.await?;