Skip to content

Commit

Permalink
Improved FileBased session sample
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Dec 24, 2024
1 parent ca507e8 commit a29bbf1
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 51 deletions.
41 changes: 20 additions & 21 deletions samples/session_file_based/AppControllerU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ TApp1MainController = class(TMVCController)
public
[MVCPath('/name')]
[MVCHTTPMethod([httpGET])]
procedure Index;
function Index: String;

[MVCPath('/list')]
[MVCHTTPMethod([httpGET])]
procedure GetCustomSessionData;
function GetCustomSessionData: String;

[MVCPath('/login/($username)')]
[MVCHTTPMethod([httpGET])]
procedure DoLogin(username: String);
function DoLogin(username: String): String;

[MVCPath('/fruit/($nameOfFruit)')]
[MVCHTTPMethod([httpGET])]
Expand All @@ -41,19 +41,18 @@ implementation

{ TApp1MainController }

procedure TApp1MainController.DoLogin(username: String);
function TApp1MainController.DoLogin(username: String): String;
begin
Session['username'] := username;
ResponseStream
.AppendLine('Logged as ' + username)
.AppendLine
.AppendLine('in address of browser type: ')
.AppendLine('http://localhost:8080/list to check the current values in session ')
.AppendLine('http://localhost:8080/fruit/apple to register apple ')
.AppendLine('http://localhost:8080/fruit/banana to register banana ')
.AppendLine('http://localhost:8080/logout to end session ')
.AppendLine('http://localhost:8080/login/johndoe to login as johndoe');
RenderResponseStream;
Result :=
'Logged as ' + username + sLineBreak +
sLineBreak +
'In the browser address bar, you can write: ' + sLineBreak +
'http://localhost:8080/list to check the current values in session ' + sLineBreak +
'http://localhost:8080/fruit/apple to register apple ' + sLineBreak +
'http://localhost:8080/fruit/banana to register banana ' + sLineBreak +
'http://localhost:8080/logout to end session ' + sLineBreak +
'http://localhost:8080/login/johndoe to login as johndoe';
end;

procedure TApp1MainController.RegisterFruit(nameOfFruit: String);
Expand All @@ -68,33 +67,33 @@ procedure TApp1MainController.DoLogout;
Render('Logout');
end;

procedure TApp1MainController.GetCustomSessionData;
function TApp1MainController.GetCustomSessionData: String;
var
I: Integer;
lList: TArray<String>;
begin
lList := Session.Keys;
ResponseStream.AppendLine('List of fruits:');
Result := 'List of fruits:' + sLineBreak;
for I := 0 to Length(lList) - 1 do
begin
ResponseStream.AppendLine(IntToStr(I + 1) + '-' + Session[lList[I]]);
Result := Result + sLineBreak + IntToStr(I + 1) + '-' + Session[lList[I]] + sLineBreak;
end;
RenderResponseStream;
end;

procedure TApp1MainController.Index;
function TApp1MainController.Index: String;
begin
ContentType := TMVCMediaType.TEXT_PLAIN;

// do not create session if not already created
if Context.SessionStarted then
begin
// automaticaly create the session
Render('Session[''username''] = ' + Session['username']);
Result := 'Session[''username''] = ' + Session['username']
end
else
begin
Render(400, 'Session not created. Do login first');
StatusCode := HTTP_STATUS.BadRequest;
Result := 'Session not created. Do login first';
end;
end;

Expand Down
11 changes: 6 additions & 5 deletions samples/session_file_based/FileBasedSessionSample.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ uses
Web.WebBroker,
IdHTTPWebBrokerBridge,
WebModuleUnit1 in 'WebModuleUnit1.pas' {WebModule1: TWebModule},
AppControllerU in 'AppControllerU.pas';
AppControllerU in 'AppControllerU.pas', MVCFramework.Logger;

{$R *.res}

Expand All @@ -24,15 +24,14 @@ procedure RunServer(APort: Integer);
var
LServer: TIdHTTPWebBrokerBridge;
begin
Writeln(Format('Starting HTTP Server or port %d', [APort]));
LogI(Format('Starting HTTP Server or http://localhost:%d', [APort]));
LServer := TIdHTTPWebBrokerBridge.Create(nil);
try
LServer.DefaultPort := APort;
LServer.Active := True;
{$IFDEF MSWINDOWS}
//ShellExecute(0, 'open', PChar('http://localhost:' + IntToStr(APort) + '/login/john'), nil, nil, SW_SHOW);
ShellExecute(0, 'open', PChar('http://localhost:' + IntToStr(APort) + '/login/john'), nil, nil, SW_SHOW);
{$ENDIF}
Writeln('CTRL+C to stop the server');
WaitForTerminationSignal;
EnterInShutdownState;
finally
Expand All @@ -42,14 +41,16 @@ end;

