Cloning Set Get Go with Webscript

A friend pointed out Set Get Go, billed as "The Web's Get/Set API." It's a charmingly simple service that allows JavaScript callers to store and retrieve key/value pairs. Given Webscript's support for HTTP and for persisted storage, I thought it would be an interesting exercise to reimplement Set Get Go with Webscript.

Set Get Go's interface is based on HTTP GET and defines two URLs: get and set. The "get" URL takes an Id and Name as query parameters, which together represent the key being retrieved. The "set" URL accepts these parameters plus a Value parameter, which gives the value to be stored.

My first take at Set Get Go:

setgetgo.webscript.io/set:

local key = request.query.Id .. ':' .. request.query.Name
local value = request.query.Value
storage[key] = value
return 200

setgetgo.webscript.io/get:

local key = request.query.Id .. ':' .. request.query.Name
return storage[key]

I should note that the real Set Get Go returns JSON with a little more data (like when the value was stored). This code just returns the text value.

Making Cross-Domain Calls

Thanks to Webscript's support for Cross-Origin Request Sharing (CORS), the above code can be called from JavaScript running in all modern browsers. Set Get Go, though, includes support for JSONP. Supporting JSONP just requires wrapping the return value in a JavaScript function call. For example, rather than returning value, we must return cb("value"), where cb comes from a callback parameter.

Here's an improved version of the "get" script that uses JSONP when the callback parameter is present:

local key = request.query.Id .. ':' .. request.query.Name
local callback = request.query.callback
-- handle JSONP
if callback then
        return string.format('%s(%s)', callback,
                        json.stringify(storage[key])),
                {['Content-Type'] = 'application/javascript'}
else
        return storage[key]
end

This script is running at setgetgo.webscript.io/get and setgetgo.webscript.io/set, so feel free to give it a try.