Skip to content

Commit

Permalink
Horse 3.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciussanchez committed Feb 1, 2023
1 parent 85ec94a commit b0ffdf1
Show file tree
Hide file tree
Showing 11 changed files with 822 additions and 127 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
### Delphi ###
# Uncomment these types if you want even more clean repository. But be careful.
# It can make harm to an existing project source. Read explanations below.
#

# Resource files are binaries containing manifest, project icon and version info.
# They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files.
#*.res
*.res

# Type library file (binary). In old Delphi versions it should be stored.
# Since Delphi 2009 it is produced from .ridl file and can safely be ignored.
#*.tlb
Expand Down
21 changes: 6 additions & 15 deletions Demo/Demo.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@ program Demo;
{$R *.res}

uses
System.SysUtils,
Horse,
Horse.Loggastic in '..\src\Horse.Loggastic.pas',
Providers.Log in '..\src\Providers\Providers.Log.pas';

var
App: THorse;

begin
App := THorse.Create;
try
//See output on https://ptsv2.com/t/39fiw-1573504844
App.Use(Loggastic('https://ptsv2.com/t/39fiw-1573504844/post'));
//See output on https://ptsv2.com/t/39fiw-1573504844
THorse.Use(Loggastic('https://ptsv2.com/t/39fiw-1573504844/post'));

App.Get('/', procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
THorse.Get('/',
procedure(Res: THorseResponse)
begin
Res.Send('Hello')
Res.Send('Hello');
end);

App.Start;

finally
App.Free;
end;
THorse.Listen;
end.
345 changes: 329 additions & 16 deletions Demo/Demo.dproj

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Demo/boss-lock.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"hash": "d41d8cd98f00b204e9800998ecf8427e",
"updated": "2019-11-11T17:37:12.2273971-03:00",
"updated": "2023-01-31T22:31:07.4074165-03:00",
"installedModules": {
"github.com/hashload/horse": {
"name": "horse",
"version": "1.7.0",
"hash": "7c3b15d6289c0ab49f483626c61da28b",
"version": "3.0.2",
"hash": "929cb417c573af469c997a7e1fda5dbb",
"artifacts": {},
"failed": false,
"changed": false
Expand Down
4 changes: 2 additions & 2 deletions Demo/boss.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"description": "",
"version": "1.0.0",
"homepage": "",
"mainsrc": "./",
"mainsrc": "./src",
"projects": [],
"dependencies": {
"github.com/hashload/horse": "^1.6.8"
"github.com/hashload/horse": "^3.0.2"
}
}
458 changes: 437 additions & 21 deletions Loggastic.dproj

Large diffs are not rendered by default.

Binary file removed Loggastic.res
Binary file not shown.
8 changes: 4 additions & 4 deletions boss-lock.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"hash": "8368f70e5346a12a93a069a04324d392",
"updated": "2019-11-11T17:31:43.7062285-03:00",
"hash": "d41d8cd98f00b204e9800998ecf8427e",
"updated": "2023-01-31T22:19:01.1604569-03:00",
"installedModules": {
"github.com/hashload/horse": {
"name": "horse",
"version": "1.7.0",
"hash": "7c3b15d6289c0ab49f483626c61da28b",
"version": "3.0.2",
"hash": "929cb417c573af469c997a7e1fda5dbb",
"artifacts": {},
"failed": false,
"changed": false
Expand Down
6 changes: 3 additions & 3 deletions boss.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "Loggastic",
"name": "loggastic",
"description": "",
"version": "1.0.0",
"homepage": "",
"mainsrc": "src/",
"mainsrc": "./src",
"projects": [],
"dependencies": {
"github.com/HashLoad/horse": "^v1.6.3"
"github.com/hashload/horse": "^3.0.2"
}
}
6 changes: 2 additions & 4 deletions src/Horse.Loggastic.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

interface

uses
Horse, Providers.Log;
uses Horse, Providers.Log;

function Loggastic(AElasticSearchUrl: string): THorseCallback;

implementation

uses
System.SysUtils;
uses System.SysUtils;

function Loggastic(AElasticSearchUrl: string): THorseCallback;
begin
Expand Down
90 changes: 33 additions & 57 deletions src/Providers/Providers.Log.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

interface

uses
System.JSON, REST.JSON, System.Net.HTTPClientComponent, System.Threading, System.SysUtils, System.DateUtils, Horse,
uses System.JSON, REST.JSON, System.Net.HTTPClientComponent, System.Threading, System.SysUtils, System.DateUtils, Horse,
REST.Client, REST.Types, System.Generics.Collections;

const
DATE_HEADER = '_@DATE';

type
TProviderLogResponse = class
private
FBody: string;
FDate: TDateTime;
FReasonString: string;
FStatusCode: integer;
FStatusCode: Integer;
FContentType: string;
FContentLength: integer;
FContentLength: Integer;
public
property Date: TDateTime read FDate write FDate;
property ReasonString: string read FReasonString write FReasonString;
property StatusCode: integer read FStatusCode write FStatusCode;
property StatusCode: Integer read FStatusCode write FStatusCode;
property ContentType: string read FContentType write FContentType;
property ContentLength: integer read FContentLength write FContentLength;
property ContentLength: Integer read FContentLength write FContentLength;
property Body: string read FBody write FBody;

function ToJSON: TJSONObject;

constructor Create(const AResponse: THorseResponse); overload;
constructor Create(AStatusCode: integer; AError, ADescription: string); overload;
constructor Create(AStatusCode: Integer; AError, ADescription: string); overload;
end;

TProviderLogRequest = class
Expand All @@ -38,19 +36,16 @@ TProviderLogRequest = class
FContentType: string;
FParams: TJSONObject;
FBody: string;
FContentLength: integer;

FContentLength: Integer;
function DictionaryToJsonObject(ADictionary: TDictionary<string, string>): TJSONObject;
public
property Date: TDateTime read FDate write FDate;
property Method: string read FMethod write FMethod;
property ContentType: string read FContentType write FContentType;
property ContentLength: integer read FContentLength write FContentLength;
property ContentLength: Integer read FContentLength write FContentLength;
property Params: TJSONObject read FParams write FParams;
property Body: string read FBody write FBody;

function ToJSON: TJSONObject;

constructor Create(const ARequest: THorseRequest; AStartDate: TDateTime);
end;

Expand All @@ -60,15 +55,13 @@ TProviderLogGeneral = class
FServerHost: string;
FPathInfo: string;
FSession: TJSONObject;

procedure SetSession(const ARequest: THorseRequest);
public
property BasePath: string read FBasePath write FBasePath;
property ServerHost: string read FServerHost write FServerHost;
property PathInfo: string read FPathInfo write FPathInfo;
property Session: TJSONObject read FSession write FSession;
function ToJSON: TJSONObject;

constructor Create(const ARequest: THorseRequest);
end;

Expand All @@ -85,23 +78,19 @@ TProviderLog = class
property General: TProviderLogGeneral read FGeneral write FGeneral;
property Request: TProviderLogRequest read FRequest write FRequest;
property Response: TProviderLogResponse read FResponse write FResponse;

procedure SendLog;
function ToJSON: TJSONObject;

constructor Create(const ARequest: THorseRequest; const AResponse: THorseResponse; AStartDate: TDateTime); overload;
constructor Create(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string; AStartDate: TDateTime); overload;
destructor Destroy; override;
end;

procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AStartDate: TDateTime); overload;

procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string; AStartDate: TDateTime); overload;

implementation

uses System.NetEncoding, System.Classes, IdHTTPWebBrokerBridge,
IdHTTPHeaderInfo;
uses System.NetEncoding, System.Classes, IdHTTPWebBrokerBridge, IdHTTPHeaderInfo;

type
TIdHTTPAppRequestHelper = class helper for TIdHTTPAppRequest
Expand All @@ -110,7 +99,6 @@ TIdHTTPAppRequestHelper = class helper for TIdHTTPAppRequest
function GetHeadersJSON: TJSONObject;
end;


procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AStartDate: TDateTime);
var
LLog: TProviderLog;
Expand Down Expand Up @@ -148,8 +136,7 @@ constructor TProviderLog.Create(const ARequest: THorseRequest; const AResponse:
begin
FGeneral := TProviderLogGeneral.Create(ARequest);
FRequest := TProviderLogRequest.Create(ARequest, AStartDate);
FResponse := TProviderLogResponse.Create(AResponse.Status, AError, THorseHackResponse(AResponse)
.GetWebResponse.Content);
FResponse := TProviderLogResponse.Create(AResponse.Status, AError, AResponse.RawWebResponse.Content);
end;

destructor TProviderLog.Destroy;
Expand Down Expand Up @@ -203,6 +190,7 @@ procedure TProviderLog.SendLog;
except
end;
end);

LTask.Start;
end;

Expand All @@ -215,10 +203,10 @@ function TProviderLog.ToJSON: TJSONObject;
end;

