2019-07-27 20:23:48 +02:00
|
|
|
|
unit WebModuleMainU;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.Classes,
|
|
|
|
|
Web.HTTPApp,
|
|
|
|
|
MVCFramework;
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
TWebModule1 = class(TWebModule)
|
|
|
|
|
procedure WebModuleCreate(Sender: TObject);
|
|
|
|
|
procedure WebModuleDestroy(Sender: TObject);
|
|
|
|
|
private
|
|
|
|
|
{ Private declarations }
|
|
|
|
|
FEngine: TMVCEngine;
|
|
|
|
|
public
|
|
|
|
|
{ Public declarations }
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
WebModuleClass: TComponentClass = TWebModule1;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
MVCFramework.Commons,
|
|
|
|
|
MVCFramework.Controllers.Register,
|
|
|
|
|
MVCFramework.Middleware.Swagger,
|
2019-07-30 23:15:37 +02:00
|
|
|
|
MVCFramework.Swagger.Commons,
|
2019-07-31 13:40:11 +02:00
|
|
|
|
MVCFramework.Middleware.JWT,
|
2020-04-29 01:59:41 +02:00
|
|
|
|
MVCFramework.Middleware.StaticFiles,
|
2019-10-30 16:02:30 +01:00
|
|
|
|
AuthHandler,
|
|
|
|
|
MVCFramework.JWT,
|
|
|
|
|
System.DateUtils;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
|
|
|
|
{%CLASSGROUP 'Vcl.Controls.TControl'}
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
2019-07-29 14:27:09 +02:00
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
procedure TWebModule1.WebModuleCreate(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
LSwagInfo: TMVCSwaggerInfo;
|
2019-07-31 13:40:11 +02:00
|
|
|
|
LClaimsSetup: TJWTClaimsSetup;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2020-05-03 01:00:17 +02:00
|
|
|
|
FEngine := TMVCEngine.Create(Self,
|
|
|
|
|
procedure(AConfig: TMVCConfig)
|
|
|
|
|
begin
|
|
|
|
|
AConfig[TMVCConfigKey.PathPrefix] := '/api';
|
|
|
|
|
AConfig[TMVCConfigKey.LoadSystemControllers] := 'false';
|
|
|
|
|
end
|
|
|
|
|
);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
|
|
|
|
LSwagInfo.Title := 'Sample Swagger API';
|
|
|
|
|
LSwagInfo.Version := 'v1';
|
|
|
|
|
LSwagInfo.TermsOfService := 'http://www.apache.org/licenses/LICENSE-2.0.txt';
|
|
|
|
|
LSwagInfo.Description := 'Swagger Documentation Example';
|
|
|
|
|
LSwagInfo.ContactName := 'Jo<4A>o Ant<6E>nio Duarte';
|
|
|
|
|
LSwagInfo.ContactEmail := 'joao.antonioduarte@hotmail.com';
|
|
|
|
|
LSwagInfo.ContactUrl := 'https://github.com/joaoduarte19';
|
|
|
|
|
LSwagInfo.LicenseName := 'Apache License - Version 2.0, January 2004';
|
|
|
|
|
LSwagInfo.LicenseUrl := 'http://www.apache.org/licenses/LICENSE-2.0';
|
2019-10-10 13:32:00 +02:00
|
|
|
|
FEngine.AddMiddleware(TMVCSwaggerMiddleware.Create(FEngine, LSwagInfo, '/api/swagger.json',
|
2020-05-03 01:00:17 +02:00
|
|
|
|
'Method for authentication using JSON Web Token (JWT)'));
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
2019-07-31 13:40:11 +02:00
|
|
|
|
LClaimsSetup := procedure(const JWT: TJWT)
|
|
|
|
|
begin
|
|
|
|
|
JWT.Claims.Issuer := 'Delphi MVC Framework Swagger Documentation';
|
|
|
|
|
JWT.Claims.ExpirationTime := Now + OneHour; // valid for 1 hour
|
|
|
|
|
JWT.Claims.NotBefore := Now - OneMinute * 5; // valid since 5 minutes ago
|
|
|
|
|
JWT.Claims.IssuedAt := Now;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
FEngine.AddMiddleware(TMVCJWTAuthenticationMiddleware.Create(
|
2020-05-03 01:00:17 +02:00
|
|
|
|
TAuthHandler.Create,
|
|
|
|
|
'D3lph1MVCFram3w0rk',
|
|
|
|
|
'/api/login',
|
|
|
|
|
LClaimsSetup,
|
|
|
|
|
[TJWTCheckableClaim.ExpirationTime, TJWTCheckableClaim.NotBefore, TJWTCheckableClaim.IssuedAt]
|
|
|
|
|
));
|
|
|
|
|
FEngine.AddMiddleware(TMVCStaticFilesMiddleware.Create(
|
|
|
|
|
'/', { StaticFilesPath }
|
|
|
|
|
'.\www', { DocumentRoot }
|
|
|
|
|
'index.html' { IndexDocument - Before it was named fallbackresource }
|
2020-04-29 01:59:41 +02:00
|
|
|
|
));
|
2019-07-31 13:40:11 +02:00
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
/// Add your registered controllers to engine.
|
|
|
|
|
/// Only registered controls such as "MyServerName" will be added
|
2019-07-29 14:27:09 +02:00
|
|
|
|
TControllersRegister.Instance.AddControllersInEngine(FEngine, 'MyServerName');
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TWebModule1.WebModuleDestroy(Sender: TObject);
|
|
|
|
|
begin
|
|
|
|
|
FEngine.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end.
|