2023-02-05 16:36:57 +00:00
|
|
|
--> A basic http server that echoes the given request
|
2023-02-04 04:40:27 +00:00
|
|
|
--> body at /ping and otherwise responds 404 "Not Found"
|
|
|
|
|
|
|
|
local PORT = if process.env.PORT ~= nil and #process.env.PORT > 0
|
|
|
|
then assert(tonumber(process.env.PORT), "Failed to parse port from env")
|
|
|
|
else 8080
|
|
|
|
|
|
|
|
-- Create our responder functions
|
|
|
|
|
2023-02-05 04:49:51 +00:00
|
|
|
local function pong(request: NetRequest): string
|
|
|
|
return `Pong!\n{request.path}\n{request.body}`
|
|
|
|
end
|
|
|
|
|
|
|
|
local function teapot(request: NetRequest): NetResponse
|
2023-02-04 04:40:27 +00:00
|
|
|
return {
|
2023-02-05 04:49:51 +00:00
|
|
|
status = 418,
|
|
|
|
body = "🫖",
|
2023-02-04 04:40:27 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function notFound(request: NetRequest): NetResponse
|
|
|
|
return {
|
|
|
|
status = 404,
|
|
|
|
body = "Not Found",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Run the server on port 8080
|
|
|
|
|
2023-02-11 13:25:53 +00:00
|
|
|
local handle = net.serve(PORT, function(request)
|
2023-02-05 04:49:51 +00:00
|
|
|
if string.sub(request.path, 1, 5) == "/ping" then
|
2023-02-04 04:40:27 +00:00
|
|
|
return pong(request)
|
2023-02-05 04:49:51 +00:00
|
|
|
elseif string.sub(request.path, 1, 7) == "/teapot" then
|
|
|
|
return teapot(request)
|
2023-02-04 04:40:27 +00:00
|
|
|
else
|
|
|
|
return notFound(request)
|
|
|
|
end
|
|
|
|
end)
|
2023-02-11 13:25:53 +00:00
|
|
|
|
|
|
|
print(`Listening on port {PORT} 🚀`)
|
|
|
|
|
|
|
|
-- Exit our example after a small delay, if you copy this
|
|
|
|
-- example just remove this part to keep the server running
|
|
|
|
|
|
|
|
task.delay(2, function()
|
|
|
|
print("Shutting down...")
|
|
|
|
task.wait(1)
|
|
|
|
handle.stop()
|
|
|
|
end)
|