.preload



   1   --[[
   2             The script .preload is executed when the application is loaded.
   3   --]] 
   4   
   5   local fmt=string.format
   6   
   7   -- The following code creates the two SOAP services from our documentation: 
   8   -- http://barracudaserver.com/ba/doc/en/lua/soap.html
   9   
  10   require"basoap"
  11   
  12   -- The applications I/O: We set the io as local variable since the
  13   -- variable is used as an upvalue by function serviceloader
  14   local io=io 
  15   
  16   --Lua SOAP service loader
  17   local function serviceloader(fname)
  18      -- io is the applications IO
  19      return function(env)
  20         local fnsource,err = io:loadfile(fname,env)
  21         if not fnsource then error("failed to load soap services : "..err) end
  22         return fnsource
  23      end
  24   end
  25   
  26   local soapdir=ba.create.dir"soap"
  27   local datedir=ba.create.soapdir("date",serviceloader"soap/.services/date.lua")
  28   local mathdir=ba.create.soapdir("math",serviceloader"soap/.services/math.lua")
  29   
  30   -- Install SOAP services in the Barracuda virtual file system
  31   dir:insert(soapdir, true)
  32   soapdir:insert(datedir, true) -- ./soap/date/
  33   soapdir:insert(mathdir, true) -- ./soap/math/
  34   
  35   
  36   -- BarracudaDrive calls this function when user stops the application
  37   function onunload()
  38      soapdir:unlink() -- Remove immediately. Do not wait for GC to remove dir.
  39      -- Speed up GC
  40      soapdir=nil
  41      datedir=nil
  42      mathdir=nil
  43   end
  44   
  45   
  46   -- File iterator factory.
  47   -- Returns a recursive iterator that find files with Lua code.
  48   function recDirIter(dir)
  49      local retVal=nil
  50      local isLuaFile = {preload=true,lsp=true,lua=true} -- Suffix: files with Lua
  51      -- Recursive directory iterator
  52      local function doDir(dir)
  53         for file,isdir in io:files(dir, true) do
  54            if isdir then
  55               -- Recurse into directory
  56               doDir(fmt("%s%s/",dir,file))
  57            else
  58               local suffix = file:match"%.([^%.]+)$"
  59               if suffix and isLuaFile[suffix] then
  60                  retVal=dir..file
  61                  coroutine.yield() -- Yield to (A)
  62               else
  63                  retVal=nil
  64               end
  65            end
  66         end
  67         retVal=nil
  68      end
  69      local co = coroutine.create(function() doDir(dir) end)
  70      return function() -- Return iterator
  71         coroutine.resume(co)
  72         -- (A)
  73         return retVal
  74      end
  75   end