delphimvcframework/samples/swaggerdoc/MainFormU.pas

114 lines
2.5 KiB
ObjectPascal
Raw Normal View History

2019-07-27 20:23:48 +02:00
unit MainFormU;
interface
uses
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.AppEvnts,
Vcl.StdCtrls,
IdHTTPWebBrokerBridge,
Web.HTTPApp,
IdContext;
2019-07-27 20:23:48 +02:00
type
TMainForm = class(TForm)
2019-07-27 20:23:48 +02:00
ButtonStart: TButton;
ButtonStop: TButton;
EditPort: TEdit;
Label1: TLabel;
ApplicationEvents1: TApplicationEvents;
ButtonOpenBrowser: TButton;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
procedure ButtonStartClick(Sender: TObject);
procedure ButtonStopClick(Sender: TObject);
procedure ButtonOpenBrowserClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
FServer: TIdHTTPWebBrokerBridge;
procedure StartServer;
procedure OnParseAuthentication(AContext: TIdContext; const AAuthType, AAuthData: String;
var VUsername, VPassword: String; var VHandled: Boolean);
2019-07-27 20:23:48 +02:00
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
2019-07-27 20:23:48 +02:00
implementation
{$R *.dfm}
uses
WinApi.Windows,
Winapi.ShellApi;
2019-07-27 20:23:48 +02:00
procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
2019-07-27 20:23:48 +02:00
begin
ButtonStart.Enabled := not FServer.Active;
ButtonStop.Enabled := FServer.Active;
EditPort.Enabled := not FServer.Active;
end;
procedure TMainForm.ButtonOpenBrowserClick(Sender: TObject);
2019-07-27 20:23:48 +02:00
var
LURL: string;
begin
StartServer;
LURL := Format('http://localhost:%s/swagger', [EditPort.Text]);
ShellExecute(0, nil, PChar(LURL), nil, nil, SW_SHOWNOACTIVATE);
2019-07-27 20:23:48 +02:00
end;
procedure TMainForm.ButtonStartClick(Sender: TObject);
2019-07-27 20:23:48 +02:00
begin
StartServer;
end;
procedure TMainForm.ButtonStopClick(Sender: TObject);
2019-07-27 20:23:48 +02:00
begin
FServer.Active := False;
FServer.Bindings.Clear;
end;
procedure TMainForm.FormCreate(Sender: TObject);
2019-07-27 20:23:48 +02:00
begin
ReportMemoryLeaksOnShutdown := True;
2019-07-27 20:23:48 +02:00
FServer := TIdHTTPWebBrokerBridge.Create(Self);
FServer.OnParseAuthentication := OnParseAuthentication;
2019-07-27 20:23:48 +02:00
end;
procedure TMainForm.FormShow(Sender: TObject);
2019-07-27 20:23:48 +02:00
begin
ButtonOpenBrowser.Click;
end;
procedure TMainForm.OnParseAuthentication(AContext: TIdContext; const AAuthType, AAuthData: String; var VUsername,
VPassword: String; var VHandled: Boolean);
begin
if SameText(AAuthType, 'Bearer') then
VHandled := True;
end;
procedure TMainForm.StartServer;
2019-07-27 20:23:48 +02:00
begin
if not FServer.Active then
begin
FServer.Bindings.Clear;
FServer.DefaultPort := StrToInt(EditPort.Text);
FServer.Active := True;
end;
end;
end.