Changeset 91 for branches/DirectWeb/UWebServer.pas
- Timestamp:
- Dec 21, 2009, 1:19:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DirectWeb/UWebServer.pas
r89 r91 18 18 type 19 19 20 { TDatabasePool }21 20 22 TDatabasePool = class(TThreadedPool)23 private24 FActive: Boolean;25 procedure SetActive(const AValue: Boolean);26 public27 property Active: Boolean read FActive write SetActive;28 public29 HostName: string;30 Schema: string;31 UserName: string;32 Password: string;33 constructor Create;34 destructor Destroy; override;35 end;36 21 37 22 { TWebServer } … … 39 24 TWebServer = class 40 25 private 41 SessionStorage: TFileHTTPSessionStorage;42 procedure ServerInfo(HandlerData: THTTPHandlerData);43 26 public 44 27 HTTPServer: THTTPServer; 45 DatabasePool: TDatabasePool;46 47 procedure ViewList(HandlerData: THTTPHandlerData);48 procedure ViewItem(HandlerData: THTTPHandlerData);49 procedure SendIndex(HandlerData: THTTPHandlerData);50 procedure WriteHeader(Stream: TMemoryStreamEx);51 procedure WriteFooter(Stream: TMemoryStreamEx);52 28 constructor Create; 53 29 destructor Destroy; override; 54 procedure LoadConfiguration;55 procedure Run;56 30 end; 57 31 58 32 implementation 59 33 60 procedure TWebServer.LoadConfiguration;61 var62 Config: TXMLDocument;63 I1: Integer;64 I2: Integer;65 I3: Integer;66 begin67 if FileExists(ConfigFileName) then begin68 ReadXMLFile(Config, ConfigFileName);69 for I1 := 0 to Config.ChildNodes.Count - 1 do70 with Config.ChildNodes[I1] do begin71 if NodeName = 'configuration' then72 for I2 := 0 to ChildNodes.Count - 1 do73 with ChildNodes[I2] do begin74 if NodeName = 'database' then75 for I3 := 0 to ChildNodes.Count - 1 do76 with ChildNodes[I3] do begin77 if NodeName = 'hostname' then78 DatabasePool.HostName := TextContent;79 if NodeName = 'schema' then80 DatabasePool.Schema := TextContent;81 if NodeName = 'username' then82 DatabasePool.UserName := TextContent;83 if NodeName = 'password' then84 DatabasePool.Password := TextContent;85 end;86 if NodeName = 'server' then87 for I3 := 0 to ChildNodes.Count - 1 do88 with ChildNodes[I3] do begin89 if NodeName = 'address' then90 HTTPServer.Socket.Address := TextContent;91 if NodeName = 'port' then92 HTTPServer.Socket.Port := StrToInt(TextContent);93 end;94 end;95 end;96 Config.Destroy;97 end;98 end;99 100 procedure TWebServer.Run;101 var102 Command: string;103 begin104 LoadConfiguration;105 DatabasePool.Active := True;106 WriteLn('WoW hosting web server');107 with HTTPServer do begin108 Socket.Active := True;109 WriteLn('Accepting connections.');110 repeat111 Write('Server command: ');112 ReadLn(Command);113 until Command = 'quit';114 //WaitForFinish;115 end;116 end;117 118 procedure TWebServer.ViewList(HandlerData: THTTPHandlerData);119 var120 SqlDatabase: TSqlDatabase;121 DbRows: TDbRows;122 I, II: integer;123 OrderColumn: string;124 OrderDirection: string;125 Title: string;126 LinkQuery: TQueryParameterList;127 PageList: TPageList;128 begin129 SqlDatabase := TSqlDatabase(DatabasePool.Acquire);130 with HandlerData, Response, Stream, SqlDatabase do131 begin132 WriteHeader(Stream);133 134 WriteString('<div align="center">');135 136 // Prepare table paging137 DbRows := Query('SELECT COUNT(*) FROM ' + Request.Query.Values['Table']);138 PageList := TPageList.Create;139 PageList.HandlerData := HandlerData;140 with PageList do begin141 TotalCount := StrToInt(DbRows[0].ValuesAtIndex[0]);142 ItemPerPage := 20;143 NavigatorVisibleItems := 5;144 Process;145 end;146 DbRows.Destroy;147 148 //WriteString(Request.Query.Values['Table']);149 OrderColumn := Request.Query.Values['OrderCol'];150 OrderDirection := Request.Query.Values['OrderDir'];151 if OrderDirection = '1' then152 OrderDirection := 'ASC'153 else154 OrderDirection := 'DESC';155 DbRows := Query('SELECT * FROM ' + Request.Query.Values['Table'] +156 ' ORDER BY ' + OrderColumn + ' ' + OrderDirection + PageList.SQLLimit);157 158 LinkQuery := TQueryParameterList.Create;159 LinkQuery.Assign(Request.Query);160 161 WriteString('<strong>Seznam typů karet</strong><br/>');162 WriteString(PageList.Output);163 WriteString('<table><tr>');164 if DbRows.Count > 0 then165 for I := 0 to DbRows[0].Count - 1 do166 begin167 Title := DbRows[0].Names[I];168 LinkQuery.Values['OrderCol'] := Title;169 if Title = OrderColumn then170 LinkQuery.Values['OrderDir'] :=171 IntToStr(1 - StrToInt(Request.Query.Values['OrderDir']))172 else173 LinkQuery.Values['OrderDir'] := Request.Query.Values['OrderDir'];174 Title := '<a href="?' + LinkQuery.Syntetize + '">' + Title + '</a>';175 176 WriteString('<th>' + Title + '</th>');177 end;178 WriteString('</tr>');179 180 LinkQuery.Destroy;181 for II := 0 to DbRows.Count - 1 do182 begin183 WriteString('<tr>');184 for I := 0 to DbRows[II].Count - 1 do185 WriteString('<td>' + DbRows[II].ValuesAtIndex[I] + '</td>');186 WriteString('</tr>');187 end;188 WriteString('</table>');189 WriteString(PageList.Output);190 WriteString('</div>');191 192 DbRows.Destroy;193 WriteFooter(Stream);194 end;195 PageList.Destroy;196 DatabasePool.Release(SqlDatabase);197 end;198 199 procedure TWebServer.ViewItem(HandlerData: THTTPHandlerData);200 var201 SqlDatabase: TSqlDatabase;202 DbRows: TDbRows;203 I, II: Integer;204 OrderColumn: string;205 OrderDirection: string;206 Title: string;207 LinkQuery: TQueryParameterList;208 begin209 SqlDatabase := TSqlDatabase(DatabasePool.Acquire);210 with HandlerData, Response, Stream, SqlDatabase do211 begin212 WriteHeader(Stream);213 DbRows := Query('SELECT * FROM ' + Request.Query.Values['Table'] +214 ' WHERE Id=' + Request.Query.Values['Id']);215 if DbRows.Count > 0 then begin216 WriteString('<strong>Zobrazení položky</strong>');217 WriteString('<table><tr><th>Vlastnost</th><th>Hodnota</th></tr>');218 for I := 0 to DbRows[0].Count - 1 do begin219 WriteString('<tr><td>' + DbRows[0].Names[I] + '</td><td>' +220 DbRows[0].ValuesAtIndex[I] + '</td></tr>');221 end;222 end else WriteString('Položka nenalezena.');223 WriteString('</table>');224 DbRows.Destroy;225 WriteFooter(Stream);226 end;227 DatabasePool.Release(SqlDatabase);228 end;229 230 procedure TWebServer.ServerInfo(HandlerData: THTTPHandlerData);231 var232 I: Integer;233 begin234 with HandlerData, Response.Stream do begin235 //Response.Cookies.Values['Test'] := 'Halo';236 //Response.Cookies.Values['Test2'] := 'Halo2';237 238 //HTTPServer.SessionHandler.Variables.Values['Session1'] := 'Value1';239 //HTTPServer.SessionHandler.Variables.Values['Session2'] := 'Value2';240 241 WriteString('<a href="?ServerInfo">Refresh</a>');242 243 WriteString('<h5>Request HTTP headers</h5>');244 for I := 0 to Request.Headers.Count - 1 do begin;245 WriteString(Request.Headers.Strings[I] + '<br/>');246 end;247 248 WriteString('<h5>Request HTTP cookies</h5>');249 for I := 0 to Request.Cookies.Count - 1 do begin;250 WriteString(Request.Cookies.Strings[I] + '<br/>');251 end;252 253 WriteString('<h5>Session variables</h5>');254 for I := 0 to Session.Count - 1 do begin;255 WriteString(Session.Strings[I] + '<br/>');256 end;257 258 WriteString('<h5>Response HTTP headers</h5>');259 with Response.Stream do260 for I := 0 to Response.Headers.Count - 1 do begin;261 WriteString(Response.Headers.Strings[I] + '<br/>');262 end;263 end;264 end;265 266 procedure TWebServer.SendIndex(HandlerData: THTTPHandlerData);267 begin268 with HandlerData, Response, Stream do269 begin270 WriteHeader(Stream);271 WriteString('Index');272 WriteFooter(Stream);273 end;274 end;275 276 procedure TWebServer.WriteHeader(Stream: TMemoryStreamEx);277 begin278 with Stream do279 begin280 WriteString('<?xml version="1.0" encoding="UTF-8"?>');281 WriteString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">');282 WriteString('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">');283 WriteString('<head>');284 WriteString('<title>');285 WriteString('Dispečink výtahů');286 WriteString('</title>');287 WriteString('<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>');288 WriteString('<link rel="stylesheet" href="Style.css" type="text/css" media="all"/>');289 WriteString('<script type="text/javascript" src="Application/Style/Custom/Global.js">');290 WriteString('</script>');291 WriteString('<script type="text/javascript" src="Base/Style/jquery.js">');292 WriteString('</script>');293 WriteString('</head>');294 WriteString('<body>');295 end;296 end;297 298 procedure TWebServer.WriteFooter(Stream: TMemoryStreamEx);299 begin300 with Stream do301 begin302 WriteString('</body>');303 WriteString('</html>');304 end;305 end;306 307 34 constructor TWebServer.Create; 308 35 begin 309 36 inherited Create; 310 DatabasePool := TDatabasePool.Create;311 DatabasePool.TotalCount := 20;312 313 SessionStorage := TFileHTTPSessionStorage.Create;314 37 HTTPServer := THTTPServer.Create; 315 38 with HTTPServer, Socket do begin 316 SessionStorage := Self.SessionStorage;317 DocumentRoot := 'Data';318 with RequestHandlerList do begin319 Add('/', SendIndex);320 Add('/index.htm', SendIndex);321 Add('/index.html', SendIndex);322 Add('/logo.png', FileResponse);323 Add('/Style.css', FileResponse);324 Add('/ViewList', ViewList);325 Add('/ViewItem', ViewItem);326 Add('/ServerInfo', ServerInfo);327 end;328 39 end; 329 40 end; … … 331 42 destructor TWebServer.Destroy; 332 43 begin 333 DatabasePool.Destroy;334 44 HTTPServer.Destroy; 335 SessionStorage.Destroy;336 45 inherited Destroy; 337 46 end; 338 47 339 { TDatabasePool }340 341 procedure TDatabasePool.SetActive(const AValue: Boolean);342 var343 I: Integer;344 begin345 if not FActive and AValue then begin346 for I := 0 to TotalCount - 1 do347 with TThreadedPoolItem(Items[I]) do begin348 Item := TSqlDatabase.Create;349 with TSqlDatabase(Item) do begin350 HostName := Self.HostName;351 UserName := Self.UserName;352 Password := Self.Password;353 Database := Self.Schema;354 Connect;355 end;356 end;357 end else358 if FActive and not AValue then begin359 360 end;361 FActive := AValue;362 end;363 364 constructor TDatabasePool.Create;365 begin366 inherited;367 end;368 369 destructor TDatabasePool.Destroy;370 begin371 inherited Destroy;372 end;373 48 374 49 end.
Note:
See TracChangeset
for help on using the changeset viewer.