Directory Plugin

The Directory plugin provides path-based routing and conditional plugin execution, allowing different plugin pipelines to handle different URL paths.

Overview

The Directory plugin acts as a sophisticated router that executes nested plugins only when requests match configured directory patterns. This enables you to create complex routing scenarios where different parts of your website have different functionality, security requirements, or processing pipelines.

Unlike simple URL matching, the Directory plugin creates isolated execution contexts for different paths, allowing you to compose complex plugin pipelines that only activate for specific sections of your site.

Key Features

Configuration

To add the Directory plugin to your pipeline:

<td itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/DirectoryPlugin">
    <div class="plugin-config">
        <div class="config-property">
            <label class="property-label">library:</label>
            <span itemprop="library" class="property-value">file://./plugins/libdirectory.so</span>
        </div>
        <div class="config-property">
            <label class="property-label">directory:</label>
            <span itemprop="directory" class="property-value">/admin</span>
        </div>
        <div itemprop="nested_plugins" itemscope itemtype="https://rustybeam.net/schema/PluginConfig">
            <!-- Nested plugin configurations go here -->
        </div>
    </div>
</td>

Configuration Parameters

Parameter Type Required Default Description
directory String No / The path prefix to match (e.g., "/admin", "/api")
nested_plugins JSON Array No [] Array of plugin configurations to execute for matching paths
Important: The nested_plugins parameter expects a JSON array due to FFI limitations. The main server serializes the microdata plugin configurations to JSON before passing them to the directory plugin.

Path Matching Rules

The Directory plugin uses the following rules for path matching:

Execution Flow

  1. Request Phase:
  2. First Response Wins:
  3. Response Phase:

Use Cases

Admin Interface Protection

<td itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/DirectoryPlugin">
    <span itemprop="directory">/admin</span>
    <div itemprop="nested_plugins">
        <!-- Basic Auth plugin -->
        <!-- Authorization plugin -->
        <!-- Admin file handler -->
    </div>
</td>

API Versioning

<!-- API v1 -->
<td itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/DirectoryPlugin">
    <span itemprop="directory">/api/v1</span>
    <div itemprop="nested_plugins">
        <!-- v1 API handler plugins -->
    </div>
</td>

<!-- API v2 -->
<td itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/DirectoryPlugin">
    <span itemprop="directory">/api/v2</span>
    <div itemprop="nested_plugins">
        <!-- v2 API handler plugins -->
    </div>
</td>

Static vs Dynamic Content

<!-- Static assets with compression -->
<td itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/DirectoryPlugin">
    <span itemprop="directory">/static</span>
    <div itemprop="nested_plugins">
        <!-- Compression plugin -->
        <!-- Cache control plugin -->
        <!-- File handler plugin -->
    </div>
</td>

Nested Plugin Configuration

Each nested plugin in the nested_plugins array requires:

Example Nested Plugin JSON

[
    {
        "library": "file://./plugins/librusty_beam_basic_auth.so",
        "config": {
            "realm": "Admin Area",
            "authfile": "./admin-users.html"
        }
    },
    {
        "library": "file://./plugins/librusty_beam_file_handler.so",
        "config": {
            "root": "./admin-files"
        }
    }
]

Security Considerations

Security Warning: The Directory plugin loads dynamic libraries at runtime. Ensure all plugin libraries are from trusted sources and have appropriate file permissions.

Performance Considerations

Debugging

Enable verbose logging to see directory plugin behavior:

cargo run -- -v config/config.html

The plugin logs:

Common Log Messages

[DirectoryPlugin] Path '/users' does not match directory '/admin'
[DirectoryPlugin] Path '/admin/users' matches directory '/admin', executing 3 nested plugins
[DirectoryPlugin] Nested plugin 'basic-auth' (index 0) handled request
[DirectoryPlugin] Failed to load nested plugin at index 2: file://./plugins/missing.so

Troubleshooting

Issue Possible Cause Solution
Nested plugins not executing Path doesn't match directory pattern Check path matching rules and trailing slashes
Plugin loading failures Library file not found or invalid Verify library paths and file permissions
Unexpected plugin execution order Multiple directory plugins with overlapping paths Order directory plugins from most to least specific
Response modifications not applied Plugin only handles request phase Ensure plugins implement handle_response method

Best Practices

See Also