-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.pas
49 lines (40 loc) · 1.03 KB
/
main.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
program HTTPServer;
{$mode objfpc}
uses
fpjson, httpdefs, fphttpapp, httproute;
type
TVersionInfo = record
Version: string;
end;
function VersionInfoToJSON(const Info: TVersionInfo): TJSONObject;
begin
Result := TJSONObject.Create;
Result.Add('version', Info.Version);
end;
procedure VersionHandler(ARequest: TRequest; AResponse: TResponse);
var
VersionInfo: TVersionInfo;
ResponseJSON: TJSONObject;
begin
VersionInfo.Version := '1.0.0';
ResponseJSON := VersionInfoToJSON(VersionInfo);
try
AResponse.ContentType := 'application/json';
AResponse.Content := ResponseJSON.AsJSON;
AResponse.Code := 200;
finally
ResponseJSON.Free;
end;
end;
procedure NotFoundHandler(ARequest: TRequest; AResponse: TResponse);
begin
AResponse.Code := 404;
AResponse.Content := '404 Not Found';
end;
begin
HTTPRouter.RegisterRoute('/version', @VersionHandler);
HTTPRouter.RegisterRoute('/*', @NotFoundHandler);
Application.Port := 8000;
WriteLn('listening on http://localhost:8080');
Application.Run;
end.