-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTinyApiExample.pas
More file actions
48 lines (40 loc) · 1.14 KB
/
Copy pathTinyApiExample.pas
File metadata and controls
48 lines (40 loc) · 1.14 KB
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
program TinyApiExample;
{
Copyright 2018, Marcus Fernstrom
License MIT
}
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}cthreads, cmem,{$ENDIF}
SysUtils, fphttpapp, httpdefs, httproute, fpjson;
procedure jsonEndpoint(aRequest : TRequest; aResponse : TResponse);
var
jObject : TJSONObject;
begin
jObject := TJSONObject.Create;
try
jObject.Booleans['success'] := true;
jObject.Strings['data'] := 'This is a JSON object';
jObject.Integers['numbers'] := 12345;
aResponse.Content := jObject.AsJSON;
aResponse.ContentType := 'application/json';
aResponse.SendContent;
finally
jObject.Free;
end;
end;
procedure textEndpoint(aRequest : TRequest; aResponse : TResponse);
begin
aResponse.Content := 'This is the default response if no other routes match.';
aResponse.ContentType := 'text/plain';
aResponse.SendContent;
end;
begin
Application.Port := 9080;
HTTPRouter.RegisterRoute('/json', @jsonEndpoint);
HTTPRouter.RegisterRoute('/text', @textEndpoint, true);
Application.Threaded := true;
Application.Initialize;
WriteLn('Server is ready at http://localhost:' + IntToStr(Application.Port));
Application.Run;
end.