Skip to content

Commit

Permalink
Added WiRL starter sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobreveglieri committed Mar 23, 2023
1 parent 2d788e9 commit ef8ee63
Show file tree
Hide file tree
Showing 10 changed files with 3,794 additions and 0 deletions.
Binary file added Samples/Starter-WiRL/DemoContentTypes.res
Binary file not shown.
41 changes: 41 additions & 0 deletions Samples/Starter-WiRL/Server.Entities.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
unit Server.Entities;

interface

type
TSimpleClass = class
private
FName: string;
FDevLanguage: string;
FDevSince: Integer;
FLastName: string;
public
property Name: string read FName write FName;
property LastName: string read FLastName write FLastName;
property DevLanguage: string read FDevLanguage write FDevLanguage;
property DevSince: Integer read FDevSince write FDevSince;
public
class function RandomEntity: TSimpleClass;
end;

implementation

{ TSimpleClass }

class function TSimpleClass.RandomEntity: TSimpleClass;
const
FirstNames: array of string = ['Paolo', 'Luca', 'Marco', 'Maurizio', 'Fabio', 'Omar', 'Carlo', 'Thomas', 'Antonio'];
LastNames: array of string = ['Rossi', 'Minuti', 'Cantù', 'Del Magno', 'Codebue', 'Bossoni', 'Narcisi', 'Ranzetti', 'Polito'];
DevLangs: array of string = ['Delphi', 'VB', 'Clipper', 'C#', 'COBOL', 'ASM', 'Pascal', 'Java'];
begin
Result := Self.Create;
Result.Name := FirstNames[Random(High(FirstNames)-1)];
Result.LastName := LastNames[Random(High(LastNames)-1)];
Result.DevLanguage := DevLangs[Random(High(DevLangs)-1)];
Result.DevSince := Random(20);
end;

initialization
Randomize();

end.
2,339 changes: 2,339 additions & 0 deletions Samples/Starter-WiRL/Server.Forms.Main.dfm

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions Samples/Starter-WiRL/Server.Forms.Main.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{******************************************************************************}
{ }
{ WiRL: RESTful Library for Delphi }
{ }
{ Copyright (c) 2015-2019 WiRL Team }
{ }
{ https://github.com/delphi-blocks/WiRL }
{ }
{******************************************************************************}
unit Server.Forms.Main;

interface

uses
System.Classes, System.SysUtils, Vcl.Forms, Vcl.ActnList, Vcl.ComCtrls,
Vcl.StdCtrls, Vcl.Controls, Vcl.ExtCtrls, System.Diagnostics, System.Actions,
WiRL.Core.Engine,
WiRL.Core.Application,
WiRL.http.Server,
WiRL.http.Server.Indy, Vcl.Imaging.pngimage;

type
TMainForm = class(TForm)
TopPanel: TPanel;
StartServerButton: TButton;
StopServerButton: TButton;
MainActionList: TActionList;
StartServerAction: TAction;
StopServerAction: TAction;
PortNumberEdit: TEdit;
PortNumberLabel: TLabel;
WiRLImage: TImage;
PromImage: TImage;
procedure StartServerActionExecute(Sender: TObject);
procedure StopServerActionExecute(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure MainActionListUpdate(Action: TBasicAction; var Handled: Boolean);
private
FServer: TWiRLServer;
public
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

uses
Prometheus.Registry,
Prometheus.Collectors.Counter,
WiRL.Core.JSON,
WiRL.Rtti.Utils;

procedure TMainForm.FormCreate(Sender: TObject);
begin
StartServerAction.Execute;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
StopServerAction.Execute;
end;

procedure TMainForm.MainActionListUpdate(Action: TBasicAction;
var Handled: Boolean);
begin
StartServerAction.Enabled := (FServer = nil) or (FServer.Active = False);
StopServerAction.Enabled := not StartServerAction.Enabled;
end;

procedure TMainForm.StartServerActionExecute(Sender: TObject);
begin
// Create http server
FServer := TWiRLServer.Create(nil);

// Server configuration
FServer
.SetPort(StrToIntDef(PortNumberEdit.Text, 8080))
// Engine configuration
.AddEngine<TWiRLEngine>('/rest')
.SetEngineName('WiRL ContentType Demo')

// Application configuration
.AddApplication('/app')
.SetAppName('Content App')
.SetWriters('*')
.SetReaders('*')
.SetResources('Server.Resources.Metrics.TMetricsResource') // metrics!
.SetResources('Server.Resources.TSampleResource');

// Metrics configuration
TCounter
.Create('http_requests_count', 'Conteggio totale delle richieste HTTP ricevute')
.Register();

// Start the Web server
if not FServer.Active then
FServer.Active := True;
end;

procedure TMainForm.StopServerActionExecute(Sender: TObject);
begin
FServer.Active := False;
FServer.Free;
// Clear metrics
TCollectorRegistry.DefaultRegistry.Clear;
end;

end.
42 changes: 42 additions & 0 deletions Samples/Starter-WiRL/Server.Resources.Metrics.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
unit Server.Resources.Metrics;

interface

uses
WiRL.Core.Attributes,
WiRL.http.Accept.MediaType;

type

{ TMetricsResource }

[Path('metrics')]
TMetricsResource = class
public
[GET, Produces(TMediaType.TEXT_PLAIN)]
function GetMetrics: string;
end;

implementation

uses
Prometheus.Registry,
Prometheus.Exposers.Text,
WiRL.Core.Registry;

{ TMetricsResource }

function TMetricsResource.GetMetrics: string;
begin
var LWriter := TTextExposer.Create;
try
Result := LWriter.Render(TCollectorRegistry.DefaultRegistry.Collect);
finally
LWriter.Free;
end;
end;

initialization
TWiRLResourceRegistry.Instance.RegisterResource<TMetricsResource>;

end.
Loading

0 comments on commit ef8ee63

Please sign in to comment.