From e69c25bce5f4093dadc2761d609292b75039a245 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Sat, 18 Mar 2023 01:33:33 +0400 Subject: [PATCH] functional prototype --- .gitignore | 3 ++- install.sh | 13 +++++++++++++ main.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 install.sh create mode 100644 main.lua diff --git a/.gitignore b/.gitignore index bfec31e..ebaee04 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -*.so +*so /luvit /lit /luvi +/base64.lua diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..02ee488 --- /dev/null +++ b/install.sh @@ -0,0 +1,13 @@ +#!/bin/bash +if [ ! -f ./luaqalculator/libqalculator.so ]; then + cd luaqalculator + make + mv libqalculator.so ../ + cd .. +fi +if [ ! -f ./luvit ]; then + curl -L https://github.com/luvit/lit/raw/master/get-lit.sh | sh +fi +if [ ! -f ./base64.lua ]; then + curl -L https://github.com/iskolbin/lbase64/raw/master/base64.lua > base64.lua +fi diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..bf6c371 --- /dev/null +++ b/main.lua @@ -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/")