|
| 1 | +local server = require("@lute/net/server") |
| 2 | +local fs = require("@std/fs") |
| 3 | +local json = require("@std/json") |
| 4 | + |
| 5 | +local TEXT_HEADERS = { ["Content-Type"] = "text/plain" } |
| 6 | +local JSON_HEADERS = { ["Content-Type"] = "application/json" } |
| 7 | +local NOT_FOUND = { status = 404, body = "", headers = TEXT_HEADERS } |
| 8 | +local UPGRADE_FAILED = { status = 400, body = "Upgrade failed", headers = TEXT_HEADERS } |
| 9 | +local PIPELINE_OK = { body = "ok", headers = TEXT_HEADERS } |
| 10 | + |
| 11 | +local dataset: any = nil |
| 12 | +local datasetLen = 0 |
| 13 | + |
| 14 | +local function buildJson(count: number, m: number): string |
| 15 | + local items = table.create(count) |
| 16 | + for i = 1, count do |
| 17 | + local src = dataset[i] |
| 18 | + items[i] = { |
| 19 | + id = src.id, |
| 20 | + name = src.name, |
| 21 | + category = src.category, |
| 22 | + price = src.price, |
| 23 | + quantity = src.quantity, |
| 24 | + active = src.active, |
| 25 | + tags = src.tags, |
| 26 | + rating = src.rating, |
| 27 | + total = src.price * src.quantity * m, |
| 28 | + } |
| 29 | + end |
| 30 | + return json.serialize({ count = count, items = items }) |
| 31 | +end |
| 32 | + |
| 33 | +local function handle(req, srv) |
| 34 | + local path = req.path |
| 35 | + local method = req.method |
| 36 | + |
| 37 | + if path == "/ws" then |
| 38 | + if srv:upgrade(req) then |
| 39 | + return |
| 40 | + end |
| 41 | + return UPGRADE_FAILED |
| 42 | + end |
| 43 | + |
| 44 | + if path == "/baseline11" then |
| 45 | + local a = tonumber(req.query.a) or 0 |
| 46 | + local b = tonumber(req.query.b) or 0 |
| 47 | + if method == "POST" then |
| 48 | + local c = tonumber(req.body) or 0 |
| 49 | + return { body = tostring(a + b + c), headers = TEXT_HEADERS } |
| 50 | + end |
| 51 | + return { body = tostring(a + b), headers = TEXT_HEADERS } |
| 52 | + end |
| 53 | + |
| 54 | + if path == "/pipeline" then |
| 55 | + return PIPELINE_OK |
| 56 | + end |
| 57 | + |
| 58 | + if path == "/upload" and method == "POST" then |
| 59 | + return { body = tostring(#req.body), headers = TEXT_HEADERS } |
| 60 | + end |
| 61 | + |
| 62 | + if string.sub(path, 1, 6) == "/json/" then |
| 63 | + local count = tonumber(string.sub(path, 7)) |
| 64 | + if count and count >= 0 and count <= datasetLen then |
| 65 | + local m = tonumber(req.query.m) or 1 |
| 66 | + return { body = buildJson(count, m), headers = JSON_HEADERS } |
| 67 | + end |
| 68 | + return NOT_FOUND |
| 69 | + end |
| 70 | + |
| 71 | + return NOT_FOUND |
| 72 | +end |
| 73 | + |
| 74 | +local wsHandlers = { |
| 75 | + message = function(ws, message) |
| 76 | + ws:send(message) |
| 77 | + end, |
| 78 | +} |
| 79 | + |
| 80 | +return { |
| 81 | + serve = function() |
| 82 | + dataset = json.deserialize(fs.readFileToString("/data/dataset.json")) :: any |
| 83 | + datasetLen = #dataset |
| 84 | + |
| 85 | + server.serve({ |
| 86 | + hostname = "0.0.0.0", |
| 87 | + port = 8080, |
| 88 | + reuseport = true, |
| 89 | + handler = handle, |
| 90 | + websocket = wsHandlers, |
| 91 | + }) |
| 92 | + |
| 93 | + if fs.exists("/certs/server.crt") and fs.exists("/certs/server.key") then |
| 94 | + server.serve({ |
| 95 | + hostname = "0.0.0.0", |
| 96 | + port = 8081, |
| 97 | + reuseport = true, |
| 98 | + tls = { |
| 99 | + certfilename = "/certs/server.crt", |
| 100 | + keyfilename = "/certs/server.key", |
| 101 | + }, |
| 102 | + handler = handle, |
| 103 | + websocket = wsHandlers, |
| 104 | + }) |
| 105 | + end |
| 106 | + end, |
| 107 | +} |
0 commit comments