demorestbridge.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. ErrorMsg: String;
  61. begin
  62. // quick check parameters
  63. ErrorMsg:=CheckOptions('hc:s:m:', ['help','config:','save-config:','max-requests:']);
  64. if ErrorMsg<>'' then begin
  65. ShowException(Exception.Create(ErrorMsg));
  66. Terminate;
  67. Exit;
  68. end;
  69. // parse parameters
  70. if HasOption('h', 'help') then begin
  71. WriteHelp;
  72. Terminate;
  73. Exit;
  74. end;
  75. Port:=3000;
  76. if HasOption('x','xml-only') then
  77. FDisp:=TXMLSQLDBRestDispatcher.Create(Self)
  78. else
  79. FDisp:=TSQLDBRestDispatcher.Create(Self);
  80. if HasOption('c', 'config') then
  81. FDisp.LoadFromFile(GetOptionValue('c', 'config'),[dioSkipReadSchemas])
  82. else
  83. begin
  84. // create a Default setup
  85. FAuth:=TRestBasicAuthenticator.Create(Self);
  86. // This is not the DB user !
  87. FAuth.DefaultUserName:='me';
  88. FAuth.DefaultPassword:='secret';
  89. FAuth.AuthenticateUserSQL.Text:='select uID from users where (uLogin=:UserName) and (uPassword=:Password)';
  90. FDisp.DispatchOptions:=FDisp.DispatchOptions+[rdoCustomView,rdoHandleCORS];
  91. FDisp.ExposeDatabase(TPQConnectionDef.TypeName,'localhost','expensetracker','You','YourSecret',Nil,[foFilter,foInInsert,foInUpdate,foOrderByDesc]);
  92. With FDisp.Schemas[0].Schema.Resources do
  93. begin
  94. FindResourceByName('users').Fields.FindByFieldName('uID').GeneratorName:='seqUsersID';
  95. FindResourceByName('projects').Fields.FindByFieldName('pID').GeneratorName:='seqProjectsID';
  96. FindResourceByName('expensetypes').Fields.FindByFieldName('etID').GeneratorName:='seqExpenseTypesID';
  97. FindResourceByName('expenses').Fields.FindByFieldName('eID').GeneratorName:='seqExpenseID';
  98. end;
  99. FDisp.Authenticator:=Fauth;
  100. if HasOption('s','save-config') then
  101. FDisp.SaveToFile(GetOptionValue('s','save-config'));
  102. end;
  103. // Mostly for debug purposes, to get e.g. a heap trace
  104. if HasOption('m','max-requests') then
  105. FMaxRequests:=StrToIntDef(GetOptionValue('m','max-requests'),0);
  106. FDisp.AfterGet:=@DoAfterRequest;
  107. FDisp.AfterPost:=@DoAfterRequest;
  108. FDisp.AfterPut:=@DoAfterRequest;
  109. FDisp.AfterDelete:=@DoAfterRequest;
  110. FDisp.Active:=True;
  111. Inherited DoRun;
  112. end;
  113. constructor TRestServerDemoApplication.Create(TheOwner: TComponent);
  114. begin
  115. inherited Create(TheOwner);
  116. StopOnException:=True;
  117. end;
  118. destructor TRestServerDemoApplication.Destroy;
  119. begin
  120. FreeAndNil(FDisp);
  121. FreeAndNil(FAuth);
  122. inherited Destroy;
  123. end;
  124. procedure TRestServerDemoApplication.WriteHelp;
  125. begin
  126. writeln('Usage: ', ExeName, ' [options]');
  127. Writeln('Where options is one or more of:');
  128. Writeln('-h --help this message');
  129. Writeln('-c --config=File Read config from .ini file');
  130. Writeln('-m --max-requests=N Server at most N requests, then quit.');
  131. Writeln('-s --saveconfig=File Write config to .ini file (ignored when -c or --config is used)');
  132. Writeln('-x --xml-only Only allow XML requests)');
  133. end;
  134. var
  135. Application: TRestServerDemoApplication;
  136. begin
  137. Application:=TRestServerDemoApplication.Create(nil);
  138. Application.Title:='SQLDB REST bridge Application';
  139. Application.Run;
  140. Application.Free;
  141. end.