Skip to content

Commit c9050cd

Browse files
committed
feat:
- Capacidade de fazer For .. in no TRALRoutes - Possibilidade de criar rotas em runtime apenas com métodos não pertencentes a uma classe com o OnReplyGen - Possibilidade de atribuir métodos diferentes por verbo nas rotas - Ajuste no Swagger para que ele consiga tratar corretamente o novo modelo de rotas, sem prejudicar o funcionamento de sistemas já existentes
1 parent 022819c commit c9050cd

3 files changed

Lines changed: 69 additions & 7 deletions

File tree

src/base/RALRoutes.pas

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface
1111
type
1212
TRALRoutes = class;
1313
TRALOnReply = procedure(ARequest: TRALRequest; AResponse: TRALResponse) of object;
14+
TRALOnReplyGen = procedure(ARequest: TRALRequest; AResponse: TRALResponse);
1415

1516
// swagger defines
1617
// array, boolean, integer, number, object, string
@@ -23,7 +24,7 @@ TRALRouteParam = class(TCollectionItem)
2324
FDescription: TStrings;
2425
FParamName: StringRAL;
2526
FParamType: TRALRouteParamType;
26-
FRequired : boolean;
27+
FRequired: boolean;
2728
protected
2829
procedure AssignTo(Dest: TPersistent); override;
2930
function GetDisplayName: string; override;
@@ -44,7 +45,7 @@ TRALRouteParam = class(TCollectionItem)
4445
TRALRouteParams = class(TOwnedCollection)
4546
public
4647
constructor Create(AOwner: TPersistent);
47-
function IndexOf(AName : StringRAL) : IntegerRAL;
48+
function IndexOf(AName: StringRAL): IntegerRAL;
4849
end;
4950

5051
{ TRALBaseRoute }
@@ -56,13 +57,14 @@ TRALBaseRoute = class(TCollectionItem)
5657
FAllowURIParams: boolean;
5758
FCallback: boolean;
5859
FDescription: TStrings;
59-
FInputParams : TRALRouteParams;
60+
FInputParams: TRALRouteParams;
6061
FName: StringRAL;
6162
FRoute: StringRAL;
6263
FSkipAuthMethods: TRALMethods;
63-
FURIParams : TRALRouteParams;
64+
FURIParams: TRALRouteParams;
6465

6566
FOnReply: TRALOnReply;
67+
FOnReplyGen: TRALOnReplyGen;
6668
protected
6769
procedure AssignTo(Dest: TPersistent); override;
6870
function GetDisplayName: string; override;
@@ -95,6 +97,7 @@ TRALBaseRoute = class(TCollectionItem)
9597
property URIParams: TRALRouteParams read FURIParams write FURIParams;
9698

9799
property OnReply: TRALOnReply read FOnReply write FOnReply;
100+
property OnReplyGen: TRALOnReplyGen read FOnReplyGen write FOnReplyGen;
98101
published
99102
property Description: TStrings read FDescription write SetDescription;
100103
property InputParams: TRALRouteParams read FInputParams write FInputParams;
@@ -114,12 +117,26 @@ TRALRoute = class(TRALBaseRoute)
114117
property URIParams;
115118

116119
property OnReply;
120+
public
121+
property OnReplyGen;
117122
end;
118123

119124
{ TRALRoutes }
120125

121126
/// Collection class to store all route definitions
122127
TRALRoutes = class(TOwnedCollection)
128+
public type
129+
/// Support enumeration of values in TRALParams.
130+
TEnumerator = class
131+
private
132+
FIndex: Integer;
133+
FArray: TRALRoutes;
134+
public
135+
constructor Create(const AArray: TRALRoutes);
136+
function GetCurrent: TRALRoute; inline;
137+
function MoveNext: Boolean; inline;
138+
property Current: TRALRoute read GetCurrent;
139+
end;
123140
private
124141
function CompareRoutes(ARoute: TRALRoute; AQuery: StringRAL;
125142
var AWeight: IntegerRAL; AURI: TStringList): boolean;
@@ -128,7 +145,10 @@ TRALRoutes = class(TOwnedCollection)
128145
constructor Create(AOwner: TPersistent);
129146
/// Returns a list of routes separated by sLineBreak
130147
function AsString: StringRAL;
148+
/// Method that will check if the request finds a matching route
131149
function CanAnswerRoute(ARequest: TRALRequest): TRALRoute;
150+
/// Retuns the internal Enumerator type to allow for..in loops
151+
function GetEnumerator: TEnumerator; inline;
132152

