-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3224ce
commit f17180f
Showing
2 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,86 @@ | ||
# horse-manipulate-request | ||
Middleware to manipulate Request in HORSE | ||
<b>horse-manipulate-request</b> is a middleware to manipulate request in APIs developed with the <a href="https://github.com/HashLoad/horse">Horse</a> framework. | ||
|
||
## ⚙️ Installation | ||
Installation is done using the [`boss install`](https://github.com/HashLoad/boss) command: | ||
``` sh | ||
boss install https://github.com/andre-djsystem/horse-manipulate-request | ||
``` | ||
If you choose to install manually, simply add the following folders to your project, in *Project > Options > Resource Compiler > Directories and Conditionals > Include file search path* | ||
``` | ||
../horse-manipulate-request/src | ||
``` | ||
|
||
## ✔️ Compatibility | ||
This middleware is compatible with projects developed in: | ||
- [X] Delphi | ||
- [X] Lazarus | ||
|
||
## ⚡️ Quickstart Delphi | ||
```delphi | ||
uses | ||
Horse, | ||
Horse.ManipulateRequest, // It's necessary to use the unit | ||
System.SysUtils; | ||
begin | ||
// It's necessary to add the middleware in the Horse: | ||
THorse.Use(ManipulateRequest( | ||
procedure(Req: THorseRequest); | ||
var | ||
Version: String = ''; | ||
begin | ||
Req.Headers.TryGetValue('x-appversion', Version); | ||
WriteLn(Format('Version: %s',[Version])); | ||
end;)); | ||
THorse.Get('/ping', | ||
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc) | ||
begin | ||
Res.Send('pong'); | ||
end); | ||
THorse.Listen(9000); | ||
end; | ||
``` | ||
|
||
## ⚡️ Quickstart Lazarus | ||
```delphi | ||
{$MODE DELPHI}{$H+} | ||
uses | ||
{$IFDEF UNIX}{$IFDEF UseCThreads} | ||
cthreads, | ||
{$ENDIF}{$ENDIF} | ||
Horse, | ||
Horse.ManipulateRequest, // It's necessary to use the unit | ||
SysUtils; | ||
procedure GetPing(Req: THorseRequest; Res: THorseResponse; Next: TNextProc); | ||
begin | ||
Res.Send('Pong'); | ||
end; | ||
procedure HandleRequest(Req: THorseRequest); | ||
var | ||
Version: String = ''; | ||
begin | ||
Req.Headers.TryGetValue('x-appversion', Version); | ||
WriteLn(Format('Version: %s',[Version])); | ||
end; | ||
begin | ||
// It's necessary to add the middleware in the Horse: | ||
THorse.Use(ManipulateRequest(HandleRequest)) | ||
THorse.Get('/ping', GetPing); | ||
THorse.Listen(9000); | ||
end. | ||
``` | ||
|
||
## ⚠️ License | ||
`horse-manipulate-request` is free and open-source middleware licensed under the [MIT License](https://github.com/andre-djsystem/horse-manipulate-request/blob/master/LICENSE). | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
unit Horse.ManipulateRequest; | ||
|
||
{$IF DEFINED(FPC)} | ||
{$MODE DELPHI}{$H+} | ||
{$ENDIF} | ||
|
||
interface | ||
|
||
uses | ||
{$IF DEFINED(FPC)} | ||
SysUtils, Classes, | ||
{$ELSE} | ||
System.SysUtils, System.Classes, | ||
{$ENDIF} | ||
Horse; | ||
|
||
type | ||
THorseManipulateRequest = {$IF NOT DEFINED(FPC)} reference to {$ENDIF} procedure(Req: THorseRequest); | ||
|
||
function ManipulateRequest(const AManipulateRequest: THorseManipulateRequest): THorseCallback; overload; | ||
procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)} TNextProc {$ELSE} TProc {$ENDIF}); | ||
|
||
implementation | ||
|
||
var | ||
ManipulateRequestCallBack: THorseManipulateRequest; | ||
|
||
function ManipulateRequest( | ||
const AManipulateRequest: THorseManipulateRequest): THorseCallback; | ||
begin | ||
ManipulateRequestCallBack := AManipulateRequest; | ||
Result := Middleware; | ||
end; | ||
|
||
procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: TNextProc); | ||
begin | ||
try | ||
ManipulateRequestCallBack(Req); | ||
except | ||
on E: exception do | ||
begin | ||
Res.Send(E.Message).Status(THTTPStatus.InternalServerError); | ||
raise EHorseCallbackInterrupted.Create; | ||
end; | ||
end; | ||
|
||
Next(); | ||
end; | ||
|
||
|
||
|
||
end. | ||
|
||
|