demorestbridge.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. program demorestbridge;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}{$IFDEF UseCThreads}
  5. cthreads,
  6. {$ENDIF}{$ENDIF}
  7. Classes, SysUtils, CustApp, sqldbrestbridge, fphttpapp, IBConnection, odbcconn, mysql55conn, mysql56conn, pqconnection,
  8. mssqlconn, oracleconnection, sqldbrestxml, sqldbrestio, sqldbrestschema, sqldbrestdata, sqldbrestjson, sqldbrestcsv, sqldbrestcds,
  9. sqldbrestconst, sqldbrestauth, sqldbrestini, sqldb, sqldbrestauthini
  10. ;
  11. type
  12. { TXMLSQLDBRestDispatcher }
  13. TXMLSQLDBRestDispatcher = class(TSQLDBRestDispatcher)
  14. Function CreateOutputStreamer(IO: TRestIO): TRestOutputStreamer; override;
  15. end;
  16. { TRestServerDemoApplication }
  17. TRestServerDemoApplication = class(THTTPApplication)
  18. private
  19. procedure DoAfterRequest(Sender: TObject; aConn: TSQLConnection; aResource: TSQLDBRestResource);
  20. Protected
  21. FAuth : TRestBasicAuthenticator;
  22. FDisp : TSQLDBRestDispatcher;
  23. FRequestCount,
  24. FMaxRequests : integer;
  25. protected
  26. procedure DoRun; override;
  27. public
  28. constructor Create(TheOwner: TComponent); override;
  29. destructor Destroy; override;
  30. procedure WriteHelp; virtual;
  31. end;
  32. { TXMLSQLDBRestDispatcher }
  33. function TXMLSQLDBRestDispatcher.CreateOutputStreamer(IO: TRestIO): TRestOutputStreamer;
  34. begin
  35. io.Response.ContentStream:=TMemoryStream.Create;
  36. io.Response.FreeContentStream:=True;
  37. Result:=TXMLOutputStreamer.Create(IO.Response.ContentStream,Strings,@IO.DoGetVariable);
  38. end;
  39. { TRestServerDemoApplication }
  40. procedure TRestServerDemoApplication.DoAfterRequest(Sender: TObject; aConn: TSQLConnection; aResource: TSQLDBRestResource);
  41. begin
  42. inc(FRequestCount);
  43. if (FMaxRequests>0) and (FRequestCount>=FMaxRequests) then
  44. begin
  45. DoLog(etInfo,'Maximum requests reached');
  46. Terminate;
  47. end;
  48. end;
  49. procedure TRestServerDemoApplication.DoRun;
  50. var
  51. ErrorMsg: String;
  52. begin
  53. // quick check parameters
  54. ErrorMsg:=CheckOptions('hc:s:m:', ['help','config:','save-config:','max-requests:']);
  55. if ErrorMsg<>'' then begin
  56. ShowException(Exception.Create(ErrorMsg));
  57. Terminate;
  58. Exit;
  59. end;
  60. // parse parameters
  61. if HasOption('h', 'help') then begin
  62. WriteHelp;
  63. Terminate;
  64. Exit;
  65. end;
  66. Port:=3000;
  67. FDisp:=TSQLDBRestDispatcher.Create(Self);
  68. if HasOption('c', 'config') then
  69. FDisp.LoadFromFile(GetOptionValue('c', 'config'),[dioSkipReadSchemas])
  70. else
  71. begin
  72. // create a Default setup
  73. FAuth:=TRestBasicAuthenticator.Create(Self);
  74. FAuth.DefaultUserName:='me';
  75. FAuth.DefaultPassword:='secret';
  76. FAuth.AuthenticateUserSQL.Text:='select uID from users where (uLogin=:UserName) and (uPassword=:Password)';
  77. FDisp.DispatchOptions:=FDisp.DispatchOptions+[rdoConnectionInURL,rdoCustomView,rdoHandleCORS];
  78. FDisp.ExposeDatabase(TPQConnectionDef.TypeName,'localhost','expensetracker','FPC','Shimrod',Nil,[foFilter,foInInsert,foInUpdate,foOrderByDesc]);
  79. With FDisp.Schemas[0].Schema.Resources do
  80. begin
  81. FindResourceByName('users').Fields.FindByFieldName('uID').GeneratorName:='seqUsersID';
  82. FindResourceByName('projects').Fields.FindByFieldName('pID').GeneratorName:='seqProjectsID';
  83. FindResourceByName('expensetypes').Fields.FindByFieldName('etID').GeneratorName:='seqExpenseTypesID';
  84. FindResourceByName('expenses').Fields.FindByFieldName('eID').GeneratorName:='seqExpenseID';
  85. end;
  86. FDisp.Authenticator:=Fauth;
  87. if HasOption('s','save-config') then
  88. FDisp.SaveToFile(GetOptionValue('s','save-config'));
  89. end;
  90. // Mostly for debug purposes, to get e.g. a heap trace
  91. if HasOption('m','max-requests') then
  92. FMaxRequests:=StrToIntDef(GetOptionValue('m','max-requests'),0);
  93. FDisp.AfterGet:=@DoAfterRequest;
  94. FDisp.AfterPost:=@DoAfterRequest;
  95. FDisp.AfterPut:=@DoAfterRequest;
  96. FDisp.AfterDelete:=@DoAfterRequest;
  97. FDisp.Active:=True;
  98. Inherited DoRun;
  99. end;
  100. constructor TRestServerDemoApplication.Create(TheOwner: TComponent);
  101. begin
  102. inherited Create(TheOwner);
  103. StopOnException:=True;
  104. end;
  105. destructor TRestServerDemoApplication.Destroy;
  106. begin
  107. FreeAndNil(FDisp);
  108. FreeAndNil(FAuth);
  109. inherited Destroy;
  110. end;
  111. procedure TRestServerDemoApplication.WriteHelp;
  112. begin
  113. writeln('Usage: ', ExeName, ' [options]');
  114. Writeln('Where options is one or more of:');
  115. Writeln('-h --help this message');
  116. Writeln('-c --config=File Read config from .ini file');
  117. Writeln('-m --max-requests=N Server at most N requests, then quit.');
  118. Writeln('-s --saveconfig=File Write config to .ini file (ignored when -c or --config is used)');
  119. end;
  120. var
  121. Application: TRestServerDemoApplication;
  122. begin
  123. Application:=TRestServerDemoApplication.Create(nil);
  124. Application.Title:='SQLDB REST bridge Application';
  125. Application.Run;
  126. Application.Free;
  127. end.