begin
ReportMemoryLeaksOnShutdown := True;
UseConsoleLogger := True;
UseLoggerVerbosityLevel := TLogLevel.levDebug;
try
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
WebRequestHandlerProc.MaxConnections := 1024;
RunServer(8080);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
LogF(E.ClassName + ': ' + E.Message);
end

end.
162 changes: 138 additions & 24 deletions samples/session_file_based/FileBasedSessionSample.dproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{F9CBCE21-869A-478F-992C-88FCAC97BC8B}</ProjectGuid>
<ProjectVersion>19.5</ProjectVersion>
<ProjectVersion>20.2</ProjectVersion>
<FrameworkType>VCL</FrameworkType>
<MainSource>FileBasedSessionSample.dpr</MainSource>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
<Platform Condition="'$(Platform)'==''">Win32</Platform>
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Console</AppType>
<ProjectName Condition="'$(ProjectName)'==''">FileBasedSessionSample</ProjectName>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
<Base>true</Base>
Expand Down Expand Up @@ -153,13 +154,8 @@
<Excluded_Packages Name="$(BDSBIN)\dclofficexp170.bpl">Microsoft Office XP Beispiele für gekapselte Komponenten für Automation Server</Excluded_Packages>
</Excluded_Packages>
</Delphi.Personality>
<Deployment Version="4">
<DeployFile LocalName="Win32\Debug\FileBasedSessionSample.exe" Configuration="Debug" Class="ProjectOutput">
<Platform Name="Win32">
<RemoteName>FileBasedSessionSample.exe</RemoteName>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<Deployment Version="5">
<DeployFile LocalName="Win32\Debug\FileBasedSessionSample.exe" Configuration="Debug" Class="ProjectOutput"/>
<DeployFile LocalName="Win32\Debug\SessionSample.exe" Configuration="Debug" Class="ProjectOutput"/>
<DeployClass Name="AdditionalDebugSymbols">
<Platform Name="OSX32">
Expand All @@ -169,16 +165,6 @@
<Operation>0</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidClasses">
<Platform Name="Android">
<RemoteDir>classes</RemoteDir>
<Operation>64</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>classes</RemoteDir>
<Operation>64</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidFileProvider">
<Platform Name="Android">
<RemoteDir>res\xml</RemoteDir>
Expand All @@ -189,12 +175,6 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidGDBServer">
<Platform Name="Android">
<RemoteDir>library\lib\armeabi-v7a</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidLibnativeArmeabiFile">
<Platform Name="Android">
<RemoteDir>library\lib\armeabi</RemoteDir>
Expand Down Expand Up @@ -247,6 +227,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashImageDefV21">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashStyles">
<Platform Name="Android">
<RemoteDir>res\values</RemoteDir>
Expand All @@ -267,6 +257,66 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashStylesV31">
<Platform Name="Android">
<RemoteDir>res\values-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\values-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIcon">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v26</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v26</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconBackground">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconForeground">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconMonochrome">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconV33">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v33</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v33</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_Colors">
<Platform Name="Android">
<RemoteDir>res\values</RemoteDir>
Expand All @@ -277,6 +327,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_ColorsDark">
<Platform Name="Android">
<RemoteDir>res\values-night-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\values-night-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_DefaultAppIcon">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
Expand Down Expand Up @@ -447,6 +507,56 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedNotificationIcon">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v24</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v24</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplash">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashDark">
<Platform Name="Android">
<RemoteDir>res\drawable-night-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-night-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashV31">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashV31Dark">
<Platform Name="Android">
<RemoteDir>res\drawable-night-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-night-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="DebugSymbols">
<Platform Name="iOSSimulator">
<Operation>1</Operation>
Expand Down Expand Up @@ -624,6 +734,9 @@
<Platform Name="Win64">
<Operation>1</Operation>
</Platform>
<Platform Name="Win64x">
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="ProjectiOSDeviceDebug">
<Platform Name="iOSDevice32">
Expand Down Expand Up @@ -885,6 +998,7 @@
<ProjectRoot Platform="OSXARM64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Win32" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Win64x" Name="$(PROJECTNAME)"/>
</Deployment>
<Platforms>
<Platform value="Win32">True</Platform>
Expand Down
3 changes: 2 additions & 1 deletion samples/session_file_based/WebModuleUnit1.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ implementation
{$R *.dfm}


uses AppControllerU, MVCFramework.Commons;
uses AppControllerU, MVCFramework.Commons, MVCFramework.Middleware.Redirect;

procedure TWebModule1.WebModuleCreate(Sender: TObject);
begin
Expand All @@ -39,6 +39,7 @@ procedure TWebModule1.WebModuleCreate(Sender: TObject);
Config[TMVCConfigKey.SessionType] := 'file';
end);
MVC.AddController(TApp1MainController);
MVC.AddMiddleware(TMVCRedirectMiddleware.Create(['/'], '/name'));
end;

end.

0 comments on commit a29bbf1

Please sign in to comment.