2018-09-25 15:36:53 +02:00
|
|
|
unit WebModuleU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
Web.HTTPApp,
|
2018-09-28 18:33:19 +02:00
|
|
|
MVCFramework,
|
|
|
|
FireDAC.Stan.Intf,
|
|
|
|
FireDAC.Stan.Option,
|
|
|
|
FireDAC.Stan.Error,
|
|
|
|
FireDAC.UI.Intf,
|
|
|
|
FireDAC.Phys.Intf,
|
|
|
|
FireDAC.Stan.Def,
|
|
|
|
FireDAC.Stan.Pool,
|
|
|
|
FireDAC.Stan.Async,
|
|
|
|
FireDAC.Phys,
|
|
|
|
FireDAC.Phys.MySQL,
|
|
|
|
FireDAC.Phys.MySQLDef,
|
|
|
|
FireDAC.VCLUI.Wait,
|
|
|
|
Data.DB,
|
2018-10-23 16:18:34 +02:00
|
|
|
FireDAC.Comp.Client,
|
|
|
|
FireDAC.Stan.Param,
|
|
|
|
FireDAC.DatS,
|
|
|
|
FireDAC.DApt.Intf,
|
|
|
|
FireDAC.DApt,
|
|
|
|
FireDAC.Comp.DataSet;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TMyWebModule = class(TWebModule)
|
2018-10-14 18:23:20 +02:00
|
|
|
FDQuery1: TFDQuery;
|
|
|
|
FDConnection1: TFDConnection;
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure WebModuleCreate(Sender: TObject);
|
|
|
|
procedure WebModuleDestroy(Sender: TObject);
|
|
|
|
private
|
|
|
|
FMVC: TMVCEngine;
|
|
|
|
public
|
|
|
|
{ Public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
WebModuleClass: TComponentClass = TMyWebModule;
|
2018-10-23 16:18:34 +02:00
|
|
|
ConnectionDefinitionName: string = '';
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
2018-09-28 13:01:46 +02:00
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
uses
|
|
|
|
System.IOUtils,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.ActiveRecordController,
|
|
|
|
MVCFramework.ActiveRecord,
|
2020-04-29 01:59:41 +02:00
|
|
|
MVCFramework.Middleware.StaticFiles,
|
|
|
|
FDConnectionConfigU,
|
|
|
|
OtherControllerU;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
procedure TMyWebModule.WebModuleCreate(Sender: TObject);
|
|
|
|
begin
|
|
|
|
FMVC := TMVCEngine.Create(Self,
|
|
|
|
procedure(Config: TMVCConfig)
|
|
|
|
begin
|
|
|
|
// session timeout (0 means session cookie)
|
|
|
|
Config[TMVCConfigKey.SessionTimeout] := '0';
|
|
|
|
// default content-type
|
|
|
|
Config[TMVCConfigKey.DefaultContentType] := TMVCConstants.DEFAULT_CONTENT_TYPE;
|
|
|
|
// default content charset
|
|
|
|
Config[TMVCConfigKey.DefaultContentCharset] := TMVCConstants.DEFAULT_CONTENT_CHARSET;
|
|
|
|
// unhandled actions are permitted?
|
|
|
|
Config[TMVCConfigKey.AllowUnhandledAction] := 'false';
|
|
|
|
// default view file extension
|
|
|
|
Config[TMVCConfigKey.DefaultViewFileExtension] := 'html';
|
|
|
|
// view path
|
|
|
|
Config[TMVCConfigKey.ViewPath] := 'templates';
|
|
|
|
// Enable Server Signature in response
|
|
|
|
Config[TMVCConfigKey.ExposeServerSignature] := 'true';
|
|
|
|
end);
|
2020-02-11 15:45:35 +01:00
|
|
|
|
2020-04-29 01:59:41 +02:00
|
|
|
FMVC.AddMiddleware(TMVCStaticFilesMiddleware.Create(
|
|
|
|
'/', { StaticFilesPath }
|
|
|
|
TPath.Combine(ExtractFilePath(GetModuleName(HInstance)), 'www'), { DocumentRoot }
|
|
|
|
'index.html' { IndexDocument }
|
|
|
|
));
|
2020-02-11 15:45:35 +01:00
|
|
|
FMVC.AddController(TOtherController, '/api/foo');
|
2018-09-25 15:36:53 +02:00
|
|
|
FMVC.AddController(TMVCActiveRecordController,
|
|
|
|
function: TMVCController
|
|
|
|
begin
|
|
|
|
Result := TMVCActiveRecordController.Create(
|
|
|
|
function: TFDConnection
|
|
|
|
begin
|
|
|
|
Result := TFDConnection.Create(nil);
|
2018-10-14 18:23:20 +02:00
|
|
|
Result.ConnectionDefName := ConnectionDefinitionName;
|
2018-09-25 15:36:53 +02:00
|
|
|
end,
|
|
|
|
function(aContext: TWebContext; aClass: TMVCActiveRecordClass; aAction: TMVCActiveRecordAction): Boolean
|
|
|
|
begin
|
|
|
|
if aContext.LoggedUser.IsValid then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
Result := True; // not(aAction in [TMVCActiveRecordAction.Delete]);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
end);
|
|
|
|
end, '/api/entities');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMyWebModule.WebModuleDestroy(Sender: TObject);
|
|
|
|
begin
|
|
|
|
FMVC.Free;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|