Router Setup

Purpose

The Router Setup subtopic addresses the organization and definition of API routers and their associated routes for the REST endpoints exposed by the system. It serves as the foundational layer that maps incoming HTTP requests to their corresponding handlers, which are implemented as controllers within the broader REST API server infrastructure described in the parent topic REST API and Web Launchers. This setup is essential for enabling structured, maintainable, and scalable HTTP routing using the popular gorilla/mux package in Go.

While the parent topic outlines the overall REST API server and web launcher functionalities, Router Setup specifically focuses on how the routing layer is constructed and integrated, defining the modular routers and their RESTful endpoint patterns. This subtopic ensures that different API domains such as runtime execution, session management, artifact handling, applications, and debugging are cleanly separated and routed to their respective controllers.

Functionality

The core functionality of the Router Setup involves:

This subtopic organizes routes by API concern, for example:

The use of gorilla/mux enables expressive route patterns with variable extraction, method-based routing, and middleware chaining.

Key Code Snippet

Excerpt from runtime.go demonstrates the pattern of route definition for the runtime API router:

func (r *RuntimeAPIRouter) Routes() Routes {
	return Routes{
		Route{
			Name:        "RunAgent",
			Methods:     []string{http.MethodPost, http.MethodOptions},
			Pattern:     "/run",
			HandlerFunc: controllers.NewErrorHandler(r.runtimeController.RunHandler),
		},
		Route{
			Name:        "RunAgentSse",
			Methods:     []string{http.MethodPost, http.MethodOptions},
			Pattern:     "/run_sse",
			HandlerFunc: controllers.NewErrorHandler(r.runtimeController.RunSSEHandler),
		},
	}
}

Here, two POST endpoints /run and /run_sse are declared with handlers wrapped in a generic error handler. Similar patterns are used for other routers, each encapsulating their domain logic.

Integration

The Router Setup acts as the glue between HTTP requests and the internal REST API controllers described in the sibling subtopic REST API Controllers. Each router binds to a specific controller responsible for implementing the business logic of the API endpoints.

This setup complements other subtopics under REST API and Web Launchers by:

The routing definitions ensure that incoming API calls reach the correct handler methods, which in turn rely on services like session management (Session Management), artifact storage (Artifact Management), and agent execution (Agent Execution Runner) to fulfill requests.

Diagram

classDiagram
class Route {
+string Name
+[]string Methods
+string Pattern
+http.HandlerFunc HandlerFunc
}
class Router {
+Routes() Routes
}
class RuntimeAPIRouter {
-RuntimeAPIController runtimeController
+Routes() Routes
}
class SessionsAPIRouter {
-SessionsAPIController sessionController
+Routes() Routes
}
class ArtifactsAPIRouter {
-ArtifactsAPIController artifactsController
+Routes() Routes
}
class AppsAPIRouter {
-AppsAPIController appsController
+Routes() Routes
}
class DebugAPIRouter {
-DebugAPIController debugController
+Routes() Routes
}
class mux.Router
Router <|-- RuntimeAPIRouter
Router <|-- SessionsAPIRouter
Router <|-- ArtifactsAPIRouter
Router <|-- AppsAPIRouter
Router <|-- DebugAPIRouter
RuntimeAPIRouter "1" *-- "1" RuntimeAPIController
SessionsAPIRouter "1" *-- "1" SessionsAPIController
ArtifactsAPIRouter "1" *-- "1" ArtifactsAPIController
AppsAPIRouter "1" *-- "1" AppsAPIController
DebugAPIRouter "1" *-- "1" DebugAPIController
NewRouter o-- mux.Router : creates
mux.Router o-- Route : contains

This class diagram illustrates the structure of routers as types implementing a common interface, each holding a controller instance and defining their route sets. The top-level mux.Router aggregates these routes into a unified HTTP router used by the REST API server.