unit thWebserver;
interface
uses System.Classes, System.SysUtils, IdHTTPServer, IdContext, IdCustomHTTPServer, System.Generics.Collections;
Type
TOnLog=procedure(Sender: TObject; LogText:
string)
of object;
TTestData=class
private
FID: Integer;
FWert: Extended;
FBeschreibung:
string;
procedure SetID(
const Value: Integer);
procedure SetWert(
const Value: Extended);
procedure SetBeschreibung(
const Value:
string);
published
property ID: Integer
read FID
write SetID;
property Wert: Extended
read FWert
write SetWert;
property Beschreibung:
string read FBeschreibung
write SetBeschreibung;
end;
TTestDataList=class(TObjectList<TTestData>)
public
function ItemOfID(value: Integer): TTestData;
end;
TTestThread=class(TThread)
private
FOnLog: TOLog;
FTestDataList: TTestDataList;
procedure DoLog(LogText:
string);
procedure AddData(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
procedure ModifyData(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
procedure DeleteData(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
procedure OnCommand(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
public
constructor Create(Suspended: Boolean; Port: Integer; OnLog: TOnLog);
protected
procedure Execute;
override;
end;
implementation
{ TTestData }
procedure SetID(
const Value: Integer);
begin
FID := Value;
end;
procedure SetWert(
const Value: Extended);
begin
FWert := Value;
end;
procedure SetBeschreibung(
const Value:
string);
begin
FBeschreibung := Value;
end;
{ TTestDataList }
function ItemOfID(value: Integer): TTestData;
var
I: Integer;
begin
Result:=nil;
for I:=0
to self.Count-1
do
if self[i].ID=value
then
begin
Result:=self[i];
Break;
end;
end;
{ TTestThread }
procedure DoLog(LogText:
string);
begin
if Assigned(FOnLog)
then
synchronize(
procedure
begin
FOnLog(self, LogText);
end);
end;
procedure AddData(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
T: TTestData;
wert: Extended;
bescheibung:
string;
begin
wert:=StrToFloat(ARequestInfo.Params.Values['
wert']);
bescheibung:=ARequestInfo.Params.Values['
bescheibung'];
T:=TTestData.Create;
T.ID:=Random(10000);
T.Wert:=wert;
T.Beschreibung:=beschreibung;
FTestDataList.Add(T);
DoLog('
Add Data');
end;
procedure ModifyData(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
T: TTestData;
wert: Extended;
bescheibung:
string;
id: Integer;
begin
id:=StrToInt(ARequestInfo.Params.Values['
id']);
wert:=StrToFloat(ARequestInfo.Params.Values['
wert']);
bescheibung:=ARequestInfo.Params.Values['
bescheibung'];
T:=FTestDataList.ItemOfID(id);
if T<>
nil then
begin
T.Wert:=wert;
T.Beschreibung:=beschreibung;
end;
DoLog('
Modify Data');
end;
procedure DeleteData(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
T: TTestData;
id: Integer;
begin
id:=StrToInt(ARequestInfo.Params.Values['
id']);
T:=FTestDataList.ItemOfID(id);
if T<>
nil then
FTestDataList.Remove(T);
DoLog('
Delete Data');
end;
procedure OnCommand(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
if ARequestInfo.Document='
/AddItem'
then
AddData(AContext, ARequestInfo, AResponseInfo)
else
if ARequestInfo.Document='
/ModifyItem'
then
ModifyData(AContext, ARequestInfo, AResponseInfo)
else
if ARequestInfo.Document='
/DeleteItem'
then
DeleteData(AContext, ARequestInfo, AResponseInfo);
AResponseInfo.ContentType:='
text/text';
AResponseInfo.ContentText:='
OK';
AResponseInfo.ResponseNo:=200;
AResponseInfo.ResponseText:='
HTTP/1.0 200 OK';
end;
constructor Create(Suspended: Boolean; Port: Integer; OnLog: TOnLog);
begin
inherited Create(Suspended);
self.FPort:=Port;
self.FOnLog:=OnLog;
end;
procedure Execute;
var
FHttpServer: TIdHTTPServer;
begin
FHttpServer:=TIdHTTPServer.Create(
nil);
FTestDataList:=TTestDataList.Create(True);
try
FHTTPServer.DefaultPort:=FPort;
FHTTPServer.AutoStartSession:=True;
FHTTPServer.SessionState:=True;
FHTTPServer.ParseParams:=True;
FHTTPServer.SessionIDCookieName:='
TeestServer';
FHTTPServer.OnCommandGet:=OnCommand;
FHTTPServer.Active:=True;
FHTTPServer.StartListening;
While not self.Terminated
do
sleep(20);
FHTTPServer.StopListening;
finally
FHTTPServer.Free;
FTestDataList.Free;
end;
end;
end.