demosvr.pas 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2019 by the Free Pascal development team
  4. Sample HTTP server application
  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. {$mode objfpc}
  12. {$h+}
  13. program demosvr;
  14. uses
  15. custhttpapp, sysutils, Classes, jsonparser, fpjson, httproute, httpdefs, fpmimetypes, fpwebfile, fpwebproxy,
  16. fpdebugcapturesvc;
  17. Type
  18. { THTTPApplication }
  19. THTTPApplication = Class(TCustomHTTPApplication)
  20. private
  21. procedure HandleCaptureOutput(aSender: TObject; aCapture: TJSONData);
  22. published
  23. procedure DoLog(EventType: TEventType; const Msg: String); override;
  24. Procedure Initialize; override;
  25. end;
  26. procedure THTTPApplication.DoLog(EventType: TEventType; const Msg: String);
  27. begin
  28. if IsConsole then
  29. Writeln(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',Now),' [',EventType,'] ',Msg)
  30. else
  31. inherited DoLog(EventType, Msg);
  32. end;
  33. procedure THTTPApplication.Initialize;
  34. var
  35. aBaseDir : String;
  36. Svc : TDebugCaptureService;
  37. begin
  38. Port:=8080;
  39. Svc:=TDebugCaptureService.Instance;
  40. Svc.OnLog:=@DoLog;
  41. Svc.LogFileName:='debug.log';
  42. Svc.RegisterHandler('log',@HandleCaptureOutput);
  43. HTTPRouter.RegisterRoute('/debugcapture',rmPost,@Svc.HandleRequest,False);
  44. aBaseDir:=IncludeTrailingPathDelimiter(GetCurrentDir);
  45. TSimpleFileModule.RegisterDefaultRoute;
  46. TSimpleFileModule.BaseDir:=aBaseDir;
  47. TSimpleFileModule.OnLog:=@Log;
  48. TSimpleFileModule.IndexPageName:='index.html';
  49. MimeTypes.LoadKnownTypes;
  50. inherited;
  51. end;
  52. procedure THTTPApplication.HandleCaptureOutput(aSender: TObject; aCapture: TJSONData);
  53. begin
  54. DoLog(etDebug,TDebugCaptureService.JSONDataToString(aCapture));
  55. end;
  56. Var
  57. Application : THTTPApplication;
  58. begin
  59. Application:=THTTPApplication.Create(Nil);
  60. Application.Initialize;
  61. Application.Run;
  62. Application.Free;
  63. end.