* Enable location management API

This commit is contained in:
Michaël Van Canneyt 2021-09-04 13:15:56 +02:00
parent 886baf4cb9
commit 06ab6d7fa7

View File

@ -44,7 +44,7 @@ uses
{$ifdef unix}
baseunix,
{$endif}
sysutils,Classes, inifiles, sslbase, httproute, httpdefs, fpmimetypes, fpwebfile, fpwebproxy, webutil;
sysutils,Classes, jsonparser, strutils, inifiles, sslbase, httproute, httpdefs, fpmimetypes, fpwebfile, fpwebproxy, webutil;
Type
@ -57,6 +57,7 @@ Type
THTTPApplication = Class(TParentApp)
private
FAPISecret : String;
FBaseDir: string;
FIndexPageName: String;
FMimeFile: String;
@ -168,27 +169,30 @@ begin
Writeln('Error: ',Msg);
Writeln('Usage ',ExtractFileName(ParamStr(0)),' [options] ');
Writeln('Where options is one or more of : ');
Writeln('-c --config=file Ini configuration file (default: simpleserver.ini)');
Writeln('-A --api=path,secret Activate location API on path, using secret as accepted bearer token.');
Writeln('-a --max-age=age Set max-age expiry header on returned file requests.');
Writeln('-c --config=file Ini configuration file (default: simpleserver.ini)');
{$ifdef unix}
Writeln('-b --background fork to background');
Writeln('-b --background fork to background');
{$endif}
Writeln('-d --directory=dir Base directory from which to serve files.');
Writeln(' Default is current working directory: ',GetCurrentDir);
Writeln('-h --help This help text');
Writeln('-i --indexpage=name Directory index page to use (default: index.html)');
Writeln('-n --noindexpage Do not allow index page.');
Writeln('-p --port=NNNN TCP/IP port to listen on (default is 3000)');
Writeln('-m --mimetypes=file path of mime.types. Loaded in addition to OS known types');
Writeln('-q --quiet Do not write diagnostic messages');
Writeln('-Q --quit=PWD register /quit url. Send request with password variable equal to PWD to stop');
Writeln('-e --echo register /quit url. Send request with password variable equal to PWD to stop');
Writeln('-s --ssl Use SSL');
Writeln('-H --hostname=NAME set hostname for self-signed SSL certificate');
Writeln('-x --proxy=proxydef Add proxy definition. Definition is of form:');
Writeln(' name:BaseURL');
Writeln('-d --directory=dir Base directory from which to serve files.');
Writeln(' Default is current working directory: ',GetCurrentDir);
Writeln('-e --echo Activate /echo URL.');
Writeln('-h --help This help text');
Writeln('-H --hostname=NAME Set hostname for self-signed SSL certificate');
Writeln('-i --indexpage=name Directory index page to use (default: index.html)');
Writeln('-m --mimetypes=file Path of mime.types. Loaded in addition to OS known types');
Writeln('-n --noindexpage Do not allow index page.');
Writeln('-p --port=NNNN TCP/IP port to listen on (default is 3000)');
Writeln('-q --quiet Do not write diagnostic messages');
Writeln('-Q --quit=PWD Register /quit URL. Send request with password variable equal to PWD to stop');
Writeln('-s --ssl Use SSL');
Writeln('-x --proxy=proxydef Add proxy definition. Definition is of form:');
Writeln(' name:BaseURL');
Writeln('');
Writeln('Config file is ini file, section [Server]. Key names are long option names');
Writeln('Proxies are defined in section [Proxy], Key is name, value is URL');
Writeln('Locations are defined in section [Locations], Key is location name, value is path');
Halt(Ord(Msg<>''));
end;
@ -230,6 +234,8 @@ procedure THTTPApplication.ReadConfigFile(Const ConfigFile : string);
Const
SConfig = 'Server';
SProxy = 'Proxy';
SLocations = 'Locations';
KeyPort = 'Port';
KeyDir = 'Directory';
KeyIndexPage = 'IndexPage';
@ -242,6 +248,7 @@ Const
KeyNoIndexPage = 'noindexpage';
KeyBackground = 'background';
KeyMaxAge = 'MaxAge';
KeyAPI = 'API';
Var
L : TStringList;
@ -265,6 +272,7 @@ begin
FPassword:=ReadString(SConfig,KeyQuit,FPassword);
FEcho:=ReadBool(SConfig,KeyEcho,FEcho);
FMaxAge:=ReadInteger(SConfig,KeyMaxAge,FMaxAge);
FAPISecret:=ReadString(SConfig,keyAPI,'');
L:=TstringList.Create;
ReadSectionValues(SProxy,L,[]);
For I:=0 to L.Count-1 do
@ -273,6 +281,14 @@ begin
if (P<>'') and (U<>'') then
ProxyManager.RegisterLocation(P,U).AppendPathInfo:=true;
end;
L.Clear;
ReadSectionValues(SLocations,L,[]);
For I:=0 to L.Count-1 do
begin
L.GetNameValue(I,P,U);
if (P<>'') and (U<>'') then
RegisterFileLocation(P,U);
end;
finally
L.Free;
Free;
@ -287,6 +303,7 @@ Var
begin
for S in GetOptionValues('x','proxy') do
AddProxy(S);
FAPISecret:=GetOptionValue('A','api');
FEcho:=HasOption('e','echo');
Quiet:=HasOption('q','quiet');
FPassword:=GetOptionValue('Q','quit');
@ -317,6 +334,8 @@ begin
Log(etInfo,'Proxy location /proxy/%s redirects to %s',[Path,URL]);
if not NoIndexPage then
Log(etInfo,'Using index page %s',[IndexPageName]);
Log(etInfo,'Location REST API '+IfThen(FAPISecret<>'','','NOT ')+'activated.');
end;
procedure THTTPApplication.DoRun;
@ -326,7 +345,7 @@ Var
begin
FMaxAge:=31557600;
S:=Checkoptions('hqd:ni:p:sH:m:x:c:beQ:a:',['help','quiet','noindexpage','directory:','port:','indexpage:','ssl','hostname:','mimetypes:','proxy:','config:','background','echo','quit:','max-age:']);
S:=Checkoptions('hqd:ni:p:sH:m:x:c:beQ:a:A:',['help','quiet','noindexpage','directory:','port:','indexpage:','ssl','hostname:','mimetypes:','proxy:','config:','background','echo','quit:','max-age:','api:']);
if (S<>'') or HasOption('h','help') then
usage(S);
if HasOption('c','config') then
@ -357,6 +376,8 @@ begin
BaseDir:=GetCurrentDir;
if (BaseDir<>'') then
BaseDir:=IncludeTrailingPathDelimiter(BaseDir);
if FAPISecret<>'' then
TFPWebFileLocationAPIModule.RegisterFileLocationAPI(ExtractWord(1,FAPISecret,[',']),ExtractWord(2,FAPISecret,[',']));
TSimpleFileModule.RegisterDefaultRoute;
TSimpleFileModule.BaseDir:=BaseDir;
TSimpleFileModule.OnLog:=@Log;