Route Groups

Learn how to organize and structure your routes using RouteMQ's route groups, enabling shared prefixes, middleware, and logical organization of related endpoints.

Overview

Route groups allow you to organize related routes under a common prefix and apply shared middleware, reducing code duplication and improving maintainability.

from core.router import Router
from app.controllers.device_controller import DeviceController
from app.middleware.auth_middleware import AuthMiddleware

router = Router()

# Group routes with common prefix and middleware
with router.group(prefix="devices", middleware=[AuthMiddleware()]) as devices:
    devices.on("status/{device_id}", DeviceController.handle_status)
    devices.on("config/{device_id}", DeviceController.handle_config)
    devices.on("commands/{device_id}", DeviceController.handle_commands)

Basic Group Syntax

Simple Prefix Grouping

The resulting MQTT topics become:

  • api/v1/users/list

  • api/v1/users/create

  • api/v1/users/{user_id} (matches api/v1/users/123, etc.)

Shared Middleware

Applying Middleware to Groups

Middleware Execution Order

Middleware executes in the order specified in the list:

Feature-based Organization

IoT Device Management System

API Versioning with Groups

Group Configuration Options

Shared Subscription Settings

QoS Settings per Group

Environment-based Grouping

Best Practices

Logical Organization

  • Group by domain/feature area (users, devices, analytics)

  • Use consistent naming conventions across groups

  • Keep related functionality together

Middleware Strategy

Performance Considerations

  • Group high-volume routes together for optimized middleware

  • Use shared subscriptions for groups with heavy traffic

  • Apply rate limiting at the group level for quota management

  • Consider QoS requirements when grouping routes

Security Boundaries

  • Group routes by authentication requirements

  • Apply authorization middleware at appropriate group levels

  • Separate public and private API groups

  • Use different middleware stacks for different security zones

Next Steps

Last updated