Moai Cloud Lua Environment

From Moai Wiki
Revision as of 19:11, 6 January 2012 by Ezra (Talk | contribs)
Jump to: navigation, search

Contents

Basic Lua Environment

The basic Lua runtime environment available to Moai Cloud Services is similar to the standard Lua runtime environment, with some functionality removed for security and stability purposes. The basic Lua environment is defined as follows. Information on how to use each of these functions can be found in the Lua 5.1 Reference Manual.

assert=assert,
error=error,
ipairs=ipairs,
loadstring=loadstring,
next=next,
ipairs=ipairs,
pairs=pairs,
pcall=pcall,
select=select,
tonumber=tonumber,
tostring=tostring,
type=type,
unpack=unpack,
xpcall=xpcall,
coroutine={
      create=coroutine.create,
      resume=coroutine.resume,
      running=coroutine.running,
      status=coroutine.status,
      wrap=coroutine.wrap,
      yield=coroutine.yield,
},
table={
      concat=table.concat,
      insert=table.insert,
      maxn=table.maxn,
      getn=table.getn,
      setn=table.setn,
      remove=table.remove,
      sort=table.sort,
},
string={
      byte=string.byte,
      char=string.char,
      find=string.find,
      format=string.format,
      gmatch=string.gmatch,
      gsub=string.gsub,
      len=string.len,
      lower=string.lower,
      match=string.match,
      rep=string.rep,
      reverse=string.reverse,
      sub=string.sub,
      upper=string.upper,
},
math={
      abs=math.abs,
      acos=math.acos,
      asin=math.asin,
      atan=math.atan,
      atan2=math.atan2,
      ceil=math.ceil,
      cos=math.cos,
      cosh=math.cosh,
      deg=math.deg,
      exp=math.exp,
      floor=math.floor,
      fmod=math.fmod,
      frexp=math.frexp,
      huge=math.huge,
      ldexp=math.ldexp,
      log=math.log,
      log10=math.log10,
      max=math.max,
      min=math.min,
      modf=math.modf,
      pi=math.pi,
      pow=math.pow,
      rad=math.rad,
      random=math.random,
      randomseed=math.randomseed,
      sin=math.sin,
      sinh=math.sinh,
      sqrt=math.sqrt,
      tan=math.tan,
      tanh=math.tanh,
},
os={
      clock=os.clock,
      date=os.date,
      difftime=os.difftime,
      time=os.time,
},

Additional Libraries

curl

The Moai Cloud Lua environment contains full support for the luacurl library and is defined as follows:

curl={
      new=curl.new,
      setopt=curl.setopt,
      perform=curl.perform,
      close=curl.close
},
       

Sample Code

The following demonstrates performing an HTTP POST of a JSON document to a given URL and retrieving the results as a string.

function curl_url(url)
  local writer = function(temp,s)
    temp[#temp+1] = s
    return s:len()
  end
 
  local post_data = {['arg1']='value1'}
 
  local buffer = {}
  local c = curl:new()
  c:setopt(curl.OPT_URL, url)
  c:setopt(curl.OPT_WRITEFUNCTION,writer)
  c:setopt(curl.OPT_WRITEDATA, buffer)
  c:setopt(curl.OPT_FOLLOWLOCATION, true)
 
  c:setopt(curl.OPT_POSTFIELDS, json.encode(post_data))
  c:setopt(curl.OPT_POSTFIELDSIZE, #json.encode(post_data))
 
  local result, msg = c:perform()
 
  return table.concat(buffer)
 end

JSON

The Moai Cloud Lua environment contains full support for the luajson library and is defined as follows:

json={
      encode=json.encode,
      isEncodable=json.isEncodable,
      decode=json.decode,
      util=json.util,
},

MD5

The Moai Cloud Lua environment contains partial support for the Lua MD5 library and is defined as follows:

md5={
      crypt=md5.cript,
      decrypt=md5.decrypt,
      exor=md5.exor,
      sum=md5.sum,
      sumhexa=md5.sumhexa,
},

mongodb

All persistence in Moai Cloud is handled by MongoDB using a subset of the luamongo driver and is defined as follows. See also: Using MongoDB Databases with Moai Cloud and the luamongo project page.

mongodb={
      count=mongodb.count,
      insert=mongodb.insert,
      insert_batch=mongodb.insert_batch,
      query=mongodb.query,
      remove=mongodb.remove,
      update=mongodb.update,
      drop_collection=drop_collection,
      list_collections=list_collections,
},

Client information

Moai Cloud provides a mechanism to allow a service callers to identify himself using a unique public identifier. When a key is provided to a service call some additional information is available to the service.

An additional mongodb connection that allows access to the caller's database is available, as well as a table of all the user-configurable settings for you service and the key, which can be used as a unique identifier, the caller is using to access your service.

 client={
   db,
   db.readonly,
   key,
   settings = {}
 }

Moai Cloud Specific

In addition to the standard functions defined below, the Moai Cloud Lua environment also provides for some Moai Cloud specific variables and functions used for loading additional code, managing file and image resources and mongoDB database access. All additions to the standard Lua environment exist under an environment variable "moai" and are defined as follows.

moai={
      app.settings= {}
      code={
        loadsource=loadsource,
        find_file=find_file,
        store_data=store_data,
        remove_file=remove_file,
        list=list
      },
      content={
        find_file=find_file,
        store_data=store_data,
        remove_file=remove_file,
        list=list
      },
      log=moai.log,
      table_print=moai.table_print,
      userid=moai.userid,
},
For more information about moai.code and moai.content see Moai File Store.

Function Reference

moai.appid - the UUID of the current service
moai.code.find_file(filename) - Load a file from the GridFS filesystem and return its contents. 
This is useful for getting the contents of image files stored in the GridFS and returning them to the calling client. 
    filename - (string) the name of a Lua source file \. 
           Note: Filenames can include path info but may not include a leading slash '/'. 
           All files are loaded using paths relative to the GridFS root for the current serivce.
moai.code.loadsource(filename) - Load a Lua source file from the gridfs filesystem and add it to the current Lua environment. 
 Note: moai.getfile can only return files that were originally uploaded with your source ZIP file at service-creation time.
   filename - (string) the name of a Lua source file included in the ZIP file that was uploaded when you created your service. 
           Note: Filenames can include path info but may not include a leading slash '/'.
           All files are loaded using paths relative to the GridFS root for the current service.
moai.log(msg, level) - Creates a log file entry for your application. The Moai Cloud internal logger provides for 
a rolling log of 1000 messages of up to 1000 characters each. You can view the log output from the Moai Cloud dashboard. 
Note, all log messages across all of your services will be written to a common log file.
    msg - (string) - the message to log
    level - (string) - a string indicating the log level (e.g. DEBUG, INFO, WARN, ERROR, FATAL, etc.)
moai.table_print(table) - Returns an HTML formatted string representation of the specified Lua table.
   table - (Lua table) - the table to stringify.
moai.userid - your Moai Cloud ID
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox