simpleserver.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. {$mode objfpc}
  2. {$h+}
  3. program simpleserver;
  4. uses sysutils,custhttpapp, fpwebfile;
  5. Type
  6. { THTTPApplication }
  7. THTTPApplication = Class(TCustomHTTPApplication)
  8. private
  9. FQuiet: Boolean;
  10. procedure Usage(Msg: String);
  11. published
  12. procedure DoLog(EventType: TEventType; const Msg: String); override;
  13. Procedure DoRun; override;
  14. property Quiet : Boolean read FQuiet Write FQuiet;
  15. end;
  16. Var
  17. Application : THTTPApplication;
  18. { THTTPApplication }
  19. procedure THTTPApplication.DoLog(EventType: TEventType; const Msg: String);
  20. begin
  21. if Quiet then
  22. exit;
  23. if IsConsole then
  24. Writeln(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',Now),' [',EventType,'] ',Msg)
  25. else
  26. inherited DoLog(EventType, Msg);
  27. end;
  28. procedure THTTPApplication.Usage(Msg : String);
  29. begin
  30. if (Msg<>'') then
  31. Writeln('Error: ',Msg);
  32. Writeln('Usage ',ExtractFileName(ParamStr(0)),' [options] ');
  33. Writeln('Where options is one or more of : ');
  34. Writeln('-d --directory=dir Base directory from which to serve files.');
  35. Writeln(' Default is current working directory: ',GetCurrentDir);
  36. Writeln('-h --help This help text');
  37. Writeln('-i --indexpage=name Directory index page to use (default: index.html)');
  38. Writeln('-n --noindexpage Do not allow index page.');
  39. Writeln('-p --port=NNNN TCP/IP port to listen on (default is 3000)');
  40. Writeln('-m --mimetypes=file path of mime.types, default under unix: /etc/mime.types');
  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. if HasOption('m','mimetypes') then
  58. MimeTypesFile:=GetOptionValue('m','mimetypes');
  59. {$ifdef unix}
  60. if MimeTypesFile='' then
  61. begin
  62. MimeTypesFile:='/etc/mime.types';
  63. if not FileExists(MimeTypesFile) then
  64. begin
  65. {$ifdef darwin}
  66. MimeTypesFile:='/private/etc/apache2/mime.types';
  67. if not FileExists(MimeTypesFile) then
  68. {$endif}
  69. MimeTypesFile:='';
  70. end;
  71. end;
  72. if (MimeTypesFile<>'') and not FileExists(MimeTypesFile) then
  73. Log(etWarning,'mimetypes file not found: '+MimeTypesFile);
  74. {$endif}
  75. TSimpleFileModule.BaseDir:=IncludeTrailingPathDelimiter(D);
  76. TSimpleFileModule.OnLog:=@Log;
  77. If not HasOption('n','noindexpage') then
  78. begin
  79. IndexPage:=GetOptionValue('i','indexpage');
  80. if IndexPage='' then
  81. IndexPage:='index.html';
  82. Log(etInfo,'Using index page %s',[IndexPage]);
  83. TSimpleFileModule.IndexPageName:=IndexPage;
  84. end;
  85. inherited;
  86. end;
  87. begin
  88. TSimpleFileModule.RegisterDefaultRoute;
  89. Application:=THTTPApplication.Create(Nil);
  90. Application.Initialize;
  91. Application.Run;
  92. Application.Free;
  93. end.