restreadonly.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2019 by the Free Pascal development team
  4. SQLDB REST rest bridge demo applocation: readonly access
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program demorestbridge;
  12. {$mode objfpc}{$H+}
  13. uses
  14. {$IFDEF UNIX}{$IFDEF UseCThreads}
  15. cthreads,
  16. {$ENDIF}{$ENDIF}
  17. Classes, SysUtils, CustApp, sqldbrestbridge, fphttpapp, IBConnection, odbcconn, mysql55conn, mysql56conn, pqconnection,
  18. mssqlconn, oracleconnection, sqldbrestxml, sqldbrestio, sqldbrestschema, sqldbrestdata, sqldbrestjson, sqldbrestcsv, sqldbrestcds,
  19. sqldbrestado, sqldbrestconst, sqldbrestauth, sqldbrestini, sqldb, sqldbrestauthini;
  20. type
  21. { TRestServerDemoApplication }
  22. TRestServerDemoApplication = class(THTTPApplication)
  23. private
  24. procedure DisplayInfo;
  25. function SetUpConnection: TSQLDBRestConnection;
  26. Protected
  27. FDisp : TSQLDBRestDispatcher;
  28. protected
  29. procedure DoRun; override;
  30. public
  31. constructor Create(TheOwner: TComponent); override;
  32. destructor Destroy; override;
  33. procedure WriteHelp; virtual;
  34. end;
  35. { TRestServerDemoApplication }
  36. procedure TRestServerDemoApplication.DoRun;
  37. var
  38. ErrorMsg: String;
  39. C : TSQLDBRestConnection;
  40. I : Integer;
  41. begin
  42. // quick check parameters
  43. ErrorMsg:=CheckOptions('hc:p:q', ['help','config:','port:','quiet']);
  44. if ErrorMsg<>'' then
  45. begin
  46. ShowException(Exception.Create(ErrorMsg));
  47. Terminate;
  48. Exit;
  49. end;
  50. // parse parameters
  51. if HasOption('h', 'help') then
  52. begin
  53. WriteHelp;
  54. Terminate;
  55. Exit;
  56. end;
  57. Port:=StrToIntDef(GetOptionValue('p','port'),3000);
  58. // Set up dispatcher
  59. // Create connection
  60. C:=SetUpConnection;
  61. // Allow filtering
  62. FDisp.ExposeConnection(C,Nil,[foFilter]);
  63. // Mark resources as read-only
  64. With FDisp.Schemas[0].Schema.Resources do
  65. For I:=0 to Count-1 do
  66. Resources[i].AllowedOperations:=[roGet,roHead,roOptions];
  67. FDisp.Active:=true;
  68. if not HasOption('q','quiet') then
  69. DisplayInfo;
  70. Inherited DoRun;
  71. end;
  72. constructor TRestServerDemoApplication.Create(TheOwner: TComponent);
  73. begin
  74. inherited Create(TheOwner);
  75. // Create dispatcher
  76. FDisp:=TSQLDBRestDispatcher.Create(Self);
  77. StopOnException:=True;
  78. end;
  79. destructor TRestServerDemoApplication.Destroy;
  80. begin
  81. FreeAndNil(FDisp);
  82. inherited Destroy;
  83. end;
  84. Function TRestServerDemoApplication.SetUpConnection : TSQLDBRestConnection;
  85. Var
  86. C : TSQLDBRestConnection;
  87. FN : String;
  88. begin
  89. // Create with empty connection config.
  90. C:=FDisp.Connections.AddConnection('','','','','');
  91. C.name:='connection';
  92. // Read connection settings if available
  93. FN:=GetOptionValue('c', 'config');
  94. if FN='' then
  95. FN:='connection.ini';
  96. if FileExists(FN) then
  97. C.LoadFromIniFile(FN,'database',[])
  98. else
  99. begin
  100. // Or set in code.
  101. C.ConnectionType:=TPQConnectionDef.TypeName;
  102. C.DatabaseName:='fpctest';
  103. C.HostName:='localhost';
  104. C.UserName:='user';
  105. C.Password:='secret';
  106. end;
  107. Result:=C;
  108. end;
  109. procedure TRestServerDemoApplication.DisplayInfo;
  110. Var
  111. I : integer;
  112. L : TStrings;
  113. C : TSQLDBRestConnection;
  114. begin
  115. Writeln('Listening on port : ',Port);
  116. Writeln('local URL : http://localhost:',Port,'/'+FDisp.BasePath);
  117. C:=FDisp.Connections[0];
  118. Writeln('Database connection : Type=',C.ConnectionType,', Host: ',C.HostName,', Database: ',C.DatabaseName,', User: ',C.UserName);
  119. Writeln('Available resources :');
  120. With FDisp.Schemas[0].Schema.Resources do
  121. For I:=0 to Count-1 do
  122. Writeln(' ',Resources[i].ResourceName);
  123. L:=TStringList.Create;
  124. try
  125. Writeln('Available output formats:');
  126. TStreamerFactory.Instance.GetStreamerList(L,rstOutput);
  127. For I:=0 to L.Count-1 do
  128. Writeln(' ',L[i]);
  129. Writeln('Available Input formats:');
  130. L.Clear;
  131. TStreamerFactory.Instance.GetStreamerList(L,rstOutput);
  132. For I:=0 to L.Count-1 do
  133. Writeln(' ',L[i]);
  134. Finally
  135. L.Free;
  136. end;
  137. end;
  138. procedure TRestServerDemoApplication.WriteHelp;
  139. begin
  140. writeln('Usage: ', ExeName, ' [options]');
  141. Writeln('Where options is one or more of:');
  142. Writeln('-h --help this message');
  143. Writeln('-c --config=File Read connection data from .ini file');
  144. Writeln('-p --port=N Set listen port number');
  145. Writeln('-q --quiet Do not display info');
  146. end;
  147. var
  148. Application: TRestServerDemoApplication;
  149. begin
  150. Application:=TRestServerDemoApplication.Create(nil);
  151. Application.Title:='SQLDB REST bridge Application - Readonly';
  152. Application.Run;
  153. Application.Free;
  154. end.