Horse Writing Middleware
Developmenthorse-writing-middlewareHorse Writing Middleware is an Agent Skill for development published in the HashLoad/horse repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.
Guide to writing custom middlewares for the Horse framework, following the callback flow with the Next parameter.
QUICK START
How to use this skill
Bring this guide into your coding agent with a prompt tailored to the tool you use.
- Open your project in Codex.
- Copy and paste the prompt below.
- Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/HashLoad/horse/blob/HEAD/doc/skills/horse-writing-middleware/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/horse-writing-middleware, but wait for my approval before writing files or running scripts.
Copying this prompt does not install or run the skill. Review third-party files before use.
Writing Custom Horse Middlewares
Middleware Signature
A custom middleware is a procedure that receives THorseRequest, THorseResponse, and a callback procedure Next.
procedure MyCustomMiddleware(Req: THorseRequest; Res: THorseResponse; Next: TProc);
begin
// 1. Logic BEFORE the request reaches the endpoint handler
Req.Headers.AddOrSetValue('X-Processed-By', 'MyMiddleware');
// 2. Call Next to delegate execution to the subsequent middleware or route handler
Next;
// 3. Logic AFTER the route handler completes its execution
Res.Headers.AddOrSetValue('X-Execution-Finished', 'True');
end;
Terminating Execution Early
If a validation fails (e.g., authentication is missing), do NOT call Next. Set the status and send the response immediately. This halts the pipeline execution.
procedure AuthMiddleware(Req: THorseRequest; Res: THorseResponse; Next: TProc);
var
LToken: string;
begin
LToken := Req.Headers.Items['Authorization'];
if LToken.IsEmpty then
begin
// Halt execution by sending response and omitting the Next call
Res.Send('Unauthorized').Status(THTTPStatus.Unauthorized);
Exit;
end;
// Token is valid, proceed with pipeline
Next;
end;