-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.lua
40 lines (32 loc) · 1.21 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
local socket = require("socket")
local copas = require("copas")
local json = require("dkjson")
local version_info = { version = "1.0.0" }
local function handle_request(client)
client = copas.wrap(client)
local request = client:receive("*l")
if request then
local method, path = request:match("^(%w+)%s(/%S*)%sHTTP/%d.%d")
if method == "GET" and path == "/version" then
local response_body = json.encode(version_info, { indent = false })
local response = "HTTP/1.1 200 OK\r\n" ..
"Content-Type: application/json\r\n" ..
"Content-Length: " .. #response_body .. "\r\n" ..
"Connection: close\r\n\r\n" ..
response_body
client:send(response)
else
local response = "HTTP/1.1 404 Not Found\r\n" ..
"Content-Type: text/plain\r\n" ..
"Content-Length: 13\r\n" ..
"Connection: close\r\n\r\n" ..
"404 Not Found"
client:send(response)
end
end
client:close()
end
local server = assert(socket.bind("*", 8000))
print("listening on http://localhost:8000")
copas.addserver(server, handle_request)
copas.loop()