You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
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/")
|