Some of this documentation is old, so to see all the latest updated information go to our wiki at http://getmoai.com/wiki/Main_Page
What is a Moai Cloud service?
A Moai Cloud service is a cloud-hosted Lua script and a dedicated mongodb database used to provide web services for your game.
What parts compose a Moai Cloud service?
A Moai Cloud service is composed of a minimum of two parts: an install/initialization script contained in a file named "manifest.lua" and at least one file containing the main entry point (handle_moai_request) for handling service requests.
Where can I get sample code?
How do I access my mongodb database from my Moai Cloud service?
Access to your mongodb is provided via the Lua environment variable moaiconfig.mongodb. The mongodb variable contains an initialized MongoConnection object, connected to your dedicated mongo database. Details on using the Lua-MongoDB database drivers can be found on the luamongo project page here: http://code.google.com/p/luamongo/
(sample code coming soon)
How do I create log output for my service?
Access to logging facilities is provided via the Lua moaiconfig.log method contained in your service's environment. When the log() method is called, output will be written to a "logs" collection in your mongo database. Log output for a given service can be viewed from dashboard.moaicloud.com. Log output for your services is collectively limited to 1000 entries, no more than 1000 bytes in length each. Logs are rotated incrementally as log entries exceed 1000 items. The log method signature is as follows:
– msg (string) – a string containing a log message, no longer than 1000 bytes in length
– log_level (string) – a string containing a log level identifier (e.g. DEBUG, INFO, WARN, etc.)
Will my service's script be sandboxed?
Yes. The Lua environment available is described here.
Can I store images and other binary resources on the server?
Yes. All file resources are managed via the mongodb GridFS system. Any files uploaded in your service's ZIP file will automatically be stored in the mongodb GridFS. Access to these files can be accomplished via the luamongo driver.
(sample code coming soon)
Can I use more than one Lua script file in my service?
Yes. You can include as many Lua script files as you like in your service's ZIP file. Additional scripts can be loaded from the mongodb GridFS and stored in a local string variable. The code can then be loaded into the current environment using Lua's loadstring method. Note: additional Lua script loaded via this method will be sandboxed. (See above)
Will my service have filesystem access?
No. All persistence must be accomplished via mongodb.
Will my service have direct access to a network stack or socket?
No. The only network access available to a service is via the "web" object passed to your service's main entry point.
Can my service maintain session state between requests?
No. All Moai Cloud services are stateless.
Show me how to write a "hello world" service.
A minimal Moai Cloud service is composed of two Lua script files compressed into a ZIP file and uploaded viadashboard.moaicloud.com.
manifest.lua – (required) – The service's ZIP file must include a lua script file named manifest.lua in the ZIP file's root folder. manifest.lua must contain at a minimum a function named moai_initialize. This method may perform any database initialization necessary and must return the name of the default Lua script resource. In this case, our default resource is named 'init.lua'
init.lua – The services ZIP file must contain a Lua script file of the name returned by the call to moai_initialize in your manifest.lua file. Your default resource file must contain at function named handle_moai_request, which in turn calls the page method on the passed web object.
– the default entry point for all service requests.
– web – an object used to respond to the service request
– req – an object containing the details of the HTTP(S) request made to the service
– ex. web:page("sample messages logged…", 200, 'OK')
web.page(response_body, HTTP_response_code, response_description)