restserver.pas 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. program restserver;
  2. uses sysutils,custhttpapp, fpwebfile, httproute, restdata;
  3. {$IF FPC_FULLVERSION<30101}
  4. {$ERROR You need at least fpc 3.1.1}
  5. {$ENDIF}
  6. Type
  7. { THTTPApplication }
  8. THTTPApplication = Class(TCustomHTTPApplication)
  9. private
  10. FQuiet: Boolean;
  11. procedure Usage(Msg: String);
  12. published
  13. procedure DoLog(EventType: TEventType; const Msg: String); override;
  14. Procedure DoRun; override;
  15. property Quiet : Boolean read FQuiet Write FQuiet;
  16. end;
  17. Var
  18. Application : THTTPApplication;
  19. { THTTPApplication }
  20. procedure THTTPApplication.DoLog(EventType: TEventType; const Msg: String);
  21. begin
  22. if Quiet then
  23. exit;
  24. if IsConsole then
  25. Writeln(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',Now),' [',EventType,'] ',Msg)
  26. else
  27. inherited DoLog(EventType, Msg);
  28. end;
  29. procedure THTTPApplication.Usage(Msg : String);
  30. begin
  31. if (Msg<>'') then
  32. Writeln('Error: ',Msg);
  33. Writeln('Usage ',ExtractFileName(ParamStr(0)),' [options] ');
  34. Writeln('Where options is one or more of : ');
  35. Writeln('-d --directory=dir Base directory from which to serve files.');
  36. Writeln(' Default is current working directory: ',GetCurrentDir);
  37. Writeln('-h --help This help text');
  38. Writeln('-i --indexpage=name Directory index page to use (default: index.html)');
  39. Writeln('-n --noindexpage Do not allow index page.');
  40. Writeln('-p --port=NNNN TCP/IP port to listen on (default is 3000)');
  41. Writeln('-q --quiet Do not write diagnostic messages');
  42. Halt(Ord(Msg<>''));
  43. end;
  44. procedure THTTPApplication.DoRun;
  45. Var
  46. S,IndexPage,D : String;
  47. begin
  48. S:=Checkoptions('hqd:ni:p:',['help','quiet','noindexpage','directory:','port:','indexpage:']);
  49. if (S<>'') or HasOption('h','help') then
  50. usage(S);
  51. Quiet:=HasOption('q','quiet');
  52. Port:=StrToIntDef(GetOptionValue('p','port'),3000);
  53. D:=GetOptionValue('d','directory');
  54. if D='' then
  55. D:=GetCurrentDir;
  56. Log(etInfo,'Listening on port %d, serving files from directory: %s',[Port,D]);
  57. {$IFDEF darwin}
  58. MimeTypesFile:='/private/etc/apache2/mime.types';
  59. {$else}
  60. {$ifdef unix}
  61. MimeTypesFile:='/etc/mime.types';
  62. {$endif}
  63. {$endif}
  64. TSimpleFileModule.BaseDir:=IncludeTrailingPathDelimiter(D);
  65. TSimpleFileModule.OnLog:=@Log;
  66. If not HasOption('n','noindexpage') then
  67. begin
  68. IndexPage:=GetOptionValue('i','indexpage');
  69. if IndexPage='' then
  70. IndexPage:='index.html';
  71. Log(etInfo,'Using index page %s',[IndexPage]);
  72. TSimpleFileModule.IndexPageName:=IndexPage;
  73. end;
  74. inherited;
  75. end;
  76. begin
  77. HTTPRouter.RegisterRoute('/countries/*',@HandleRest);
  78. TSimpleFileModule.RegisterDefaultRoute;
  79. Application:=THTTPApplication.Create(Nil);
  80. Application.Initialize;
  81. Application.Run;
  82. Application.Free;
  83. end.