simpleserver.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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('-q --quiet Do not write diagnostic messages');
  41. Halt(Ord(Msg<>''));
  42. end;
  43. procedure THTTPApplication.DoRun;
  44. Var
  45. S,IndexPage,D : String;
  46. begin
  47. S:=Checkoptions('hqd:ni:p:',['help','quiet','noindexpage','directory:','port:','indexpage:']);
  48. if (S<>'') or HasOption('h','help') then
  49. usage(S);
  50. Quiet:=HasOption('q','quiet');
  51. Port:=StrToIntDef(GetOptionValue('p','port'),3000);
  52. D:=GetOptionValue('d','directory');
  53. if D='' then
  54. D:=GetCurrentDir;
  55. Log(etInfo,'Listening on port %d, serving files from directory: %s',[Port,D]);
  56. {$ifdef unix}
  57. MimeTypesFile:='/etc/mime.types';
  58. if not FileExists(MimeTypesFile) then
  59. MimeTypesFile:='';
  60. {$endif}
  61. TSimpleFileModule.BaseDir:=IncludeTrailingPathDelimiter(D);
  62. TSimpleFileModule.OnLog:=@Log;
  63. If not HasOption('n','noindexpage') then
  64. begin
  65. IndexPage:=GetOptionValue('i','indexpage');
  66. if IndexPage='' then
  67. IndexPage:='index.html';
  68. Log(etInfo,'Using index page %s',[IndexPage]);
  69. TSimpleFileModule.IndexPageName:=IndexPage;
  70. end;
  71. inherited;
  72. end;
  73. begin
  74. TSimpleFileModule.RegisterDefaultRoute;
  75. Application:=THTTPApplication.Create(Nil);
  76. Application.Initialize;
  77. Application.Run;
  78. Application.Free;
  79. end.