procedure TProviderLogGeneral.SetSession(const ARequest: THorseRequest);
var
LPayloadEncoded, LPayloadDecoded, LToken: string;
const
JWT_PAYLOAD = 1;
var
LPayloadEncoded, LPayloadDecoded, LToken: string;
begin
LToken := ARequest.Headers['X-Authorization'];

Expand All @@ -245,36 +233,30 @@ function TProviderLogGeneral.ToJSON: TJSONObject;
{ TProviderLogGeneral }

constructor TProviderLogGeneral.Create(const ARequest: THorseRequest);
var
LHostRequest: THorseHackRequest;
begin
LHostRequest := THorseHackRequest(ARequest);
FBasePath := LHostRequest.GetWebRequest.PathInfo;
FServerHost := LHostRequest.GetWebRequest.Host;
FPathInfo := LHostRequest.GetWebRequest.PathInfo;
FServerHost := ARequest.RawWebRequest.Host;
FBasePath := ARequest.RawWebRequest.PathInfo;
FPathInfo := ARequest.RawWebRequest.PathInfo;
SetSession(ARequest);
end;

{ TProviderLogRequest }

constructor TProviderLogRequest.Create(const ARequest: THorseRequest; AStartDate: TDateTime);
var
LHackedRequest: THorseHackRequest;
begin
FDate := AStartDate;
FMethod := THorseHackRequest(ARequest).GetWebRequest.Method;
FContentType := THorseHackRequest(ARequest).GetWebRequest.ContentType;
LHackedRequest := THorseHackRequest(ARequest);
FMethod := ARequest.RawWebRequest.Method;
FContentType := ARequest.RawWebRequest.ContentType;
FParams := TJSONObject.Create;

FParams.AddPair('querys', DictionaryToJsonObject(ARequest.Query));
FParams.AddPair('params', DictionaryToJsonObject(ARequest.Params));
if LHackedRequest.GetWebRequest.inheritsfrom(TIdHTTPAppRequest) then
begin
FParams.AddPair('headers', TIdHTTPAppRequest(LHackedRequest.GetWebRequest).GetHeadersJSON);
end;
FParams.AddPair('querys', DictionaryToJsonObject(ARequest.Query.Dictionary));
FParams.AddPair('params', DictionaryToJsonObject(ARequest.Params.Dictionary));

if ARequest.RawWebRequest.InheritsFrom(TIdHTTPAppRequest) then
FParams.AddPair('headers', TIdHTTPAppRequest(ARequest.RawWebRequest).GetHeadersJSON);

if FContentType = 'application/json' then
FBody := THorseHackRequest(ARequest).Body;
FBody := ARequest.Body;

FContentLength := FBody.Length;
end;
Expand All @@ -285,9 +267,7 @@ function TProviderLogRequest.DictionaryToJsonObject(ADictionary: TDictionary<str
begin
Result := TJSONObject.Create;
for LPair in ADictionary do
begin
Result.AddPair(LPair.Key, LPair.Value)
end;
Result.AddPair(LPair.Key, LPair.Value);
end;

function TProviderLogRequest.ToJSON: TJSONObject;
Expand All @@ -304,23 +284,19 @@ function TProviderLogRequest.ToJSON: TJSONObject;
{ TProviderLogResponse }

constructor TProviderLogResponse.Create(const AResponse: THorseResponse);
var
LHostResponse: THorseHackResponse;
begin
LHostResponse := THorseHackResponse(AResponse);

FDate := Now;
FReasonString := LHostResponse.GetWebResponse.ReasonString;
FStatusCode := LHostResponse.GetWebResponse.StatusCode;
FContentType := LHostResponse.GetWebResponse.ContentType;
FReasonString := AResponse.RawWebResponse.ReasonString;
FStatusCode := AResponse.RawWebResponse.StatusCode;
FContentType := AResponse.RawWebResponse.ContentType;

if FContentType = 'application/json' then
FBody := LHostResponse.GetWebResponse.Content;
FBody := AResponse.RawWebResponse.Content;

FContentLength := LHostResponse.GetWebResponse.ContentLength;
FContentLength := AResponse.RawWebResponse.ContentLength;
end;

constructor TProviderLogResponse.Create(AStatusCode: integer; AError, ADescription: string);
constructor TProviderLogResponse.Create(AStatusCode: Integer; AError, ADescription: string);
var
LBody: TJSONObject;
begin
Expand Down

0 comments on commit b0ffdf1

Please sign in to comment.