Created
December 11, 2012 06:11
-
-
Save adelevie/4256280 to your computer and use it in GitHub Desktop.
Using webscript.io's API to build a Sinatra-like web framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[ | |
This is a proof of concept that you can run locally to upload scripts to webscript.io. It provides a simple Sinatra-like API (in Lua) for defining URL routes and their corresponding actions. | |
To run on your own, you'll need your own webscript account (and api key) and Lua installation. | |
Just copy the script locally, fill in your own values in the `app` table below, cd into the directory and run `lua yourfilename.lua`. | |
Then log into webscript.io to make sure everything is there! | |
This is clearly highly experimental, probably broken in many ways, and only supports GET requests. Use at your own risk. Also, I'm totally new to Lua, so feel free to point out any egregious mistakes. | |
]] | |
-- Let's start by defining the most basic parts of the application: | |
-- api credentials and subdomain | |
local app = { | |
api_key = "mykey", | |
username = "[email protected]", | |
subdomain = "mysubdomain" | |
} | |
-- We'll store our routes here | |
app.routes = {} | |
-- Basic method for creating a route | |
function app.routes:create(method, path, script) | |
table.insert(app.routes, {method=method, path=path, script=script}) | |
end | |
-- Pretty simple so far | |
-- app.routes:create("GET", "/hello", [[ | |
-- if not foo then | |
-- print('not foo') | |
-- end | |
-- return 'hello!' | |
-- ]]) | |
-- Adding some syntactic sugar | |
function app:get(path, script) | |
local formatted_script = "if request.method == 'GET' then\n" .. script .. "\nend\n" | |
app.routes:create("GET", path, formatted_script) | |
end | |
-- Looking Sinatra-like | |
-- app:get("/wow", [[ | |
-- return 'my response.' | |
-- ]]) | |
-- Makes it easy to track progress | |
function app:inspect() | |
for i,v in ipairs(app.routes) do | |
print(v.method .. " " .. v.path) | |
print(v.script) | |
end | |
end | |
-- Epic yak shave | |
if not http then | |
http = {} | |
http.request = function(params) | |
if params.method == "POST" then | |
local string = "curl -d ".. params.data .." -u ".. params.auth.username ..":".. params.auth.password .." ".. params.url .."" | |
print(string) -- take a look at the command line to see the curl request | |
os.execute(string) | |
end | |
end | |
end | |
-- Now to upload the application to Webscript | |
function app:upload() | |
for i,route in ipairs(app.routes) do | |
http.request{ | |
method = "POST", | |
url = "https://www.webscript.io/api/0.1/script/".. app.subdomain .. route.path, | |
data = "\""..route.script.."\"", -- this should probably have better string formatting | |
auth = { | |
username = app.username, | |
password = app.api_key | |
} | |
} | |
end | |
end | |
-- after you run this, check your Webscript page to see that the code matches up | |
-- app:upload() | |
-- full example: | |
app:get("/home", [[ | |
return 'Welcome to my website.' | |
]]) | |
app:get("/helloname", [[ | |
return tostring(request.query.name) | |
]]) | |
app:upload() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment