delphimvcframework/samples/swaggerdoc/MainFormU.pas
2020-05-02 20:00:17 -03:00

114 lines
2.5 KiB
ObjectPascal

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;
type
TMainForm = class(TForm)
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);
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
uses
WinApi.Windows,
Winapi.ShellApi;
procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
ButtonStart.Enabled := not FServer.Active;
ButtonStop.Enabled := FServer.Active;
EditPort.Enabled := not FServer.Active;
end;
procedure TMainForm.ButtonOpenBrowserClick(Sender: TObject);
var
LURL: string;
begin
StartServer;
LURL := Format('http://localhost:%s/swagger/index.html', [EditPort.Text]);
ShellExecute(0, nil, PChar(LURL), nil, nil, SW_SHOWNOACTIVATE);
end;
procedure TMainForm.ButtonStartClick(Sender: TObject);
begin
StartServer;
end;
procedure TMainForm.ButtonStopClick(Sender: TObject);
begin
FServer.Active := False;
FServer.Bindings.Clear;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := True;
FServer := TIdHTTPWebBrokerBridge.Create(Self);
FServer.OnParseAuthentication := OnParseAuthentication;
end;
procedure TMainForm.FormShow(Sender: TObject);
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;
begin
if not FServer.Active then
begin
FServer.Bindings.Clear;
FServer.DefaultPort := StrToInt(EditPort.Text);
FServer.Active := True;
end;
end;
end.