|
|
@ -0,0 +1,47 @@ |
|
|
|
local http = require("http") |
|
|
|
local querystring = require("querystring") |
|
|
|
local qalculate = require("libqalculator").qalc |
|
|
|
local base64 = require("base64") |
|
|
|
|
|
|
|
-- [[ |
|
|
|
-- welcam to best api ever yes |
|
|
|
-- doing this at like 1 am |
|
|
|
-- how to use: |
|
|
|
-- http://localhost:8080/?expression=your shitty expression here&binaryoptions=whatever the fuck |
|
|
|
-- fields: |
|
|
|
-- expression (string) - the thing to calculate |
|
|
|
-- (any of the whatever fields mean that if the option is present, it counts as binary "true". if it isn't, it's a binary "false") |
|
|
|
-- exact (whatever) - try and give an exact answer (do not calculate irrational fractions) |
|
|
|
-- interval (whatever) - give an answer in interval when appropriate |
|
|
|
-- structuring (whatever) - if true, factorize the result. otherwise, expand. |
|
|
|
-- base64 (whatever) - if true, decode the expression as a base64 string first. |
|
|
|
|
|
|
|
local function onRequest(req, res) |
|
|
|
p(req.url:match("%?(.*)$")) |
|
|
|
local options = querystring.parse(req.url:match("%?(.*)$")) |
|
|
|
p(options) |
|
|
|
local expression = options.expression |
|
|
|
local body |
|
|
|
if expression then |
|
|
|
if options.base64 then |
|
|
|
expression = base64.decode(expression) |
|
|
|
end |
|
|
|
body = qalculate( |
|
|
|
expression, |
|
|
|
options.exact, |
|
|
|
options.interval, |
|
|
|
options.structuring |
|
|
|
) |
|
|
|
if not body then |
|
|
|
body = "" |
|
|
|
end |
|
|
|
else |
|
|
|
body = "Invalid request" |
|
|
|
end |
|
|
|
res:setHeader("Content-Type", "text/plain") |
|
|
|
res:setHeader("Content-Length", #body) |
|
|
|
res:finish(body) |
|
|
|
end |
|
|
|
|
|
|
|
http.createServer(onRequest):listen(8080) |
|
|
|
print("Server listening at http://localhost:8080/") |