Changeset 105 for trunk/UCore.pas


Ignore:
Timestamp:
Oct 9, 2012, 1:58:45 PM (12 years ago)
Author:
chronos
Message:
  • Přidáno: Hlavní modul Base, který slouží jako základní rozhraní k aplikaci pro ostatní moduly. Modul System zajistí udržování seznamu instalovaných modulů v perzistentním úložišti v databázi.
  • Upraveno: Správce modulů ModuleManager je nyní použit pro každé komunikační spojení zvlášť.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r103 r105  
    1717
    1818type
    19   TCore = class;
    20 
    21 
    2219  { TCore }
    2320
     
    2724    procedure WebApp1PageProduce(HandlerData: THTTPHandlerData);
    2825  private
    29     FGeneratePage: TGeneratePageEvent;
    3026    procedure Show(Content: string);
    31     procedure RegisterModules;
    32     procedure CheckModules;
    3327  public
    3428    CommonDatabase: TSqlDatabase;
    35     ModuleManager: TModuleManager;
    3629    Load: string;
    3730    Unload: string;
     
    4740    NetworkPort: Integer;
    4841    MaxConnections: Integer;
    49     Pages: TPageList;
    5042    DatabaseHostname: string;
    5143    DatabaseUserName: string;
    5244    DatabasePassword: string;
    5345    DatabaseSchema: string;
    54     procedure GeneratePageDefault(ASession: TWebSession; Page: TWebPage);
    5546    procedure LoadFromRegistry;
    5647    procedure SaveToRegistry;
     
    5849    constructor Create(AOwner: TComponent); override;
    5950    destructor Destroy; override;
    60     property GeneratePage: TGeneratePageEvent read FGeneratePage write FGeneratePage;
    6151  end;
    6252
     
    6757implementation
    6858
    69 uses
    70   UModuleSystem, UModuleZdechovNET, UModuleUser, UPageAdmin, UModuleTV,
    71   UModuleFinance, UModuleNetwork, UModulePortal, UModuleIS;
    72 
    73 resourcestring
    74   SError = '<div>Error: %s</div>';
    75 
    7659
    7760{ TCore }
    78 
    79 procedure TCore.GeneratePageDefault(ASession: TWebSession; Page: TWebPage);
    80 var
    81   I: Integer;
    82 begin
    83   with ASession do begin
    84     HtmlDocument.ContentLanguage := 'cs';
    85     GlobalTitle := 'WebSystem';
    86 
    87       //Page.Page.OnProduce(HandlerData);
    88       HtmlDocument.Title := Page.Caption;
    89       with HtmlDocument.AsXmlDocument do
    90       try
    91         Formated := TCore(MainModule).FormatHTML;
    92         Response.Content.WriteString(AsString);
    93       finally
    94         Free;
    95       end;
    96     end;
    97 end;
    9861
    9962constructor TCore.Create(AOwner: TComponent);
    10063begin
    10164  inherited;
    102   ModuleManager := TModuleManager.Create(nil);
    103   Pages := TPageList.Create;
    10465  CommonDatabase := TSqlDatabase.Create(nil);
    10566
    10667  LoadFromRegistry;
    107   with Pages do begin
    108     RegisterPage(TPageAdmin, PageAdmin, 'administration');
    109   end;
    11068  with WebApp1 do begin
    11169    if ServerType = stTCP then begin
     
    12179  //SaveToRegistry;
    12280  FreeAndNil(CommonDatabase);
    123   ModuleManager.StopAll;
    124   FreeAndNil(ModuleManager);
    125   FreeAndNil(Pages);
    12681  inherited;
    12782end;
     
    154109end;
    155110
    156 procedure TCore.RegisterModules;
    157 begin
    158   with ModuleManager do begin
    159     RegisterModule(TModuleSystem.Create(nil), True);
    160     RegisterModule(TModuleUser.Create(nil), True);
    161     RegisterModule(TModuleZdechovNET.Create(nil), True);
    162     RegisterModule(TModuleTV.Create(nil), True);
    163     RegisterModule(TModuleFinance.Create(nil), True);
    164     RegisterModule(TModuleNetwork.Create(nil), True);
    165     RegisterModule(TModulePortal.Create(nil), True);
    166     RegisterModule(TModuleIS.Create(nil), True);
    167   end;
    168 end;
    169 
    170 procedure TCore.CheckModules;
    171 var
    172   DbRows: TDbRows;
    173   Module: TModule;
    174   I: Integer;
    175 begin
    176   try
    177     DbRows := TDbRows.Create;
    178     CommonDatabase.Query(DbRows, 'SELECT * FROM INFORMATION_SCHEMA.TABLES ' +
    179       'WHERE TABLE_SCHEMA = "' + CommonDatabase.Database + '" AND TABLE_NAME = "SystemModule"');
    180     Module := ModuleManager.FindModuleByName('System');
    181     if DbRows.Count > 0 then Module.SetInstalledState(True)
    182       else Module.SetInstalledState(False);
    183   finally
    184     DbRows.Free;
    185   end;
    186 end;
    187 
    188111procedure TCore.WebApp1PageProduce(HandlerData: THTTPHandlerData);
    189112var
    190   FileName: string;
    191   PageName: string;
    192   Page: TRegistredPage;
    193113  NewSession: TWebSession;
    194   BaseUrlParts: TListString;
    195114begin
    196   with HandlerData do begin
    197     try
    198       BaseUrlParts := TListString.Create;
    199       BaseUrlParts.Explode(BaseURL, '/', StrToStr);
    200       while (BaseUrlParts.Count > 0) and (Request.Path.Count > 0) and
    201          (BaseUrlParts[0] = Request.Path[0]) do begin
    202            BaseUrlParts.Delete(0);
    203            Request.Path.Delete(0);
    204          end;
    205     finally
    206       BaseUrlParts.Free;
    207     end;
    208     if Request.Path.Count > 0 then PageName := Request.Path[0]
    209       else PageName := '';
    210     Page := Pages.FindByName(PageName);
    211     if Assigned(Page) then begin
    212       NewSession := TWebSession.Create;
    213       NewSession.MainModule := Self;
    214       NewSession.Assign(HandlerData);
    215       NewSession.TimeStart := Now;
    216       NewSession.Database.HostName := DatabaseHostName;
    217       NewSession.Database.Password := DatabasePassword;
    218       NewSession.Database.Database := DatabaseSchema;
    219       NewSession.Database.UserName := DatabaseUserName;
    220       NewSession.InitDatabase;
    221       try
    222         Page.Page.OnProduce(NewSession);
    223       except
    224         on E: Exception do begin
    225           THTMLString(TWebSession(NewSession).HtmlDocument.Body.SubItems.AddNew(THtmlString.Create)).
    226             Text := Format(SError, [E.Message]);
    227           if Assigned(GeneratePage) then GeneratePage(NewSession, Page.Page)
    228             else GeneratePageDefault(NewSession, Page.Page);
    229         end;
    230       end;
    231       HandlerData.Assign(NewSession);
    232     end else begin
    233       WebApp1.HTTPServer.FileResponse(HandlerData)
    234     end;
    235   end;
     115  NewSession := TWebSession.Create;
     116  NewSession.MainModule := Self;
     117  NewSession.Assign(HandlerData);
     118  NewSession.TimeStart := Now;
     119  NewSession.Database.HostName := DatabaseHostName;
     120  NewSession.Database.Password := DatabasePassword;
     121  NewSession.Database.Database := DatabaseSchema;
     122  NewSession.Database.UserName := DatabaseUserName;
     123  NewSession.InitDatabase;
     124  NewSession.BaseURL := BaseURL;
     125  NewSession.Run;
     126  HandlerData.Assign(NewSession);
    236127end;
    237128
     
    296187  CommonDatabase.Connect;
    297188
    298   RegisterModules;
    299   CheckModules;
    300   ModuleManager.StartInstalled;
    301 
    302189  WebApp1.HTTPServer.DocumentRoot := ExtractFileDir(ParamStrUTF8(0));
    303190  WebApp1.Run;
Note: See TracChangeset for help on using the changeset viewer.