webmcp

diff framework/bin/mcp.lua @ 352:2b5bdf9028fb

Code cleanup and performance improvements regarding 404 check; Deprecated encode.action_file_path{...} and encode.view_file_path{...}
author jbe
date Thu Mar 26 16:40:04 2015 +0100 (2015-03-26)
parents 3687294cb955
children e7e51bcf47d1
line diff
     1.1 --- a/framework/bin/mcp.lua	Thu Mar 26 16:38:30 2015 +0100
     1.2 +++ b/framework/bin/mcp.lua	Thu Mar 26 16:40:04 2015 +0100
     1.3 @@ -9,68 +9,6 @@
     1.4  --//--
     1.5  
     1.6  --[[--
     1.7 -_G
     1.8 -
     1.9 -A reference to the global namespace. To avoid accidental programming errors, global variables cannot be set directly, but they must be set through the _G reference, e.g. use _G.foo = true to set the variable "foo" to a value of true.
    1.10 -
    1.11 -Note that the global namespace may or may not be shared between requests (Moonbridge creates multiple forks of the Lua machine). To set variables that are to be cleared after the request has been finished, an application may use the "app" table, e.g. app.foo = true to set the variable app.foo to a value of true, which will be cleared automatically when the request has ended.
    1.12 -
    1.13 ---]]--
    1.14 -local _G = _G
    1.15 -local allowed_globals = {}
    1.16 -local protected_environment = setmetatable(
    1.17 -  {},  -- proxy environment used all chunks loaded through loadcached(...)
    1.18 -  {
    1.19 -    __index = _G,
    1.20 -    __newindex = function(self, key, value)
    1.21 -      if allowed_globals[key] then
    1.22 -        _G[key] = value
    1.23 -      else
    1.24 -        if type(key) == "string" and string.match(key, "^[A-Za-z_][A-Za-z_0-9]*$") then
    1.25 -          error('Attempt to set global variable "' .. key .. '" (Hint: missing local statement? Use _G.' .. key .. '=<value> to really set global variable.)', 2)
    1.26 -        else
    1.27 -          error('Attempt to set global variable', 2)
    1.28 -        end
    1.29 -      end
    1.30 -    end
    1.31 -  }
    1.32 -)
    1.33 ---//--
    1.34 -
    1.35 ---[[--
    1.36 -lua_func =   -- compiled Lua function
    1.37 -loadcached(
    1.38 -  filename   -- path to a Lua source or byte-code file
    1.39 -)
    1.40 -
    1.41 -Loads, compiles and caches a Lua chunk. If the file does not exist, nil and an error string are returned. Otherwise the file is loaded, compiled, and cached. The cached value (i.e. the compiled function) is returned. An error is raised if the compilation was not successful.
    1.42 -
    1.43 ---]]--
    1.44 -do
    1.45 -  local cache = {}
    1.46 -  function loadcached(filename)
    1.47 -    local cached_func = cache[filename]
    1.48 -    if cached_func then
    1.49 -      return cached_func
    1.50 -    end
    1.51 -    local file, read_error = io.open(filename, "r")
    1.52 -    if file then
    1.53 -      local filedata = assert(file:read("*a"))
    1.54 -      assert(file:close())
    1.55 -      local func, compile_error = load(filedata, "=" .. filename, nil, protected_environment)
    1.56 -      if func then
    1.57 -        cache[filename] = func
    1.58 -        return func
    1.59 -      end
    1.60 -      error(compile_error, 0)
    1.61 -    else
    1.62 -      return nil, read_error
    1.63 -    end
    1.64 -  end
    1.65 -end
    1.66 ---//--
    1.67 -
    1.68 ---[[--
    1.69  WEBMCP_MODE
    1.70  
    1.71  A constant set to "listen" in case of a network request, or set to "interactive" in case of interactive mode.
    1.72 @@ -145,19 +83,6 @@
    1.73    WEBMCP_APP_NAME       = arg3
    1.74  end
    1.75  
    1.76 --- check if framework path is correct
    1.77 -do
    1.78 -  local file, errmsg = io.open(WEBMCP_FRAMEWORK_PATH .. "webmcp_version", "r")
    1.79 -  if not file then
    1.80 -    error('Could not find "webmcp_version" file: ' .. errmsg, 0)
    1.81 -  end
    1.82 -  local version = assert(file:read())
    1.83 -  assert(file:close())
    1.84 -  if version ~= WEBMCP_VERSION then
    1.85 -    error('Version mismatch in file "' .. WEBMCP_FRAMEWORK_PATH .. 'webmcp_version"')
    1.86 -  end
    1.87 -end
    1.88 -
    1.89  -- setup search paths for libraries
    1.90  do
    1.91    if string.match(package.path, "^[^;]") then
    1.92 @@ -183,6 +108,88 @@
    1.93    package.cpath = table.concat(paths, ";")
    1.94  end
    1.95  
    1.96 +-- load "extos" library (needed by function "loadcached")
    1.97 +_G.extos = require "extos"
    1.98 +
    1.99 +--[[--
   1.100 +_G
   1.101 +
   1.102 +A reference to the global namespace. To avoid accidental programming errors, global variables cannot be set directly, but they must be set through the _G reference, e.g. use _G.foo = true to set the variable "foo" to a value of true.
   1.103 +
   1.104 +Note that the global namespace may or may not be shared between requests (Moonbridge creates multiple forks of the Lua machine). To set variables that are to be cleared after the request has been finished, an application may use the "app" table, e.g. app.foo = true to set the variable app.foo to a value of true, which will be cleared automatically when the request has ended.
   1.105 +
   1.106 +--]]--
   1.107 +local _G = _G
   1.108 +local allowed_globals = {}
   1.109 +local protected_environment = setmetatable(
   1.110 +  {},  -- proxy environment used all chunks loaded through loadcached(...)
   1.111 +  {
   1.112 +    __index = _G,
   1.113 +    __newindex = function(self, key, value)
   1.114 +      if allowed_globals[key] then
   1.115 +        _G[key] = value
   1.116 +      else
   1.117 +        if type(key) == "string" and string.match(key, "^[A-Za-z_][A-Za-z_0-9]*$") then
   1.118 +          error('Attempt to set global variable "' .. key .. '" (Hint: missing local statement? Use _G.' .. key .. '=<value> to really set global variable.)', 2)
   1.119 +        else
   1.120 +          error('Attempt to set global variable', 2)
   1.121 +        end
   1.122 +      end
   1.123 +    end
   1.124 +  }
   1.125 +)
   1.126 +--//--
   1.127 +
   1.128 +--[[--
   1.129 +lua_func =   -- compiled Lua function, nil if the file does not exist
   1.130 +errmsg       -- error message (only for non-existing file, other errors are thrown)
   1.131 +loadcached(
   1.132 +  filename   -- path to a Lua source or byte-code file
   1.133 +)
   1.134 +
   1.135 +Loads, compiles and caches a Lua chunk. The cached value (i.e. the compiled function) is returned. If the file does not exist, nil and an error string are returned. Any other errors are thrown using error(...). Unsuccessful attempts are not cached (to prohibit cache pollution).
   1.136 +
   1.137 +--]]--
   1.138 +do
   1.139 +  local cache = {}
   1.140 +  function loadcached(filename)
   1.141 +    local cached_func = cache[filename]
   1.142 +    if cached_func then
   1.143 +      return cached_func
   1.144 +    end
   1.145 +    local stat, errmsg = extos.stat(filename)
   1.146 +    if stat == nil then
   1.147 +      error(errmsg)
   1.148 +    elseif stat == false then
   1.149 +      return nil, 'File "' .. filename .. '" does not exist'
   1.150 +    elseif stat.isdir then
   1.151 +      error('File "' .. filename .. '" is a directory')
   1.152 +    elseif not stat.isreg then
   1.153 +      error('File "' .. filename .. '" is not a regular file')
   1.154 +    end
   1.155 +    local func, compile_error = loadfile(filename, nil, protected_environment)
   1.156 +    if func then
   1.157 +      cache[filename] = func
   1.158 +      return func
   1.159 +    end
   1.160 +    error(compile_error, 0)
   1.161 +  end
   1.162 +end
   1.163 +--//--
   1.164 +
   1.165 +-- check if framework path is correct
   1.166 +do
   1.167 +  local file, errmsg = io.open(WEBMCP_FRAMEWORK_PATH .. "webmcp_version", "r")
   1.168 +  if not file then
   1.169 +    error('Could not find "webmcp_version" file: ' .. errmsg, 0)
   1.170 +  end
   1.171 +  local version = assert(file:read())
   1.172 +  assert(file:close())
   1.173 +  if version ~= WEBMCP_VERSION then
   1.174 +    error('Version mismatch in file "' .. WEBMCP_FRAMEWORK_PATH .. 'webmcp_version"')
   1.175 +  end
   1.176 +end
   1.177 +
   1.178  -- autoloader system for WebMCP environment "$WEBMCP_FRAMEWORK_PATH/env/",
   1.179  -- application environment extensions "$WEBMCP_BASE_PATH/env/"
   1.180  -- and models "$WEBMCP_BASE_PATH/model/"

Impressum / About Us