-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_concurrency_example.lua
More file actions
77 lines (68 loc) · 1.85 KB
/
https_concurrency_example.lua
File metadata and controls
77 lines (68 loc) · 1.85 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
--- HTTPS + Concurrency Example
--- Run:
--- # 1) Generate self-signed certs (development only)
--- mkdir -p ./examples/certs
--- openssl req -x509 -newkey rsa:2048 -nodes \
--- -keyout ./examples/certs/server.key \
--- -out ./examples/certs/server.crt \
--- -days 365 \
--- -subj "/CN=localhost"
---
--- # 2) Start server
--- lua ./examples/https_concurrency_example.lua
---
--- Test:
--- curl -k https://localhost:8443/
--- curl -k https://localhost:8443/work
local scriptDir = debug.getinfo(1, "S").source:match("@(.*/)") or "./"
package.path =
scriptDir .. "../?.lua;" ..
scriptDir .. "../?/init.lua;" ..
package.path
local PudimServer = require("PudimServer")
---@type Server
local server = PudimServer:Create{
ServiceName = "HTTPS + Concurrency Example",
Address = "localhost",
Port = 8443,
Https = {
Enabled = true,
Key = "./examples/certs/server.key",
Certificate = "./examples/certs/server.crt",
Protocol = "any",
Verify = "none",
},
Concurrency = {
Enabled = true,
Sleep = 0.001,
}
}
server:Routes("/", function(req, res)
if req.method == "GET" then
return res:response(200, {
ok = true,
protocol = "https",
concurrency = "cooperative coroutines",
now = os.time()
})
end
return res:response(405, { error = "Method not allowed" })
end)
server:Routes("/work", function(req, res)
if req.method == "GET" then
-- Simulate some CPU work
local sum = 0
for i = 1, 100000 do
sum = sum + i
end
return res:response(200, {
message = "heavy work done",
result = sum
})
end
return res:response(405, { error = "Method not allowed" })
end)
print("=== HTTPS + Concurrency Example running at https://localhost:8443 ===")
print("Try: curl -k https://localhost:8443/")
print("Try: curl -k https://localhost:8443/work")
server:Run()