File: restserver.lpr

package info (click to toggle)
lazarus 2.2.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,980 kB
  • sloc: pascal: 1,944,919; xml: 357,634; makefile: 270,608; cpp: 57,115; sh: 3,249; java: 609; perl: 297; sql: 222; ansic: 137
file content (91 lines) | stat: -rw-r--r-- 2,457 bytes parent folder | download | duplicates (4)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
program restserver;

{$mode objfpc}{$H+}

uses
  {$ifdef unix}  cwstring, {$endif}
  sysutils, fphttpapp, dmRestBridge, custApp, fpwebfile;

Type

  { TRestHTTPApplication }

  TRestHTTPApplication = class(THTTPApplication)
  Public
    Procedure Usage(Msg : String);
    Procedure DoRun; override;
  end;

{ TRestHTTPApplication }

procedure TRestHTTPApplication.Usage(Msg: String);
begin
  if (Msg<>'') then
    Writeln('Error : ',msg);
  Writeln('Usage : ',ChangeFileExt(ExtractFileName(ParamStr(0)),''), '[Options]');
  Writeln('Where options is one or more of');
  Writeln('-h --help             this message');
  Writeln('-c --connection=file  File with connection definitions');
  Writeln('-f --files[=Dir]      Serve files from directory');
  Writeln('-i --ini=file         File with dispatched settings');
  Writeln('-p --port=Num         Listen on port');
  Writeln('-q --quiet            do not log anything (overrides .ini settings)');
  {$IFNDEF VER3_0}
    Writeln('-s --ssl              Use SSL protocol');
  {$Endif}
  Writeln('-v --verbose          Log more (includes SQL logging)');
  ExitCode:=Ord(Msg<>'');
end;

procedure TRestHTTPApplication.DoRun;

Var
  S,aDir,Header : String;

begin
  Application.Title:='restserver';
  S:=CheckOptions('hb:c:i:p:vqf::s',['help','base:','connection:','ini:','quiet','verbose','port:','file::','ssl']);
  if (S<>'') or HasOption('h','help') then
    begin
    Usage(S);
    Terminate;
    Exit;
    end;
  Port:=StrToIntDef(GetOptionvalue('p','port'),8080);
  Header:='Started REST server. listening on port: '+intToStr(Port);
  {$IFNDEF VER3_0}
  UseSSL:=Hasoption('s','ssl');
  if UseSSL then
    Header:=Header+'; Using SSL';
  {$Endif}
  if HasOption('f','file') then
     begin
     aDir:=GetOptionValue('f','file');
     if aDir='' then
       aDir:=ExtractFilePath(ParamStr(0))
     else
       ADir:=IncludeTrailingPathDelimiter(ADir);
     {$IFNDEF VER3_0}
     TSimpleFileModule.BaseDir:=aDir;
     TSimpleFileModule.RegisterDefaultRoute;
     {$else}
     RegisterFileLocation('files',aDir);
     {$Endif}
     Header:=Header+'; Serving files from: '+aDir;
     end;
  If IsConsole then
    Writeln(Header)
  else
    Log(etInfo,Header);
  inherited DoRun;
end;

begin
  FreeAndNil(Application);
  Application:=TRestHTTPApplication.Create(Nil);
  CustomApplication:=Application;
  Application.CreateForm(TRestDataModule,RestDataModule);
  Application.Initialize;
  Application.Run;
end.