-
Notifications
You must be signed in to change notification settings - Fork 18
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
2d788e9
commit ef8ee63
Showing
10 changed files
with
3,794 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,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. |
Large diffs are not rendered by default.
Oops, something went wrong.
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,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. |
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,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. |
Oops, something went wrong.