133153
property Find[const ARoute: StringRAL]: TRALRoute read GetRoute;
134154
end;
@@ -170,6 +190,8 @@ procedure TRALBaseRoute.Execute(ARequest: TRALRequest; AResponse: TRALResponse);
170190

171191
if Assigned(OnReply) then
172192
OnReply(ARequest, AResponse)
193+
else if Assigned(OnReplyGen) then
194+
OnReplyGen(ARequest, AResponse)
173195
else
174196
AResponse.Answer(HTTP_NotFound);
175197
end;
@@ -448,6 +470,11 @@ function TRALRoutes.CompareRoutes(ARoute: TRALRoute; AQuery: StringRAL;
448470
end;
449471
end;
450472

473+
function TRALRoutes.GetEnumerator: TEnumerator;
474+
begin
475+
Result := TEnumerator.Create(Self);
476+
end;
477+
451478
function TRALRoutes.GetRoute(const ARoute: StringRAL): TRALRoute;
452479
var
453480
I: integer;
@@ -495,7 +522,8 @@ function TRALRoutes.CanAnswerRoute(ARequest: TRALRequest): TRALRoute;
495522
for vInt := 0 to Pred(Self.Count) do
496523
begin
497524
vRoute := TRALRoute(Items[vInt]);
498-
if CompareRoutes(vRoute, vQuery, vTempWeight, vTempUriRoute) then
525+
526+
if vRoute.IsMethodAllowed(ARequest.Method) and CompareRoutes(vRoute, vQuery, vTempWeight, vTempUriRoute) then
499527
begin
500528
if vTempWeight < vRouteWeight then
501529
begin
@@ -585,4 +613,25 @@ function TRALRouteParams.IndexOf(AName: StringRAL): IntegerRAL;
585613
end;
586614
end;
587615

616+
{ TRALRoutes.TEnumerator }
617+
618+
constructor TRALRoutes.TEnumerator.Create(const AArray: TRALRoutes);
619+
begin
620+
inherited Create;
621+
FIndex := -1;
622+
FArray := AArray;
623+
end;
624+
625+
function TRALRoutes.TEnumerator.GetCurrent: TRALRoute;
626+
begin
627+
Result := TRALRoute(FArray.Items[FIndex]);
628+
end;
629+
630+
function TRALRoutes.TEnumerator.MoveNext: Boolean;
631+
begin
632+
Result := FIndex < FArray.Count - 1;
633+
if Result then
634+
Inc(FIndex);
635+
end;
636+
588637
end.

src/base/RALServer.pas

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ TRALServer = class(TRALComponent)
229229
function CountSubModules: IntegerRAL;
230230
// Shortcut to create routes on the server
231231
function CreateRoute(const ARoute: StringRAL; AReplyProc: TRALOnReply;
232-
const ADescription: StringRAL = ''): TRALRoute;
232+
const ADescription: StringRAL = ''): TRALRoute; overload;
233+
function CreateRoute(const ARoute: StringRAL; AReplyProc: TRALOnReplyGen;
234+
const ADescription: StringRAL = ''): TRALRoute; overload;
233235
{ Core procedure of the server, every request will pass through here to be
234236
processed into response that will be answered to the client}
235237
procedure ProcessCommands(ARequest: TRALRequest; AResponse: TRALResponse);
@@ -460,6 +462,14 @@ function TRALServer.CreateRoute(const ARoute: StringRAL; AReplyProc: TRALOnReply
460462
Result.Description.Text := ADescription;
461463
end;
462464

465+
function TRALServer.CreateRoute(const ARoute: StringRAL;
466+
AReplyProc: TRALOnReplyGen; const ADescription: StringRAL): TRALRoute;
467+
begin
468+
Result := TRALRoute(FRoutes.Add);
469+
Result.Route := ARoute;
470+
Result.OnReplyGen := AReplyProc;
471+
Result.Description.Text := ADescription;
472+
end;
463473

464474
function TRALServer.IPv6IsImplemented: boolean;
465475
begin

src/utils/RALSwaggerExporter.pas

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,10 @@ procedure TRALSwaggerExporter.RouteToJSON(AItem: TRALJSONObject; AServer: TRALSe
517517
vRoute.Add(vStrMethod, vjMethod);
518518
end;
519519
end;
520-
AItem.Add(vStrRoute, vRoute)
520+
if assigned(AItem.Get(vStrRoute)) then // rota já existe, insere novo método
521+
TRALJSONObject(AItem.Get(vStrRoute)).Add(vStrMethod, vjMethod)
522+
else
523+
AItem.Add(vStrRoute, vRoute)
521524
finally
522525
FreeAndNil(vURIParams);
523526
end;

0 commit comments

Comments
 (0)