1 | unit Core;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, LResources, HTTPServerCGI, HTTPSessionMySQL,
|
---|
7 | HTTPServer, SqlDatabase, INIFiles, DateUtils, WebPage, WebApp,
|
---|
8 | XmlClasses, HtmlClasses, Utils, ApplicationInfo, HTTPServerTCP,
|
---|
9 | HTTPSessionFile, User, Registry, ModularSystem, WebSession, LazUTF8;
|
---|
10 |
|
---|
11 | const
|
---|
12 | ConfigFile = 'Config.ini';
|
---|
13 | SystemVersionTable = 'SystemVersion';
|
---|
14 |
|
---|
15 | type
|
---|
16 |
|
---|
17 | { TCore }
|
---|
18 |
|
---|
19 | TCore = class(TDataModule)
|
---|
20 | ApplicationInfo1: TApplicationInfo;
|
---|
21 | WebApp1: TWebApp;
|
---|
22 | procedure WebApp1PageProduce(HandlerData: THTTPHandlerData);
|
---|
23 | private
|
---|
24 | procedure Show(Content: string);
|
---|
25 | public
|
---|
26 | CommonDatabase: TSqlDatabase;
|
---|
27 | Load: string;
|
---|
28 | Unload: string;
|
---|
29 | ShowRuntimeInfo: Boolean;
|
---|
30 | Charset: string;
|
---|
31 | Admin: string;
|
---|
32 | AdminEmail: string;
|
---|
33 | Keywords: string;
|
---|
34 | Style: string;
|
---|
35 | BaseURL: string;
|
---|
36 | FormatHTML: Boolean;
|
---|
37 | NetworkAddress: string;
|
---|
38 | NetworkPort: Integer;
|
---|
39 | MaxConnections: Integer;
|
---|
40 | DatabaseHostname: string;
|
---|
41 | DatabaseUserName: string;
|
---|
42 | DatabasePassword: string;
|
---|
43 | DatabaseSchema: string;
|
---|
44 | procedure LoadFromRegistry;
|
---|
45 | procedure SaveToRegistry;
|
---|
46 | procedure Run;
|
---|
47 | constructor Create(AOwner: TComponent); override;
|
---|
48 | destructor Destroy; override;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | var
|
---|
52 | Core: TCore;
|
---|
53 |
|
---|
54 |
|
---|
55 | implementation
|
---|
56 |
|
---|
57 | { TCore }
|
---|
58 |
|
---|
59 | constructor TCore.Create(AOwner: TComponent);
|
---|
60 | begin
|
---|
61 | inherited;
|
---|
62 | CommonDatabase := TSqlDatabase.Create(nil);
|
---|
63 |
|
---|
64 | LoadFromRegistry;
|
---|
65 | with WebApp1 do begin
|
---|
66 | if ServerType = stTCP then begin
|
---|
67 | THTTPServerTCP(HTTPServer).Socket.Address := NetworkAddress;
|
---|
68 | THTTPServerTCP(HTTPServer).Socket.Port := NetworkPort;
|
---|
69 | THTTPServerTCP(HTTPServer).MaxConnection := MaxConnections;
|
---|
70 | end;
|
---|
71 | end;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | destructor TCore.Destroy;
|
---|
75 | begin
|
---|
76 | //SaveToRegistry;
|
---|
77 | FreeAndNil(CommonDatabase);
|
---|
78 | inherited;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | procedure TCore.Show(Content: string);
|
---|
82 | begin
|
---|
83 | //HtmlDocument.Title := ;
|
---|
84 | (* BodyParam := '';
|
---|
85 | if Load <> '' then BodyParam := BodyParam + ' onload="' + Load + '"';
|
---|
86 | if Unload <> '' then BodyParam := BodyParam + ' onunload="' + Unload + '"';
|
---|
87 | Document.Encoding := Charset;
|
---|
88 | Document.Formated := FormatHTML;*)
|
---|
89 |
|
---|
90 |
|
---|
91 | (*
|
---|
92 | Title := GlobalTitle;
|
---|
93 | if Title <> '' then Title := Title + ' - ' + Title;
|
---|
94 | Document.TitleTag.SubElements := Title;
|
---|
95 | Title := '<span class="GlobalTitle">' + GlobalTitle + '</span>';
|
---|
96 | if Title <> '' then Title := Title + ' - ' + $this.Title;
|
---|
97 |
|
---|
98 | Keywords := TXMLTag.Create('meta');
|
---|
99 | Keywords.Attributes := array('name' => 'keywords', 'content' => implode(',', $this->Keywords));
|
---|
100 | Keywords.ShringEmpty := False;
|
---|
101 | Document.HeadTag.SubElements.Add(Keywords);
|
---|
102 |
|
---|
103 | Document.BodyTag.SubElements := array('<div class="TitlePanel">' + Title + '</div>',
|
---|
104 | TopMenu, PageMenu, Content, Footer);
|
---|
105 | Result := Document.GetOutput; *)
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TCore.WebApp1PageProduce(HandlerData: THTTPHandlerData);
|
---|
109 | var
|
---|
110 | NewSession: TWebSession;
|
---|
111 | begin
|
---|
112 | NewSession := TWebSession.Create;
|
---|
113 | NewSession.MainModule := Self;
|
---|
114 | NewSession.Assign(HandlerData);
|
---|
115 | NewSession.TimeStart := Now;
|
---|
116 | NewSession.Database.HostName := DatabaseHostName;
|
---|
117 | NewSession.Database.Password := DatabasePassword;
|
---|
118 | NewSession.Database.Database := DatabaseSchema;
|
---|
119 | NewSession.Database.UserName := DatabaseUserName;
|
---|
120 | NewSession.InitDatabase;
|
---|
121 | NewSession.BaseURL := BaseURL;
|
---|
122 | NewSession.Run;
|
---|
123 | HandlerData.Assign(NewSession);
|
---|
124 | end;
|
---|
125 |
|
---|
126 | procedure TCore.LoadFromRegistry;
|
---|
127 | const
|
---|
128 | SectionGeneral = 'General';
|
---|
129 | SectionDatabase = 'Database';
|
---|
130 | SectionHTTPServer = 'HTTPServer';
|
---|
131 | begin
|
---|
132 | with TIniFile.Create(ConfigFile) do
|
---|
133 | try
|
---|
134 | //RootKey := HKEY_CURRENT_USER;
|
---|
135 | //OpenKey(ApplicationInfo.RegistryKey, True);
|
---|
136 | Style := ReadString(SectionGeneral, 'Style', 'Basic');
|
---|
137 | BaseURL := ReadString(SectionGeneral, 'BaseURL', 'http://localhost');
|
---|
138 | DatabaseHostname := ReadString(SectionDatabase, 'DatabaseHostName', 'localhost');
|
---|
139 | DatabaseSchema := ReadString(SectionDatabase, 'DatabaseDatabase', 'web');
|
---|
140 | DatabaseUserName := ReadString(SectionDatabase, 'DatabaseUserName', 'user');
|
---|
141 | DatabasePassword := ReadString(SectionDatabase, 'DatabasePassword', 'password');
|
---|
142 | FormatHTML := ReadBool(SectionGeneral, 'FormatHTML', False);
|
---|
143 | WebApp1.LogException := ReadBool(SectionGeneral, 'LogException', False);
|
---|
144 | WebApp1.HTTPServer.ShowExceptions := ReadBool(SectionGeneral, 'ShowException', False);
|
---|
145 | NetworkAddress := ReadString(SectionHTTPServer, 'NetworkAddress', 'localhost');
|
---|
146 | NetworkPort := ReadInteger(SectionHTTPServer, 'NetworkPort', 80);
|
---|
147 | MaxConnections := ReadInteger(SectionHTTPServer, 'MaxConnections', 10);
|
---|
148 | finally
|
---|
149 | Free;
|
---|
150 | end;
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TCore.SaveToRegistry;
|
---|
154 | const
|
---|
155 | SectionGeneral = 'General';
|
---|
156 | SectionDatabase = 'Database';
|
---|
157 | SectionHTTPServer = 'HTTPServer';
|
---|
158 | begin
|
---|
159 | with TIniFile.Create(ConfigFile) do
|
---|
160 | try
|
---|
161 | //RootKey := HKEY_CURRENT_USER;
|
---|
162 | //OpenKey(ApplicationInfo.RegistryKey, True);
|
---|
163 | WriteString(SectionGeneral, 'Style', Style);
|
---|
164 | WriteString(SectionGeneral, 'BaseURL', BaseURL);
|
---|
165 | WriteString(SectionDatabase, 'DatabaseHostName', DatabaseHostname);
|
---|
166 | WriteString(SectionDatabase, 'DatabaseDatabase', DatabaseSchema);
|
---|
167 | WriteString(SectionDatabase, 'DatabaseUserName', DatabaseUserName);
|
---|
168 | WriteString(SectionDatabase, 'DatabasePassword', DatabasePassword);
|
---|
169 | WriteBool(SectionGeneral, 'FormatHTML', FormatHTML);
|
---|
170 | //WriteBool(SectionGeneral, 'ShowException', not WebApp1.LogException);
|
---|
171 | WriteString(SectionHTTPServer, 'NetworkAddress', NetworkAddress);
|
---|
172 | WriteInteger(SectionHTTPServer, 'NetworkPort', NetworkPort);
|
---|
173 | WriteInteger(SectionHTTPServer, 'MaxConnections', MaxConnections);
|
---|
174 | finally
|
---|
175 | Free;
|
---|
176 | end;
|
---|
177 | end;
|
---|
178 |
|
---|
179 | procedure TCore.Run;
|
---|
180 | begin
|
---|
181 | CommonDatabase.UserName := DatabaseUserName;
|
---|
182 | CommonDatabase.HostName := DatabaseHostname;
|
---|
183 | CommonDatabase.Database := DatabaseSchema;
|
---|
184 | CommonDatabase.Password := DatabasePassword;
|
---|
185 | CommonDatabase.Connect;
|
---|
186 |
|
---|
187 | WebApp1.HTTPServer.DocumentRoot := ExtractFileDir(ParamStrUTF8(0));
|
---|
188 | WebApp1.Run;
|
---|
189 | end;
|
---|
190 |
|
---|
191 | initialization
|
---|
192 |
|
---|
193 | {$I Core.lrs}
|
---|
194 |
|
---|
195 | end.
|
---|
196 |
|
---|