Browse Source

* Enable location management API

Michaël Van Canneyt 3 years ago
parent
commit
06ab6d7fa7
1 changed files with 39 additions and 18 deletions
  1. 39 18
      packages/fcl-web/examples/simpleserver/simpleserver.pas

+ 39 - 18
packages/fcl-web/examples/simpleserver/simpleserver.pas

@@ -44,7 +44,7 @@ uses
   {$ifdef unix}
   {$ifdef unix}
   baseunix,
   baseunix,
   {$endif}
   {$endif}
-  sysutils,Classes, inifiles, sslbase, httproute, httpdefs, fpmimetypes, fpwebfile, fpwebproxy, webutil;
+  sysutils,Classes, jsonparser, strutils, inifiles, sslbase, httproute, httpdefs, fpmimetypes, fpwebfile, fpwebproxy, webutil;
 
 
 Type
 Type
 
 
@@ -57,6 +57,7 @@ Type
 
 
   THTTPApplication = Class(TParentApp)
   THTTPApplication = Class(TParentApp)
   private
   private
+    FAPISecret : String;
     FBaseDir: string;
     FBaseDir: string;
     FIndexPageName: String;
     FIndexPageName: String;
     FMimeFile: String;
     FMimeFile: String;
@@ -168,27 +169,30 @@ begin
     Writeln('Error: ',Msg);
     Writeln('Error: ',Msg);
   Writeln('Usage ',ExtractFileName(ParamStr(0)),' [options] ');
   Writeln('Usage ',ExtractFileName(ParamStr(0)),' [options] ');
   Writeln('Where options is one or more of : ');
   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}
 {$ifdef unix}
-  Writeln('-b --background     fork to background');
+  Writeln('-b --background       fork to background');
 {$endif}
 {$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('');
   Writeln('Config file is ini file, section [Server]. Key names are long option names');
   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('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<>''));
   Halt(Ord(Msg<>''));
 end;
 end;
 
 
@@ -230,6 +234,8 @@ procedure THTTPApplication.ReadConfigFile(Const ConfigFile : string);
 Const
 Const
   SConfig  = 'Server';
   SConfig  = 'Server';
   SProxy = 'Proxy';
   SProxy = 'Proxy';
+  SLocations = 'Locations';
+
   KeyPort  = 'Port';
   KeyPort  = 'Port';
   KeyDir   = 'Directory';
   KeyDir   = 'Directory';
   KeyIndexPage = 'IndexPage';
   KeyIndexPage = 'IndexPage';
@@ -242,6 +248,7 @@ Const
   KeyNoIndexPage = 'noindexpage';
   KeyNoIndexPage = 'noindexpage';
   KeyBackground = 'background';
   KeyBackground = 'background';
   KeyMaxAge = 'MaxAge';
   KeyMaxAge = 'MaxAge';
+  KeyAPI = 'API';
 
 
 Var
 Var
   L : TStringList;
   L : TStringList;
@@ -265,6 +272,7 @@ begin
       FPassword:=ReadString(SConfig,KeyQuit,FPassword);
       FPassword:=ReadString(SConfig,KeyQuit,FPassword);
       FEcho:=ReadBool(SConfig,KeyEcho,FEcho);
       FEcho:=ReadBool(SConfig,KeyEcho,FEcho);
       FMaxAge:=ReadInteger(SConfig,KeyMaxAge,FMaxAge);
       FMaxAge:=ReadInteger(SConfig,KeyMaxAge,FMaxAge);
+      FAPISecret:=ReadString(SConfig,keyAPI,'');
       L:=TstringList.Create;
       L:=TstringList.Create;
       ReadSectionValues(SProxy,L,[]);
       ReadSectionValues(SProxy,L,[]);
       For I:=0 to L.Count-1 do
       For I:=0 to L.Count-1 do
@@ -273,6 +281,14 @@ begin
         if (P<>'') and (U<>'') then
         if (P<>'') and (U<>'') then
           ProxyManager.RegisterLocation(P,U).AppendPathInfo:=true;
           ProxyManager.RegisterLocation(P,U).AppendPathInfo:=true;
         end;
         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
     finally
       L.Free;
       L.Free;
       Free;
       Free;
@@ -287,6 +303,7 @@ Var
 begin
 begin
   for S in GetOptionValues('x','proxy') do
   for S in GetOptionValues('x','proxy') do
     AddProxy(S);
     AddProxy(S);
+  FAPISecret:=GetOptionValue('A','api');
   FEcho:=HasOption('e','echo');
   FEcho:=HasOption('e','echo');
   Quiet:=HasOption('q','quiet');
   Quiet:=HasOption('q','quiet');
   FPassword:=GetOptionValue('Q','quit');
   FPassword:=GetOptionValue('Q','quit');
@@ -317,6 +334,8 @@ begin
       Log(etInfo,'Proxy location /proxy/%s redirects to %s',[Path,URL]);
       Log(etInfo,'Proxy location /proxy/%s redirects to %s',[Path,URL]);
   if not NoIndexPage then
   if not NoIndexPage then
     Log(etInfo,'Using index page %s',[IndexPageName]);
     Log(etInfo,'Using index page %s',[IndexPageName]);
+  Log(etInfo,'Location REST API '+IfThen(FAPISecret<>'','','NOT ')+'activated.');
+
 end;
 end;
 
 
 procedure THTTPApplication.DoRun;
 procedure THTTPApplication.DoRun;
@@ -326,7 +345,7 @@ Var
 
 
 begin
 begin
   FMaxAge:=31557600;
   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
   if (S<>'') or HasOption('h','help') then
     usage(S);
     usage(S);
   if HasOption('c','config') then
   if HasOption('c','config') then
@@ -357,6 +376,8 @@ begin
     BaseDir:=GetCurrentDir;
     BaseDir:=GetCurrentDir;
   if (BaseDir<>'') then
   if (BaseDir<>'') then
     BaseDir:=IncludeTrailingPathDelimiter(BaseDir);
     BaseDir:=IncludeTrailingPathDelimiter(BaseDir);
+  if FAPISecret<>'' then
+    TFPWebFileLocationAPIModule.RegisterFileLocationAPI(ExtractWord(1,FAPISecret,[',']),ExtractWord(2,FAPISecret,[',']));
   TSimpleFileModule.RegisterDefaultRoute;
   TSimpleFileModule.RegisterDefaultRoute;
   TSimpleFileModule.BaseDir:=BaseDir;
   TSimpleFileModule.BaseDir:=BaseDir;
   TSimpleFileModule.OnLog:=@Log;
   TSimpleFileModule.OnLog:=@Log;