demorestbridge.pp 5.2 KB

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