demorestbridge.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.
  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. { TXMLSQLDBRestDispatcher }
  22. TXMLSQLDBRestDispatcher = class(TSQLDBRestDispatcher)
  23. Function CreateOutputStreamer(IO: TRestIO): TRestOutputStreamer; override;
  24. end;
  25. { TRestServerDemoApplication }
  26. TRestServerDemoApplication = class(THTTPApplication)
  27. private
  28. procedure DoAfterRequest(Sender: TObject; aConn: TSQLConnection; aResource: TSQLDBRestResource);
  29. Protected
  30. FAuth : TRestBasicAuthenticator;
  31. FDisp : TSQLDBRestDispatcher;
  32. FRequestCount,
  33. FMaxRequests : integer;
  34. protected
  35. procedure DoRun; override;
  36. public
  37. constructor Create(TheOwner: TComponent); override;
  38. destructor Destroy; override;
  39. procedure WriteHelp; virtual;
  40. end;
  41. { TXMLSQLDBRestDispatcher }
  42. function TXMLSQLDBRestDispatcher.CreateOutputStreamer(IO: TRestIO): TRestOutputStreamer;
  43. begin
  44. io.Response.ContentStream:=TMemoryStream.Create;
  45. io.Response.FreeContentStream:=True;
  46. Result:=TXMLOutputStreamer.Create(IO.Response.ContentStream,Strings,Statuses, @IO.DoGetVariable);
  47. end;
  48. { TRestServerDemoApplication }
  49. procedure TRestServerDemoApplication.DoAfterRequest(Sender: TObject; aConn: TSQLConnection; aResource: TSQLDBRestResource);
  50. begin
  51. inc(FRequestCount);
  52. if (FMaxRequests>0) and (FRequestCount>=FMaxRequests) then
  53. begin
  54. DoLog(etInfo,'Maximum requests reached');
  55. Terminate;
  56. end;
  57. end;
  58. procedure TRestServerDemoApplication.DoRun;
  59. var
  60. UN,PWD,
  61. ErrorMsg: String;
  62. begin
  63. // quick check parameters
  64. ErrorMsg:=CheckOptions('hc:s:m:p:u:w:', ['help','config:','save-config:','max-requests:','port:','user:','password:']);
  65. if ErrorMsg<>'' then begin
  66. ShowException(Exception.Create(ErrorMsg));
  67. Terminate;
  68. Exit;
  69. end;
  70. // parse parameters
  71. if HasOption('h', 'help') then begin
  72. WriteHelp;
  73. Terminate;
  74. Exit;
  75. end;
  76. Port:=3000;
  77. if HasOption('p','port') then
  78. Port:=StrToIntDef(GetOptionValue('p','port'),Port);
  79. if HasOption('x','xml-only') then
  80. FDisp:=TXMLSQLDBRestDispatcher.Create(Self)
  81. else
  82. FDisp:=TSQLDBRestDispatcher.Create(Self);
  83. if HasOption('c', 'config') then
  84. FDisp.LoadFromFile(GetOptionValue('c', 'config'),[{dioSkipReadSchemas}])
  85. else
  86. begin
  87. // create a Default setup
  88. FAuth:=TRestBasicAuthenticator.Create(Self);
  89. // This is not the DB user !
  90. FAuth.DefaultUserName:='me';
  91. FAuth.DefaultPassword:='secret';
  92. FAuth.AuthenticateUserSQL.Text:='select uID from users where (uLogin=:UserName) and (uPassword=:Password)';
  93. FDisp.DispatchOptions:=FDisp.DispatchOptions+[rdoCustomView,rdoHandleCORS];
  94. UN:=GetOptionValue('u','user');
  95. if UN='' then
  96. UN:='You';
  97. PWD:=GetOPtionValue('w','password');
  98. if PWD='' then
  99. PWD:='Secret';
  100. FDisp.ExposeDatabase(TPQConnectionDef.TypeName,'localhost','expensetracker',UN,PWD,Nil,[foFilter,foInInsert,foInUpdate,foOrderByDesc]);
  101. With FDisp.Schemas[0].Schema.Resources do
  102. begin
  103. FindResourceByName('users').Fields.FindByFieldName('uID').GeneratorName:='seqUsersID';
  104. FindResourceByName('projects').Fields.FindByFieldName('pID').GeneratorName:='seqProjectsID';
  105. FindResourceByName('expensetypes').Fields.FindByFieldName('etID').GeneratorName:='seqExpenseTypesID';
  106. FindResourceByName('expenses').Fields.FindByFieldName('eID').GeneratorName:='seqExpenseID';
  107. end;
  108. FDisp.Authenticator:=Fauth;
  109. if HasOption('s','save-config') then
  110. FDisp.SaveToFile(GetOptionValue('s','save-config'));
  111. end;
  112. // Mostly for debug purposes, to get e.g. a heap trace
  113. if HasOption('m','max-requests') then
  114. FMaxRequests:=StrToIntDef(GetOptionValue('m','max-requests'),0);
  115. FDisp.AfterGet:=@DoAfterRequest;
  116. FDisp.AfterPost:=@DoAfterRequest;
  117. FDisp.AfterPut:=@DoAfterRequest;
  118. FDisp.AfterDelete:=@DoAfterRequest;
  119. FDisp.Active:=True;
  120. Inherited DoRun;
  121. end;
  122. constructor TRestServerDemoApplication.Create(TheOwner: TComponent);
  123. begin
  124. inherited Create(TheOwner);
  125. StopOnException:=True;
  126. end;
  127. destructor TRestServerDemoApplication.Destroy;
  128. begin
  129. FreeAndNil(FDisp);
  130. FreeAndNil(FAuth);
  131. inherited Destroy;
  132. end;
  133. procedure TRestServerDemoApplication.WriteHelp;
  134. begin
  135. writeln('Usage: ', ExeName, ' [options]');
  136. Writeln('Where options is one or more of:');
  137. Writeln('-h --help this message');
  138. Writeln('-c --config=File Read config from .ini file');
  139. Writeln('-m --max-requests=N Server at most N requests, then quit.');
  140. Writeln('-p --port=N TCP/IP Port to listen on (default 3000)');
  141. Writeln('-s --saveconfig=File Write config to .ini file (ignored when -c or --config is used)');
  142. Writeln('-u --user=USER Database connection username');
  143. Writeln('-w --password=PWD Password for database connection user');
  144. Writeln('-x --xml-only Only allow XML requests)');
  145. end;
  146. var
  147. Application: TRestServerDemoApplication;
  148. begin
  149. Application:=TRestServerDemoApplication.Create(nil);
  150. Application.Title:='SQLDB REST bridge Application';
  151. Application.Run;
  152. Application.Free;
  153. end.