Overview
THorse is the main class of the Horse web framework. It extends THorseProvider (which extends THorseCore) and provides all routing, middleware, and server control functionality.
type
THorse = class(THorseProvider);
Routing Methods
Get
Register a route that responds to GET requests.
class function Get(const APath: string; const ACallback: THorseCallback): THorseCore;
class function Get(const APath: string; const ACallback: THorseCallbackRequestResponse): THorseCore;
class function Get(const APath: string; const ACallback: THorseCallbackRequest): THorseCore;
class function Get(const APath: string; const ACallback: THorseCallbackResponse): THorseCore; // Delphi only
The route path (e.g., ‘/users’, ‘/products/:id’)
ACallback
THorseCallback | THorseCallbackRequestResponse | THorseCallbackRequest | THorseCallbackResponse
required
The callback function to execute when the route is matched
Returns the Horse instance for method chaining
Example:
THorse.Get('/users',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('List of users');
end);
// With route parameters
THorse.Get('/users/:id',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('User ID: ' + Req.Params['id']);
end);
Post
Register a route that responds to POST requests.
class function Post(const APath: string; const ACallback: THorseCallback): THorseCore;
class function Post(const APath: string; const ACallback: THorseCallbackRequestResponse): THorseCore;
class function Post(const APath: string; const ACallback: THorseCallbackRequest): THorseCore;
class function Post(const APath: string; const ACallback: THorseCallbackResponse): THorseCore; // Delphi only
ACallback
THorseCallback | THorseCallbackRequestResponse | THorseCallbackRequest | THorseCallbackResponse
required
The callback function to execute
Returns the Horse instance for method chaining
Example:
THorse.Post('/users',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
// Create new user with Req.Body
Res.Status(THTTPStatus.Created).Send('User created');
end);
Put
Register a route that responds to PUT requests.
class function Put(const APath: string; const ACallback: THorseCallback): THorseCore;
class function Put(const APath: string; const ACallback: THorseCallbackRequestResponse): THorseCore;
class function Put(const APath: string; const ACallback: THorseCallbackRequest): THorseCore;
class function Put(const APath: string; const ACallback: THorseCallbackResponse): THorseCore; // Delphi only
ACallback
THorseCallback | THorseCallbackRequestResponse | THorseCallbackRequest | THorseCallbackResponse
required
The callback function to execute
Returns the Horse instance for method chaining
Example:
THorse.Put('/users/:id',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
// Update user with ID from Req.Params['id']
Res.Send('User updated');
end);
Delete
Register a route that responds to DELETE requests.
class function Delete(const APath: string; const ACallback: THorseCallback): THorseCore;
class function Delete(const APath: string; const ACallback: THorseCallbackRequestResponse): THorseCore;
class function Delete(const APath: string; const ACallback: THorseCallbackRequest): THorseCore;
class function Delete(const APath: string; const ACallback: THorseCallbackResponse): THorseCore; // Delphi only
ACallback
THorseCallback | THorseCallbackRequestResponse | THorseCallbackRequest | THorseCallbackResponse
required
The callback function to execute
Returns the Horse instance for method chaining
Example:
THorse.Delete('/users/:id',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
// Delete user with ID from Req.Params['id']
Res.Status(THTTPStatus.NoContent).Send('');
end);
Patch
Register a route that responds to PATCH requests.
class function Patch(const APath: string; const ACallback: THorseCallback): THorseCore;
class function Patch(const APath: string; const ACallback: THorseCallbackRequestResponse): THorseCore;
class function Patch(const APath: string; const ACallback: THorseCallbackRequest): THorseCore;
class function Patch(const APath: string; const ACallback: THorseCallbackResponse): THorseCore; // Delphi only
ACallback
THorseCallback | THorseCallbackRequestResponse | THorseCallbackRequest | THorseCallbackResponse
required
The callback function to execute
Returns the Horse instance for method chaining
Example:
THorse.Patch('/users/:id',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
// Partially update user
Res.Send('User patched');
end);
Head
Register a route that responds to HEAD requests.
class function Head(const APath: string; const ACallback: THorseCallback): THorseCore;
class function Head(const APath: string; const ACallback: THorseCallbackRequestResponse): THorseCore;
class function Head(const APath: string; const ACallback: THorseCallbackRequest): THorseCore;
class function Head(const APath: string; const ACallback: THorseCallbackResponse): THorseCore; // Delphi only
ACallback
THorseCallback | THorseCallbackRequestResponse | THorseCallbackRequest | THorseCallbackResponse
required
The callback function to execute
Returns the Horse instance for method chaining
Example:
THorse.Head('/users',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Status(THTTPStatus.Ok);
end);
All
Register a route that responds to all HTTP methods.
class function All(const APath: string; const ACallback: THorseCallback): THorseCore;
class function All(const APath: string; const ACallback: THorseCallbackRequestResponse): THorseCore;
class function All(const APath: string; const ACallback: THorseCallbackRequest): THorseCore;
class function All(const APath: string; const ACallback: THorseCallbackResponse): THorseCore; // Delphi only
ACallback
THorseCallback | THorseCallbackRequestResponse | THorseCallbackRequest | THorseCallbackResponse
required
The callback function to execute
Returns the Horse instance for method chaining
Example:
THorse.All('/status',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('Server is running');
end);
Middleware Methods
Use
Register middleware that executes for all routes or routes matching a specific path.
class function Use(const ACallback: THorseCallback): THorseCore;
class function Use(const APath: string; const ACallback: THorseCallback): THorseCore;
class function Use(const ACallbacks: array of THorseCallback): THorseCore;
class function Use(const APath: string; const ACallbacks: array of THorseCallback): THorseCore;
Optional path prefix to apply middleware to. If not provided, middleware applies to all routes.
The middleware callback function
ACallbacks
array of THorseCallback
required
Array of middleware callbacks to register
Returns the Horse instance for method chaining
Example:
// Global middleware
THorse.Use(
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Writeln('Request received: ' + Req.PathInfo);
Next; // Call next middleware/route
end);
// Path-specific middleware
THorse.Use('/api',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
// Middleware only for /api/* routes
Next;
end);
// Multiple middlewares
THorse.Use([Middleware1, Middleware2, Middleware3]);
AddCallback
Add a callback to a collection that will be registered with the next route definition.
class function AddCallback(const ACallback: THorseCallback): THorseCore;
The callback function to add
Returns the Horse instance for method chaining
Example:
THorse
.AddCallback(AuthMiddleware)
.AddCallback(LoggingMiddleware)
.Get('/protected', GetProtectedResource);
AddCallbacks
Add multiple callbacks to a collection that will be registered with the next route definition.
class function AddCallbacks(const ACallbacks: TArray<THorseCallback>): THorseCore;
ACallbacks
TArray<THorseCallback>
required
Array of callback functions to add
Returns the Horse instance for method chaining
Example:
var
Middlewares: TArray<THorseCallback>;
begin
SetLength(Middlewares, 2);
Middlewares[0] := AuthMiddleware;
Middlewares[1] := LoggingMiddleware;
THorse
.AddCallbacks(Middlewares)
.Get('/protected', GetProtectedResource);
end;
Grouping and Routing
Route
Create a route builder for advanced route configuration.
class function Route(const APath: string): IHorseCoreRoute<THorseCore>;
Result
IHorseCoreRoute<THorseCore>
Returns a route builder interface
Example:
THorse.Route('/users')
.Get(
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('Get users');
end)
.Post(
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('Create user');
end);
Group
Create a group builder for organizing related routes.
class function Group: IHorseCoreGroup<THorseCore>;
Result
IHorseCoreGroup<THorseCore>
Returns a group builder interface
Example:
THorse.Group
.Prefix('/api/v1')
.Get('/users',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('Users');
end)
.Get('/products',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('Products');
end);
Server Control Methods
Listen
Start the HTTP server and begin listening for requests.
class procedure Listen; overload;
class procedure Listen(const APort: Integer; const AHost: string = '0.0.0.0'; const ACallbackListen: TProc = nil; const ACallbackStopListen: TProc = nil); overload;
class procedure Listen(const APort: Integer; const ACallbackListen: TProc; const ACallbackStopListen: TProc = nil); overload;
class procedure Listen(const AHost: string; const ACallbackListen: TProc = nil; const ACallbackStopListen: TProc = nil); overload;
class procedure Listen(const ACallbackListen: TProc; const ACallbackStopListen: TProc = nil); overload;
The port number to listen on (default: 9000)
The host address to bind to (default: ‘0.0.0.0’)
Optional callback executed when server starts listening
Optional callback executed when server stops listening
Example:
// Simple listen on default port 9000
THorse.Listen;
// Listen on specific port
THorse.Listen(8080);
// Listen with callback
THorse.Listen(9000,
procedure
begin
Writeln('Server is running on port 9000');
end);
// Listen on specific host and port
THorse.Listen(8080, '127.0.0.1',
procedure
begin
Writeln('Server started on 127.0.0.1:8080');
end,
procedure
begin
Writeln('Server stopped');
end);
StopListen
Stop the HTTP server.
class procedure StopListen;
Example:
Server Configuration Properties
Port
Get or set the server port.
class property Port: Integer read GetPort write SetPort;
Example:
THorse.Port := 8080;
THorse.Listen;
Host
Get or set the server host address.
class property Host: string read GetHost write SetHost;
Example:
THorse.Host := '127.0.0.1';
THorse.Listen;
MaxConnections
Get or set the maximum number of simultaneous connections.
class property MaxConnections: Integer read GetMaxConnections write SetMaxConnections;
Example:
THorse.MaxConnections := 1000;
THorse.Listen;
ListenQueue
Get or set the listen queue size (pending connections backlog).
class property ListenQueue: Integer read GetListenQueue write SetListenQueue;
Example:
THorse.ListenQueue := 50;
THorse.Listen;
KeepConnectionAlive
Get or set whether to keep connections alive after requests.
class property KeepConnectionAlive: Boolean read GetKeepConnectionAlive write SetKeepConnectionAlive;
Example:
THorse.KeepConnectionAlive := True;
THorse.Listen;
IsRunning
Check whether the Horse server is currently running.
class function IsRunning: Boolean;
Returns True if the server is running, False otherwise
Example:
if THorse.IsRunning then
Writeln('Server is running')
else
Writeln('Server is stopped');
Module Methods
ToModule
Convert the current Horse instance to a module that can be mounted in another Horse application.
class function ToModule: THorseModule;
Returns a THorseModule that can be used in another Horse instance
Example:
var
App, AdminModule: THorse;
begin
AdminModule := THorse.Create;
AdminModule.Get('/users', GetUsers);
AdminModule.Get('/settings', GetSettings);
App := THorse.Create;
App.Use('/admin', AdminModule.ToModule);
App.Listen(9000);
end;
Utility Methods
Version
Get the current Horse framework version.
class function Version: string;
Returns the version string
Example:
Writeln('Horse version: ' + THorse.Version);
See Also