routers.go

Overview

This file defines the routing infrastructure for the ADK-Web REST API by declaring HTTP routes and assembling them into a top-level router. It provides abstractions for describing API routes, interfaces for route providers, and utility functions for creating and populating a Gorilla mux router with these routes. This setup is integral to directing incoming HTTP requests to the appropriate handler functions implemented elsewhere in the system, enabling modular and scalable HTTP routing.

The primary purpose is to facilitate the registration and aggregation of multiple API routers (each responsible for a domain of REST endpoints) into a single HTTP router instance used by the web server. It leverages the github.com/gorilla/mux package to support expressive route definitions including HTTP methods, URL patterns, and handler functions.

This routing mechanism is a core component of the broader Router Setup subtopic within the REST API and Web Launchers topic, which organizes REST API endpoints into modular subrouters that connect HTTP requests to controller handlers.


Detailed Explanation of Code Components

Type: Route

type Route struct {
	Name        string
	Methods     []string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

Type: Routes

type Routes []Route

Interface: Router

type Router interface {
	Routes() Routes
}

Function: NewRouter

func NewRouter(routers ...Router) *mux.Router {
	router := mux.NewRouter().StrictSlash(true)
	SetupSubRouters(router)
	return router
}

Function: SetupSubRouters

func SetupSubRouters(router *mux.Router, subrouters ...Router) {
	for _, api := range subrouters {
		for _, route := range api.Routes() {
			var handler http.Handler = route.HandlerFunc

			router.
				Methods(route.Methods...).
				Path(route.Pattern).
				Name(route.Name).
				Handler(handler)
		}
	}
}

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Router Structure and Workflow

classDiagram
class Route {
+Name: string
+Methods: []string
+Pattern: string
+HandlerFunc: http.HandlerFunc
}
class Routes {
<<slice>>
}
class Router {
+Routes() Routes
}
class mux_Router {
+StrictSlash()
+Methods()
+Path()
+Name()
+Handler()
}
class routers_go {
+NewRouter(routers ...Router) *mux.Router
+SetupSubRouters(router *mux.Router, subrouters ...Router)
}
Router <|.. RuntimeAPIRouter
Router <|.. SessionsAPIRouter
Router <|.. ArtifactsAPIRouter
routers_go --> mux_Router : creates and configures
mux_Router "1" o-- "*" Route : contains
routers_go ..> Router : uses

Summary

The routers.go file provides the core routing abstractions and setup functions for the REST API server by defining Route structures, a Router interface, and methods to create and populate a Gorilla mux router. This supports modular, scalable HTTP routing essential for mapping REST API endpoints to their handlers, linking the HTTP layer to the underlying controller logic described in the REST API and Web Launchers topic and its Router Setup subtopic.