Changeset 78 for trunk/Modules


Ignore:
Timestamp:
Jun 18, 2012, 10:44:21 PM (12 years ago)
Author:
chronos
Message:
  • Upraveno: Obsluha stránek rozdělena do samostatných sezení(TWebSession). Každé sezení si tak alokuje vlastní instanci TSqlDatabase. Zároveň při obsluze jednoduchých souborů se nyní již nenačítá info o přihlášeném uživateli a nevytváří se tak pro každé načítání souboru instance databázového připojení.
Location:
trunk/Modules
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/UMainModule.lfm

    r77 r78  
    22  OldCreateOrder = False
    33  Height = 278
    4   HorizontalOffset = 418
    5   VerticalOffset = 205
     4  HorizontalOffset = 554
     5  VerticalOffset = 266
    66  Width = 431
    7   object HTTPSessionStorageMySQL1: THTTPSessionStorageMySQL
    8     Database = Database
    9     Timeout = 10800
    10     SessionIdCookieName = 'SessionId'
    11     left = 304
    12     top = 32
    13   end
    14   object Database: TSqlDatabase
    15     Connected = False
    16     Port = 3306
    17     Encoding = 'utf8'
    18     left = 81
    19     top = 86
    20   end
    217  object WebApp1: TWebApp
    228    OnPageProduce = WebApp1PageProduce
    23     ServerType = stTCP
     9    ServerType = stCGI
    2410    left = 160
    2511    top = 88
  • trunk/Modules/UMainModule.lrs

    r77 r78  
    33LazarusResources.Add('TMainModule','FORMDATA',[
    44  'TPF0'#11'TMainModule'#10'MainModule'#14'OldCreateOrder'#8#6'Height'#3#22#1#16
    5   +'HorizontalOffset'#3#162#1#14'VerticalOffset'#3#205#0#5'Width'#3#175#1#0#24
    6   +'THTTPSessionStorageMySQL'#24'HTTPSessionStorageMySQL1'#8'Database'#7#8'Data'
    7   +'base'#7'Timeout'#3'0*'#19'SessionIdCookieName'#6#9'SessionId'#4'left'#3'0'#1
    8   +#3'top'#2' '#0#0#12'TSqlDatabase'#8'Database'#9'Connected'#8#4'Port'#3#234#12
    9   +#8'Encoding'#6#4'utf8'#4'left'#2'Q'#3'top'#2'V'#0#0#7'TWebApp'#7'WebApp1'#13
    10   +'OnPageProduce'#7#18'WebApp1PageProduce'#10'ServerType'#7#5'stTCP'#4'left'#3
    11   +#160#0#3'top'#2'X'#0#0#0
     5  +'HorizontalOffset'#3'*'#2#14'VerticalOffset'#3#10#1#5'Width'#3#175#1#0#7'TWe'
     6  +'bApp'#7'WebApp1'#13'OnPageProduce'#7#18'WebApp1PageProduce'#10'ServerType'#7
     7  +#5'stCGI'#4'left'#3#160#0#3'top'#2'X'#0#0#0
    128]);
  • trunk/Modules/UMainModule.pas

    r77 r78  
    1515
    1616type
    17   TSession = class
    18 
     17  TMainModule = class;
     18
     19  { TWebSession }
     20
     21  TWebSession = class(THTTPHandlerData)
     22  private
     23    procedure TopMenu;
     24    procedure Footer;
     25  public
     26    MainModule: TMainModule;
     27    Database: TSqlDatabase;
     28    SessionStorage: THTTPSessionStorageMySQL;
     29    User: TWebUser;
     30    UserOnline: TWebOnlineUser;
     31    HtmlDocument: THtmlDocument;
     32    GlobalTitle: string;
     33    TimeStart: TDateTime;
     34    procedure LoadUserInfo;
     35    procedure InitDatabase;
     36    procedure GeneratePage(Page: TWebPage);
     37    constructor Create; override;
     38    destructor Destroy; override;
    1939  end;
    2040
     
    2242
    2343  TMainModule = class(TDataModule)
    24     HTTPSessionStorageMySQL1: THTTPSessionStorageMySQL;
    25     Database: TSqlDatabase;
    2644    WebApp1: TWebApp;
    2745    procedure WebApp1PageProduce(HandlerData: THTTPHandlerData);
    2846  private
    29     procedure Footer;
    30     procedure InitDatabase;
    3147    procedure Show(Content: string);
    32     procedure TopMenu;
    3348  public
    34     TimeStart: TDateTime;
    3549    Load: string;
    3650    Unload: string;
    37     GlobalTitle: string;
    3851    ShowRuntimeInfo: Boolean;
    3952    Charset: string;
    4053    Admin: string;
    4154    AdminEmail: string;
     55    Keywords: string;
    4256    Style: string;
    43     Keywords: string;
    44     HtmlDocument: THtmlDocument;
    4557    BaseURL: string;
    46     User: TWebUser;
    47     UserOnline: TWebOnlineUser;
    4858    FormatHTML: Boolean;
    4959    NetworkAddress: string;
     
    5161    MaxConnections: Integer;
    5262    Pages: TPageList;
    53     procedure LoadUserInfo(HandlerData: THTTPHandlerData);
     63    DatabaseHostname: string;
     64    DatabaseUserName: string;
     65    DatabasePassword: string;
     66    DatabaseSchema: string;
    5467    procedure LoadFromRegistry;
    5568    procedure SaveToRegistry;
    56     procedure GeneratePage(HandlerData: THTTPHandlerData; Page: TWebPage);
    5769    procedure Run;
    5870    constructor Create(AOwner: TComponent); override;
     
    7183  UNetworkPage, UAboutPage, UContactPage;
    7284
    73 { TMainModule }
    74 
    75 procedure TMainModule.GeneratePage(HandlerData: THTTPHandlerData; Page: TWebPage);
     85{ TWebSession }
     86
     87constructor TWebSession.Create;
     88begin
     89  inherited;
     90  Database := TSqlDatabase.Create(nil);
     91  SessionStorage := THTTPSessionStorageMySQL.Create(nil);
     92  SessionStorage.Database := Database;
     93  HtmlDocument := THtmlDocument.Create;
     94  User := TWebUser.Create;
     95  User.Database := Database;
     96  UserOnline := TWebOnlineUser.Create;
     97  UserOnline.Database := Database;
     98end;
     99
     100destructor TWebSession.Destroy;
     101begin
     102  HtmlDocument.Free;
     103  User.Free;
     104  UserOnline.Free;
     105  SessionStorage.Free;
     106  Database.Free;
     107  inherited Destroy;
     108end;
     109
     110procedure TWebSession.InitDatabase;
     111var
     112  DbRows: TDbRows;
     113begin
     114  with Database do begin
     115    Connect;
     116  end;
     117  try
     118    DbRows := TDbRows.Create;
     119    Database.Query(DbRows, 'SET NAMES utf8');
     120  finally
     121    DbRows.Free;
     122  end;
     123end;
     124
     125procedure TWebSession.LoadUserInfo;
     126begin
     127  User.HandlerData := Self;
     128  UserOnline.HandlerData := Self;
     129  UserOnline.Update;
     130  User.Id := UserOnline.User;
     131  User.Load;
     132end;
     133
     134procedure TWebSession.GeneratePage(Page: TWebPage);
    76135var
    77136  I: Integer;
    78137  TitleTag: THtmlString;
    79138begin
    80   with HandlerData do begin
    81139    HtmlDocument.ContentLanguage := 'cs';
    82140    GlobalTitle := 'ZděchovNET';
    83     HtmlDocument.Styles.Add(NavigationLink('/Style/' + Style + '/Style.css'));
    84     HtmlDocument.Scripts.Add(NavigationLink('/Style/' + Style + '/Global.js'));
    85     HtmlDocument.Scripts.Add(NavigationLink('/Style/' + Style + '/jquery.js'));
     141    HtmlDocument.Styles.Add(NavigationLink('/Style/' + MainModule.Style + '/Style.css'));
     142    HtmlDocument.Scripts.Add(NavigationLink('/Style/' + MainModule.Style + '/Global.js'));
     143    HtmlDocument.Scripts.Add(NavigationLink('/Style/' + MainModule.Style + '/jquery.js'));
    86144
    87145      TitleTag := THtmlString.Create;
     
    95153      with HtmlDocument.AsXmlDocument do
    96154      try
    97         Formated := FormatHTML;
     155        Formated := MainModule.FormatHTML;
    98156        Response.Content.WriteString(AsString);
    99157      finally
    100158        Free;
    101159      end;
    102   end;
    103 end;
    104 
    105 procedure TMainModule.InitDatabase;
    106 var
    107   DbRows: TDbRows;
    108 begin
    109   with Database do begin
    110     Connect;
    111   end;
    112   try
    113     DbRows := TDbRows.Create;
    114     Database.Query(DbRows, 'SET NAMES utf8');
    115   finally
    116     DbRows.Free;
    117   end;
    118 end;
     160end;
     161
     162
     163{ TMainModule }
    119164
    120165constructor TMainModule.Create(AOwner: TComponent);
     
    122167  inherited;
    123168  Pages := TPageList.Create;
    124   HtmlDocument := THtmlDocument.Create;
    125   User := TWebUser.Create;
    126   User.Database := Database;
    127   UserOnline := TWebOnlineUser.Create;
    128   UserOnline.Database := Database;
    129169  LoadFromRegistry;
    130170  with Pages do begin
     
    156196begin
    157197  //SaveToRegistry;
    158   HtmlDocument.Free;
    159   User.Free;
    160   UserOnline.Free;
    161198  Pages.Free;
    162199  inherited Destroy;
    163200end;
    164201
    165 procedure TMainModule.Footer;
     202procedure TWebSession.Footer;
    166203var
    167204  Tag: TXMLTag;
     
    178215      Name := 'li';
    179216      with TXmlString(SubElements.AddNew(TXmlString.Create)) do begin
    180         Text := Admin;
     217        Text := MainModule.Admin;
    181218      end;
    182219    end;
     
    184221      Name := 'li';
    185222      with TXmlString(SubElements.AddNew(TXmlString.Create)) do begin
    186         Text := AdminEmail;
    187       end;
    188     end;
    189     if ShowRuntimeInfo then begin
     223        Text := MainModule.AdminEmail;
     224      end;
     225    end;
     226    if MainModule.ShowRuntimeInfo then begin
    190227      with TXmlTag(SubElements.AddNew(TXmlTag.Create)) do begin
    191228        Name := 'li';
     
    232269end;
    233270
    234 procedure TMainModule.TopMenu;
     271procedure TWebSession.TopMenu;
    235272begin
    236273  with THtmlString(HtmlDocument.Body.SubItems.InsertNew(1, THtmlString.Create)) do begin
     
    269306end;
    270307
    271 procedure TMainModule.LoadUserInfo(HandlerData: THTTPHandlerData);
    272 begin
    273   User.HandlerData := HandlerData;
    274   UserOnline.HandlerData := HandlerData;
    275   UserOnline.Update;
    276   User.Id := UserOnline.User;
    277   User.Load;
    278 end;
    279 
    280308procedure TMainModule.WebApp1PageProduce(HandlerData: THTTPHandlerData);
    281309var
    282310  FileName: string;
     311  PageName: string;
     312  Page: TRegistredPage;
     313  NewSession: TWebSession;
     314  BaseUrlParts: TListString;
    283315begin
    284316  with HandlerData do begin
    285     if not Pages.ProducePage(HandlerData) then begin
     317    try
     318      BaseUrlParts := TListString.Create;
     319      BaseUrlParts.Explode(BaseURL, '/', StrToStr);
     320      while (BaseUrlParts.Count > 0) and (Request.Path.Count > 0) and
     321         (BaseUrlParts[0] = Request.Path[0]) do begin
     322           BaseUrlParts.Delete(0);
     323           Request.Path.Delete(0);
     324         end;
     325    finally
     326      BaseUrlParts.Free;
     327    end;
     328    if Request.Path.Count > 0 then PageName := Request.Path[0]
     329      else PageName := '';
     330    Page := Pages.FindByName(PageName);
     331    if Assigned(Page) then begin
     332      NewSession := TWebSession.Create;
     333      NewSession.MainModule := Self;
     334      NewSession.Assign(HandlerData);
     335      NewSession.TimeStart := Now;
     336      NewSession.Database.HostName := DatabaseHostName;
     337      NewSession.Database.Password := DatabasePassword;
     338      NewSession.Database.Database := DatabaseSchema;
     339      NewSession.Database.UserName := DatabaseUserName;
     340      NewSession.InitDatabase;
     341      Page.Page.OnProduce(NewSession);
     342      HandlerData.Assign(NewSession);
     343    end else begin
    286344      WebApp1.HTTPServer.FileResponse(HandlerData)
    287345    end;
     
    301359    Style := ReadString(SectionGeneral, 'Style', 'Basic');
    302360    BaseURL := ReadString(SectionGeneral, 'BaseURL', 'http://localhost');
    303     Database.Hostname := ReadString(SectionDatabase, 'DatabaseHostName', 'localhost');
    304     Database.Database := ReadString(SectionDatabase, 'DatabaseDatabase', 'web');
    305     Database.UserName := ReadString(SectionDatabase, 'DatabaseUserName', 'user');
    306     Database.Password := ReadString(SectionDatabase, 'DatabasePassword', 'password');
     361    DatabaseHostname := ReadString(SectionDatabase, 'DatabaseHostName', 'localhost');
     362    DatabaseSchema := ReadString(SectionDatabase, 'DatabaseDatabase', 'web');
     363    DatabaseUserName := ReadString(SectionDatabase, 'DatabaseUserName', 'user');
     364    DatabasePassword := ReadString(SectionDatabase, 'DatabasePassword', 'password');
    307365    FormatHTML := ReadBool(SectionGeneral, 'FormatHTML', False);
    308366    //MainModule.WebApp1.LogException := not ReadBool(SectionGeneral, 'ShowException', False);
     
    327385    WriteString(SectionGeneral, 'Style', Style);
    328386    WriteString(SectionGeneral, 'BaseURL', BaseURL);
    329     WriteString(SectionDatabase, 'DatabaseHostName', Database.Hostname);
    330     WriteString(SectionDatabase, 'DatabaseDatabase', Database.Database);
    331     WriteString(SectionDatabase, 'DatabaseUserName', Database.UserName);
    332     WriteString(SectionDatabase, 'DatabasePassword', Database.Password);
     387    WriteString(SectionDatabase, 'DatabaseHostName', DatabaseHostname);
     388    WriteString(SectionDatabase, 'DatabaseDatabase', DatabaseSchema);
     389    WriteString(SectionDatabase, 'DatabaseUserName', DatabaseUserName);
     390    WriteString(SectionDatabase, 'DatabasePassword', DatabasePassword);
    333391    WriteBool(SectionGeneral, 'FormatHTML', FormatHTML);
    334392    //WriteBool(SectionGeneral, 'ShowException', not MainModule.WebApp1.LogException);
     
    343401procedure TMainModule.Run;
    344402begin
    345   InitDatabase;
    346   //Database.Query('SELECT * FROM ssss');
    347 
     403  WebApp1.HTTPServer.DocumentRoot := ExtractFileDir(ParamStrUTF8(0));
    348404  WebApp1.Run;
    349405end;
Note: See TracChangeset for help on using the changeset viewer.