Skip to main content

Overview

The Horse framework provides a collection of officially maintained middleware packages to extend functionality. These middlewares are maintained by the Horse core team and provide essential features for web applications.

Available Middlewares

horse/json (Jhonson)

JSON serialization and deserialization middleware. Features:
  • Automatic JSON parsing for request bodies
  • Automatic JSON serialization for response objects
  • Support for custom serialization rules
Installation:
boss install jhonson
Usage:
uses
  Horse,
  Horse.Jhonson;

begin
  THorse.Use(Jhonson);
  
  THorse.Post('/users',
    procedure(Req: THorseRequest; Res: THorseResponse)
    var
      User: TUser;
    begin
      User := Req.Body<TUser>;
      // User object automatically deserialized from JSON
      Res.Send<TUser>(User); // Automatically serialized to JSON
    end);
    
  THorse.Listen(9000);
end.

horse/basic-auth

HTTP Basic Authentication middleware. Features:
  • Simple username/password authentication
  • Custom validation callback support
  • Configurable realm
Installation:
boss install horse-basic-auth
Usage:
uses
  Horse,
  Horse.BasicAuthentication;

begin
  THorse.Use(HorseBasicAuthentication(
    function(const AUsername, APassword: string): Boolean
    begin
      Result := (AUsername = 'admin') and (APassword = 'password');
    end));
    
  THorse.Get('/protected',
    procedure(Req: THorseRequest; Res: THorseResponse)
    begin
      Res.Send('Access granted');
    end);
    
  THorse.Listen(9000);
end.

horse/cors

Cross-Origin Resource Sharing (CORS) middleware. Features:
  • Configure allowed origins
  • Configure allowed methods
  • Configure allowed headers
  • Support for credentials
Installation:
boss install horse-cors
Usage:
uses
  Horse,
  Horse.CORS;

begin
  THorse.Use(CORS);
  
  // Or with custom configuration
  THorse.Use(CORS
    .AllowedOrigin('*')
    .AllowedMethods('GET,POST,PUT,DELETE')
    .AllowedHeaders('Content-Type'));
    
  THorse.Listen(9000);
end.

horse/stream

Octet stream middleware for binary data handling. Features:
  • Handle binary request data
  • Send binary response data
  • Stream processing support
Installation:
boss install horse-octet-stream
Usage:
uses
  Horse,
  Horse.OctetStream;

begin
  THorse.Use(OctetStream);
  
  THorse.Post('/upload',
    procedure(Req: THorseRequest; Res: THorseResponse)
    var
      Stream: TStream;
    begin
      Stream := Req.Body<TStream>;
      // Process binary stream
      Res.Send('Upload complete');
    end);
    
  THorse.Listen(9000);
end.

horse/jwt

JSON Web Token (JWT) authentication middleware. Features:
  • JWT token validation
  • Token generation helpers
  • Configurable secret key
  • Custom claims support
Installation:
boss install horse-jwt
Usage:
uses
  Horse,
  Horse.JWT,
  JOSE.Core.JWT;

begin
  THorse.Post('/login',
    procedure(Req: THorseRequest; Res: THorseResponse)
    var
      Token: string;
    begin
      // Validate credentials
      Token := HorseJWT.CreateToken('my-secret', 'username');
      Res.Send(Token);
    end);
    
  THorse.Use('/api', HorseJWT('my-secret'));
  
  THorse.Get('/api/protected',
    procedure(Req: THorseRequest; Res: THorseResponse)
    begin
      Res.Send('Protected resource');
    end);
    
  THorse.Listen(9000);
end.

horse/exception

Exception handling middleware. Features:
  • Global exception handling
  • Custom error responses
  • Exception logging
  • Development/production modes
Installation:
boss install handle-exception
Usage:
uses
  Horse,
  Horse.HandleException;

begin
  THorse.Use(HandleException);
  
  THorse.Get('/error',
    procedure(Req: THorseRequest; Res: THorseResponse)
    begin
      raise Exception.Create('Something went wrong');
    end);
    
  THorse.Listen(9000);
end.

horse/logger

Request/response logging middleware. Features:
  • Request logging
  • Response logging
  • Configurable log formats
  • File and console output
Installation:
boss install horse-logger
Usage:
uses
  Horse,
  Horse.Logger;

begin
  THorse.Use(HorseLogger);
  
  THorse.Get('/test',
    procedure(Req: THorseRequest; Res: THorseResponse)
    begin
      Res.Send('Test');
    end);
    
  THorse.Listen(9000);
end.

horse/compression

Response compression middleware (gzip, deflate). Features:
  • Gzip compression
  • Deflate compression
  • Configurable compression threshold
  • Content-type filtering
Installation:
boss install horse-compression
Usage:
uses
  Horse,
  Horse.Compression;

begin
  THorse.Use(Compression);
  
  THorse.Get('/large-data',
    procedure(Req: THorseRequest; Res: THorseResponse)
    begin
      Res.Send('Large response data here...');
      // Automatically compressed if client supports it
    end);
    
  THorse.Listen(9000);
end.

Compatibility Matrix

MiddlewareDelphiLazarus
horse/json✔️✔️
horse/basic-auth✔️✔️
horse/cors✔️✔️
horse/stream✔️✔️
horse/jwt✔️✔️
horse/exception✔️✔️
horse/logger✔️✔️
horse/compression✔️✔️

See Also

Build docs developers (and LLMs) love