From a29bbf15774dad97d0d7f156cb64cbc615617aed Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Tue, 24 Dec 2024 10:40:10 +0100 Subject: [PATCH] Improved FileBased session sample --- samples/session_file_based/AppControllerU.pas | 41 +++-- .../FileBasedSessionSample.dpr | 11 +- .../FileBasedSessionSample.dproj | 162 +++++++++++++++--- samples/session_file_based/WebModuleUnit1.pas | 3 +- 4 files changed, 166 insertions(+), 51 deletions(-) diff --git a/samples/session_file_based/AppControllerU.pas b/samples/session_file_based/AppControllerU.pas index bdac9165..de53c8d8 100644 --- a/samples/session_file_based/AppControllerU.pas +++ b/samples/session_file_based/AppControllerU.pas @@ -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])] @@ -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); @@ -68,21 +67,20 @@ procedure TApp1MainController.DoLogout; Render('Logout'); end; -procedure TApp1MainController.GetCustomSessionData; +function TApp1MainController.GetCustomSessionData: String; var I: Integer; lList: TArray; 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; @@ -90,11 +88,12 @@ procedure TApp1MainController.Index; 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; diff --git a/samples/session_file_based/FileBasedSessionSample.dpr b/samples/session_file_based/FileBasedSessionSample.dpr index bf342dbd..679706ab 100644 --- a/samples/session_file_based/FileBasedSessionSample.dpr +++ b/samples/session_file_based/FileBasedSessionSample.dpr @@ -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} @@ -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 @@ -42,6 +41,8 @@ end; begin ReportMemoryLeaksOnShutdown := True; + UseConsoleLogger := True; + UseLoggerVerbosityLevel := TLogLevel.levDebug; try if WebRequestHandler <> nil then WebRequestHandler.WebModuleClass := WebModuleClass; @@ -49,7 +50,7 @@ begin RunServer(8080); except on E: Exception do - Writeln(E.ClassName, ': ', E.Message); + LogF(E.ClassName + ': ' + E.Message); end end. diff --git a/samples/session_file_based/FileBasedSessionSample.dproj b/samples/session_file_based/FileBasedSessionSample.dproj index 4e9c4a3c..c3b0b9f1 100644 --- a/samples/session_file_based/FileBasedSessionSample.dproj +++ b/samples/session_file_based/FileBasedSessionSample.dproj @@ -1,7 +1,7 @@  {F9CBCE21-869A-478F-992C-88FCAC97BC8B} - 19.5 + 20.2 VCL FileBasedSessionSample.dpr True @@ -9,6 +9,7 @@ Win32 1 Console + FileBasedSessionSample true @@ -153,13 +154,8 @@ Microsoft Office XP Beispiele für gekapselte Komponenten für Automation Server - - - - FileBasedSessionSample.exe - true - - + + @@ -169,16 +165,6 @@ 0 - - - classes - 64 - - - classes - 64 - - res\xml @@ -189,12 +175,6 @@ 1 - - - library\lib\armeabi-v7a - 1 - - library\lib\armeabi @@ -247,6 +227,16 @@ 1 + + + res\drawable-anydpi-v21 + 1 + + + res\drawable-anydpi-v21 + 1 + + res\values @@ -267,6 +257,66 @@ 1 + + + res\values-v31 + 1 + + + res\values-v31 + 1 + + + + + res\drawable-anydpi-v26 + 1 + + + res\drawable-anydpi-v26 + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\drawable-anydpi-v33 + 1 + + + res\drawable-anydpi-v33 + 1 + + res\values @@ -277,6 +327,16 @@ 1 + + + res\values-night-v21 + 1 + + + res\values-night-v21 + 1 + + res\drawable @@ -447,6 +507,56 @@ 1 + + + res\drawable-anydpi-v24 + 1 + + + res\drawable-anydpi-v24 + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\drawable-night-anydpi-v21 + 1 + + + res\drawable-night-anydpi-v21 + 1 + + + + + res\drawable-anydpi-v31 + 1 + + + res\drawable-anydpi-v31 + 1 + + + + + res\drawable-night-anydpi-v31 + 1 + + + res\drawable-night-anydpi-v31 + 1 + + 1 @@ -624,6 +734,9 @@ 1 + + 1 + @@ -885,6 +998,7 @@ + True diff --git a/samples/session_file_based/WebModuleUnit1.pas b/samples/session_file_based/WebModuleUnit1.pas index b7d8a556..818d383d 100644 --- a/samples/session_file_based/WebModuleUnit1.pas +++ b/samples/session_file_based/WebModuleUnit1.pas @@ -27,7 +27,7 @@ implementation {$R *.dfm} -uses AppControllerU, MVCFramework.Commons; +uses AppControllerU, MVCFramework.Commons, MVCFramework.Middleware.Redirect; procedure TWebModule1.WebModuleCreate(Sender: TObject); begin @@ -39,6 +39,7 @@ procedure TWebModule1.WebModuleCreate(Sender: TObject); Config[TMVCConfigKey.SessionType] := 'file'; end); MVC.AddController(TApp1MainController); + MVC.AddMiddleware(TMVCRedirectMiddleware.Create(['/'], '/name')); end; end.