What We’re Building
We’ll create a plugin calledrequestinfo that:
- Adds custom headers to HTTP responses
- Logs request details (method, path, client IP)
- Supports Caddyfile configuration
- Implements the full module lifecycle
Prerequisites
- Go 1.25.0 or newer
- Basic understanding of Go and HTTP
- Familiarity with Caddy configuration
Step 1: Create the Module Structure
Create a new directory for your plugin:requestinfo.go:
Step 2: Define the Module Structure
Based on the pattern frommodules/caddyhttp/staticresp.go:88, define your handler:
Step 3: Implement the Module Interface
ImplementCaddyModule() from modules.go:54:
The module ID
http.handlers.requestinfo places this in the HTTP handlers namespace.Step 4: Implement the Lifecycle Methods
Provision
ImplementProvision() based on modules/caddyhttp/tracing/module.go:48:
Validate
Step 5: Implement the HTTP Handler
ImplementServeHTTP() to handle requests:
Step 6: Add Caddyfile Support
ImplementUnmarshalCaddyfile() based on modules/caddyhttp/staticresp.go:141:
Step 7: Register the Module
Add registration in theinit() function from modules/caddyhttp/tracing/module.go:15:
Step 8: Add Interface Guards
Add compile-time interface checks:Step 9: Build with xcaddy
Install xcaddy:Step 10: Test Your Plugin
JSON Configuration
Createcaddy.json:
Caddyfile Configuration
Create aCaddyfile:
Run and Test
- The custom header in the response
- Request logs in the Caddy output (if
log_enabledis true)
Complete Code
Next Steps
Module Development
Deep dive into module architecture
Testing Guide
Learn how to test your plugins
xcaddy Tool
Master the xcaddy build tool
Contributing
Share your plugin with the community