sessions.go

Overview

The sessions.go file defines the SessionsAPIRouter struct and its associated methods to set up HTTP routing for session-related REST API endpoints. It acts as a liaison between incoming HTTP requests targeting session management operations and the corresponding controller handlers implemented in the SessionsAPIController. This router encapsulates the route declarations for session CRUD (Create, Read, Delete) operations, including listing sessions and creating sessions either with or without an explicit session ID.

The file contributes to the modular routing infrastructure described in Router Setup and supports session lifecycle management as part of the REST API layer outlined in REST API and Web Launchers.


Structure and Components

SessionsAPIRouter

type SessionsAPIRouter struct {
	sessionController *controllers.SessionsAPIController
}

NewSessionsAPIRouter

func NewSessionsAPIRouter(controller *controllers.SessionsAPIController) *SessionsAPIRouter {
	return &SessionsAPIRouter{sessionController: controller}
}

Routes Method

func (r *SessionsAPIRouter) Routes() Routes

Interaction with Other Components


Implementation Details and Algorithms


Usage Example

Assuming a SessionsAPIController instance has been created, the router can be instantiated and integrated into the HTTP server setup:

import (
	"net/http"
	"google.golang.org/adk/server/adkrest/controllers"
	"google.golang.org/adk/server/adkrest/internal/routers"
)

func main() {
	sessionController := controllers.NewSessionsAPIController(sessionService)
	sessionsRouter := routers.NewSessionsAPIRouter(sessionController)

	routes := sessionsRouter.Routes()
	mainMux := http.NewServeMux()

	for _, route := range routes {
		mainMux.HandleFunc(route.Pattern, route.HandlerFunc)
	}

	http.ListenAndServe(":8080", mainMux)
}

This example demonstrates how SessionsAPIRouter provides the route definitions and handlers which can then be registered with the HTTP multiplexer.


Visual Diagram

classDiagram
class SessionsAPIRouter {
-sessionController: SessionsAPIController
+NewSessionsAPIRouter()
+Routes()
}
class SessionsAPIController {
+GetSessionHandler()
+CreateSessionHandler()
+DeleteSessionHandler()
+ListSessionsHandler()
}
SessionsAPIRouter "1" *-- "1" SessionsAPIController
SessionsAPIRouter : Routes() --> Route
class Route {
+Name
+Methods
+Pattern
+HandlerFunc
}
SessionsAPIRouter ..> Route : returns

Summary of Routes and Their Handlers

Route Name

HTTP Method(s)

URL Pattern

Controller Handler

GetSession

GET

/apps/{app_name}/users/{user_id}/sessions/{session_id}

GetSessionHandler

CreateSession

POST

/apps/{app_name}/users/{user_id}/sessions

CreateSessionHandler

CreateSessionWithId

POST

/apps/{app_name}/users/{user_id}/sessions/{session_id}

CreateSessionHandler

DeleteSession

DELETE, OPTIONS

/apps/{app_name}/users/{user_id}/sessions/{session_id}

DeleteSessionHandler

ListSessions

GET

/apps/{app_name}/users/{user_id}/sessions

ListSessionsHandler

These routes provide full CRUD capabilities on session resources scoped by application and user.


This file is a core component of the session management REST API routing, enabling modular and maintainable HTTP endpoint definitions, which integrate tightly with session lifecycle controllers and the broader REST API server infrastructure.