-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServer.dpr
65 lines (53 loc) · 1.76 KB
/
WebServer.dpr
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
program WebServer;
{$APPTYPE CONSOLE}
uses
SysUtils, SynCommons, mORMot, mORMotHttpServer, Sempare.Template, Winapi.Windows, Winapi.ShellAPI;
type
// Note: TSQLRestServerFullMemory has no database support
TMyRestServer = class(TSQLRestServerFullMemory)
published
procedure Home(aContext: TSQLRestServerURIContext);
end;
TPageData = record
UserAgent: string;
TmplEngineName: string;
WebServrName: string;
end;
procedure TMyRestServer.Home(aContext: TSQLRestServerURIContext);
var
htmlTmpl, htmlResult: string;
pageData: TPageData;
begin
// Populate the page data
pageData.UserAgent := aContext.UserAgent;
pageData.TmplEngineName := 'Sempare Template Engine for Delphi';
pageData.WebServrName := 'mORMot v1';
// Load html template from file
htmlTmpl := StringFromFile(ExtractFilePath(ParamStr(0)) + 'Home-template.html');
// Render it
htmlResult := Template.Eval(htmlTmpl, pageData);
// Return the html page to the web browser
aContext.Returns(htmlResult, HTTP_SUCCESS, HTML_CONTENT_TYPE_HEADER);
end;
const
cWebServerAddr = 'http://localhost/www/home';
var
dbModel: TSQLModel;
restSvr: TMyRestServer;
httpSvr: TSQLHttpServer;
begin
dbModel := TSQLModel.Create([],'www');
restSvr := TMyRestServer.Create(dbModel);
dbModel.Owner := restSvr;
httpSvr := TSQLHttpServer.Create('80', [restSvr], '+', useHttpSocket, 8);
httpSvr.AccessControlAllowOrigin := '*';
WriteLn('');
WriteLn('Demonstrating mORMot 1''s method-based service serving html page rendered by Sempare Template Engine.');
WriteLn('');
WriteLn('Please visit ' + cWebServerAddr);
WriteLn('Press [Enter] to close the web server.');
ShellExecute(0, 'open', PChar(cWebServerAddr), nil, nil, SW_SHOWNORMAL);
ReadLn;
httpSvr.Free;
restSvr.Free;
end.