Skip to content

Commit

Permalink
Middleware units support for sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Jan 20, 2025
1 parent 61658de commit 23fbace
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
102 changes: 102 additions & 0 deletions sources/MVCFramework.Middleware.Session.Internal.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// ***************************************************************************
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2025 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.
//
// *************************************************************************** }

unit MVCFramework.Middleware.Session.Internal;

{$I dmvcframework.inc}

interface

uses
MVCFramework,
MVCFramework.Session,
MVCFramework.Logger;

type
TMVCSessionMiddleware = class(TInterfacedObject, IMVCMiddleware)
private
fSessionFactory: TMVCWebSessionFactory;
protected
procedure OnAfterControllerAction(aContext: TWebContext; const aControllerQualifiedClassName: string;
const aActionName: string; const AHandled: Boolean);
procedure OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
procedure OnBeforeControllerAction(AContext: TWebContext;
const AControllerQualifiedClassName: string; const AActionNAme: string; var AHandled: Boolean);
procedure OnAfterRouting(AContext: TWebContext; const AHandled: Boolean);
public
constructor Create(const SessionFactory: TMVCWebSessionFactory); virtual;
destructor Destroy; override;
end;

implementation

uses
System.SysUtils,
System.ZLib,
System.Classes,
MVCFramework.Session.Database,
MVCFramework.Commons;

{ TMVCSalutationMiddleware }

constructor TMVCSessionMiddleware.Create(const SessionFactory: TMVCWebSessionFactory);
begin
inherited Create;
fSessionFactory := SessionFactory;
end;

destructor TMVCSessionMiddleware.Destroy;
begin
fSessionFactory.Free;
inherited;
end;

procedure TMVCSessionMiddleware.OnAfterControllerAction(
AContext: TWebContext;
const AControllerQualifiedClassName: string; const AActionName: string;
const AHandled: Boolean);
begin
// do nothing
end;

procedure TMVCSessionMiddleware.OnAfterRouting(AContext: TWebContext;
const AHandled: Boolean);
begin
end;

procedure TMVCSessionMiddleware.OnBeforeControllerAction
(AContext: TWebContext; const AControllerQualifiedClassName,
AActionNAme: string; var AHandled: Boolean);
begin
// do nothing
end;

procedure TMVCSessionMiddleware.OnBeforeRouting(aContext: TWebContext;
var AHandled: Boolean);
begin
aContext.SetSessionFactory(fSessionFactory);
end;


end.
66 changes: 66 additions & 0 deletions sources/MVCFramework.Middleware.Session.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ***************************************************************************
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2025 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.
//
// *************************************************************************** }

unit MVCFramework.Middleware.Session;

{$I dmvcframework.inc}

interface

uses
MVCFramework,
MVCFramework.Session,
MVCFramework.Middleware.Session.Internal
;

function UseMemorySessionMiddleware(aTimeoutInMinutes: Integer = 0): TMVCSessionMiddleware;
function UseFileSessionMiddleware(aTimeoutInMinutes: Integer = 0; aSessionFolder: String = 'dmvc_sessions'): TMVCSessionMiddleware;
function UseDatabaseSessionMiddleware(aTimeoutInMinutes: Integer = 0): TMVCSessionMiddleware;

implementation

uses
MVCFramework.Session.Database;


function UseMemorySessionMiddleware(aTimeoutInMinutes: Integer = 0): TMVCSessionMiddleware;
begin
Result := TMVCSessionMiddleware.Create(TMVCWebSessionMemoryFactory.Create(aTimeoutInMinutes));
end;

function UseFileSessionMiddleware(aTimeoutInMinutes: Integer = 0; aSessionFolder: String = 'dmvc_sessions'): TMVCSessionMiddleware;
begin
Result := TMVCSessionMiddleware.Create(TMVCWebSessionFileFactory.Create(aTimeoutInMinutes, aSessionFolder));
end;

function UseDatabaseSessionMiddleware(aTimeoutInMinutes: Integer = 0): TMVCSessionMiddleware;
begin
Result := TMVCSessionMiddleware.Create(
TMVCWebSessionDatabaseFactory.Create(aTimeoutInMinutes, 'notused'));
end;




end.

0 comments on commit 23fbace

Please sign in to comment.