Config[TMVCConfigKey.FallbackResource]

Defines a default URL for requests that don't map to a route or a file (useful for client side web app)

The experts generates a Config[TMVCConfigKey.FallbackResource] which ponts to 'index.html'
This commit is contained in:
Daniele Teti 2016-12-07 12:34:51 +01:00
parent 81744b892e
commit 60ef18d148
5 changed files with 94 additions and 71 deletions

View File

@ -1,32 +1,32 @@
{***************************************************************************}
{ }
{ Delphi MVC Framework }
{ }
{ Copyright (c) 2010-2016 Daniele Teti and the DMVCFramework Team }
{ }
{ https://github.com/danieleteti/delphimvcframework }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{ This IDE expert is based off of the one included with the DUnitX }
{ project. Original source by Robert Love. Adapted by Nick Hodges. }
{ }
{ The DUnitX project is run by Vincent Parrett and can be found at: }
{ }
{ https://github.com/VSoftTechnologies/DUnitX }
{***************************************************************************}
{ *************************************************************************** }
{ }
{ Delphi MVC Framework }
{ }
{ Copyright (c) 2010-2016 Daniele Teti and the DMVCFramework Team }
{ }
{ https://github.com/danieleteti/delphimvcframework }
{ }
{ *************************************************************************** }
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{ This IDE expert is based off of the one included with the DUnitX }
{ project. Original source by Robert Love. Adapted by Nick Hodges. }
{ }
{ The DUnitX project is run by Vincent Parrett and can be found at: }
{ }
{ https://github.com/VSoftTechnologies/DUnitX }
{ *************************************************************************** }
unit DMVC.Expert.CodeGen.Templates;
@ -46,7 +46,7 @@ resourcestring
' MVCFramework.Logger,' + sLineBreak +
' Winapi.Windows,' + sLineBreak +
' Winapi.ShellAPI,' + sLineBreak +
' ReqMulti, {enables files upload}' + sLineBreak +
' ReqMulti, {enables files upload}' + sLineBreak +
' Web.WebReq,' + sLineBreak +
' Web.WebBroker,' + sLineBreak +
' IdHTTPWebBrokerBridge;' + sLineBreak +
@ -129,8 +129,8 @@ resourcestring
' end;' + sLineBreak +
sLineBreak +
'implementation' + sLineBreak +
'uses' + sLineBreak +
' MVCFramework.Logger;' + sLineBreak +
'uses' + sLineBreak +
' MVCFramework.Logger;' + sLineBreak +
sLineBreak +
'%3:s' + sLineBreak +
'%5:s' + sLineBreak +
@ -244,6 +244,8 @@ resourcestring
' Config[TMVCConfigKey.Messaging] := ''false'';' + sLineBreak +
' //Enable Server Signature in response' + sLineBreak +
' Config[TMVCConfigKey.ExposeServerSignature] := ''true'';' + sLineBreak +
' // Define a default URL for requests that don''t map to a route or a file (useful for client side web app)' + sLineBreak +
' Config[TMVCConfigKey.FallbackResource] := ''index.html'';' + sLineBreak +
' end);' + sLineBreak +
' FMVC.AddController(%3:s);' + sLineBreak +
'end;' + sLineBreak +

View File

@ -205,16 +205,7 @@
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployClass Name="DependencyModule">
<Platform Name="Win32">
<Operation>0</Operation>
<Extensions>.dll;.bpl</Extensions>
</Platform>
<Platform Name="OSX32">
<Operation>1</Operation>
<Extensions>.dylib</Extensions>
</Platform>
</DeployClass>
<DeployClass Name="ProjectiOSDeviceResourceRules"/>
<DeployClass Name="ProjectOSXResource">
<Platform Name="OSX32">
<RemoteDir>Contents\Resources</RemoteDir>
@ -554,7 +545,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="ProjectiOSDeviceResourceRules"/>
<DeployClass Name="DependencyModule">
<Platform Name="Win32">
<Operation>0</Operation>
<Extensions>.dll;.bpl</Extensions>
</Platform>
<Platform Name="OSX32">
<Operation>1</Operation>
<Extensions>.dylib</Extensions>
</Platform>
</DeployClass>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>

View File

@ -34,7 +34,7 @@ begin
procedure(Config: TMVCConfig)
begin
// enable static files
Config[TMVCConfigKey.DocumentRoot] := ExtractFilePath(GetModuleName(HInstance)) + '\www';
Config[TMVCConfigKey.DocumentRoot] := ExtractFilePath(GetModuleName(HInstance)) + 'www';
// session timeout (0 means session cookie)
Config[TMVCConfigKey.SessionTimeout] := '0';
// default content-type
@ -51,6 +51,9 @@ begin
Config[TMVCConfigKey.Messaging] := 'false';
// Enable Server Signature in response
Config[TMVCConfigKey.ExposeServerSignature] := 'true';
// Define a default URL for requests that don't map to a route or a file
Config[TMVCConfigKey.FallbackResource] := 'index.html';
end);
FMVC.AddController(TCustomersController);
FMVC.AddMiddleware(TCORSMiddleware.Create);

View File

@ -603,6 +603,10 @@ type
ExposeServerSignature = 'server_signature';
IndexDocument = 'index_document';
SessionType = 'session_type';
/// <summary>
/// Define a default URL for requests that don't map to a route or a file
/// </summary>
FallbackResource = 'fallback_resource';
end;
function IsShuttingDown: Boolean;
@ -725,6 +729,7 @@ begin
Log.Info('ENTER: Config default values', LOGGERPRO_TAG);
Config[TMVCConfigKey.SessionTimeout] := '30'; // 30 minutes
Config[TMVCConfigKey.DocumentRoot] := '.\www';
Config[TMVCConfigKey.FallbackResource] := '';
Config[TMVCConfigKey.DefaultContentType] :=
TMVCConstants.DEFAULT_CONTENT_TYPE;
Config[TMVCConfigKey.DefaultContentCharset] :=
@ -821,10 +826,9 @@ var
lActionFormalParams: TArray<TRttiParameter>;
lActualParams: TArray<TValue>;
function SendDocumentIndexIfPresent: Boolean;
function SendFileIfPresent(const AFileName: String): Boolean;
begin
lStaticFileName := TPath.Combine(Config[TMVCConfigKey.DocumentRoot],
Config[TMVCConfigKey.IndexDocument]);
lStaticFileName := TPath.Combine(Config[TMVCConfigKey.DocumentRoot], AFileName);
if TFile.Exists(lStaticFileName) then
begin
if FMimeTypes.TryGetValue(LowerCase(ExtractFileExt(lStaticFileName)), lContentType) then
@ -843,6 +847,29 @@ var
Result := false;
end;
function SendDocumentIndexIfPresent: Boolean;
begin
Result := SendFileIfPresent(Config[TMVCConfigKey.IndexDocument]);
// lStaticFileName := TPath.Combine(Config[TMVCConfigKey.DocumentRoot],
// Config[TMVCConfigKey.IndexDocument]);
// if TFile.Exists(lStaticFileName) then
// begin
// if FMimeTypes.TryGetValue(LowerCase(ExtractFileExt(lStaticFileName)), lContentType) then
// begin
// lContentType := lContentType + ';charset=' + FMVCConfig
// [TMVCConfigKey.DefaultContentCharset];
// end
// else
// begin
// lContentType := TMVCMimeType.APPLICATION_OCTETSTREAM;
// end;
// TMVCStaticContents.SendFile(lStaticFileName, lContentType, lContext);
// Result := true;
// end
// else
// Result := false;
end;
procedure FillActualParamsForAction(const AContext: TWebContext;
const aActionFormalParams: TArray<TRttiParameter>; const aActionName: string;
var aActualParams: TArray<TValue>);
@ -1062,16 +1089,6 @@ begin
lRouter.MethodToCall.Name);
end;
end;
if lContext.SessionMustBeClose then
begin
// SessionList.Remove(SelectedController.Session.SessionID);
end
else
begin
end;
finally
lSelectedController.MVCControllerBeforeDestroy;
end;
@ -1116,10 +1133,11 @@ begin
else
begin
if Config[TMVCConfigKey.AllowUnhandledAction] = 'false' then
// tristan
begin
Result := true;
if not SendDocumentIndexIfPresent then // danieleteti
Result := false;
if not Config[TMVCConfigKey.FallbackResource].IsEmpty then
Result := SendFileIfPresent(Config[TMVCConfigKey.FallbackResource]);
if not Result then
begin
Http404(lContext);
Log(TLogLevel.levNormal, Request.Method + ':' +

View File

@ -248,16 +248,7 @@
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployClass Name="DependencyModule">
<Platform Name="Win32">
<Operation>0</Operation>
<Extensions>.dll;.bpl</Extensions>
</Platform>
<Platform Name="OSX32">
<Operation>1</Operation>
<Extensions>.dylib</Extensions>
</Platform>
</DeployClass>
<DeployClass Name="ProjectiOSDeviceResourceRules"/>
<DeployClass Name="ProjectOSXResource">
<Platform Name="OSX32">
<RemoteDir>Contents\Resources</RemoteDir>
@ -599,7 +590,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="ProjectiOSDeviceResourceRules"/>
<DeployClass Name="DependencyModule">
<Platform Name="Win32">
<Operation>0</Operation>
<Extensions>.dll;.bpl</Extensions>
</Platform>
<Platform Name="OSX32">
<Operation>1</Operation>
<Extensions>.dylib</Extensions>
</Platform>
</DeployClass>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>