-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete_featured_example.lua
More file actions
176 lines (156 loc) · 4.69 KB
/
complete_featured_example.lua
File metadata and controls
176 lines (156 loc) · 4.69 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
--- Complete Featured Example
--- Run (HTTP):
--- lua ./examples/complete_featured_example.lua
---
--- Optional HTTPS mode:
--- ENABLE_HTTPS=1 \
--- TLS_KEY=./examples/certs/server.key \
--- TLS_CERT=./examples/certs/server.crt \
--- lua ./examples/complete_featured_example.lua
---
--- Test:
--- curl http://localhost:8087/
--- curl http://localhost:8087/health
--- curl "http://localhost:8087/search?q=pudim&page=1&tag=lua&tag=server"
--- curl http://localhost:8087/users/42
--- curl -H "Authorization: Bearer token123" http://localhost:8087/api/protected
---
--- Hot reload test:
--- 1) request /reload
--- 2) edit ./examples/hot_reload_message.lua
--- 3) request /reload again (no restart)
local scriptDir = debug.getinfo(1, "S").source:match("@(.*/)") or "./"
package.path =
scriptDir .. "../?.lua;" ..
scriptDir .. "../?/init.lua;" ..
package.path
local PudimServer = require("PudimServer")
local Cache = require("PudimServer.cache")
local enableHttps = os.getenv("ENABLE_HTTPS") == "1"
local address = os.getenv("HOST") or "localhost"
local port = tonumber(os.getenv("PORT")) or (enableHttps and 8443 or 8087)
local createConfig = {
ServiceName = "Pudim Complete Example",
Address = address,
Port = port,
Concurrency = {
Enabled = true,
Sleep = 0.001,
},
HotReload = {
Enabled = true,
Modules = {"examples.hot_reload_message"}
}
}
if enableHttps then
createConfig.Https = {
Enabled = true,
Key = os.getenv("TLS_KEY") or "./examples/certs/server.key",
Certificate = os.getenv("TLS_CERT") or "./examples/certs/server.crt",
Protocol = "any",
Verify = "none",
}
end
---@type Server
local server = PudimServer:Create(createConfig)
---@cast server PudimServer
server:EnableCors{
AllowOrigins = "*",
AllowMethods = {"GET", "POST", "OPTIONS"},
AllowHeaders = {"Content-Type", "Authorization"},
ExposeHeaders = {"X-Trace-Id"},
MaxAge = 86400,
}
local cache = Cache.new{ MaxSize = 200, DefaultTTL = 30 }
server:UseHandler(Cache.createPipelineHandler(cache))
server:UseHandler{
name = "logger",
Handler = function(req, res, next)
print(("[%s] %s %s"):format(os.date("%H:%M:%S"), req.method, req.path))
return next()
end
}
server:UseHandler{
name = "auth-protected",
Handler = function(req, res, next)
if req.path == "/api/protected" and not req.headers["authorization"] then
return res:response(401, { error = "Unauthorized" })
end
return next()
end
}
server:UseHandler{
name = "trace-header",
Handler = function(req, res, next)
local response = next()
if response then
local traceId = tostring(os.time()) .. "-" .. tostring(math.random(1000, 9999))
response = response:gsub("(\r\n\r\n)", "\r\nX-Trace-Id: " .. traceId .. "%1", 1)
end
return response
end
}
server:Routes("/", function(req, res)
if req.method == "GET" then
return res:response(200, {
service = "Pudim Complete Example",
protocol = enableHttps and "https" or "http",
endpoints = {
"/",
"/health",
"/search?q=...&tag=...",
"/users/:id",
"/api/protected",
"/reload"
}
})
end
return res:response(405, { error = "Method not allowed" })
end)
server:Routes("/health", function(req, res)
if req.method == "GET" then
return res:response(200, {
ok = true,
now = os.date("%Y-%m-%d %H:%M:%S"),
cacheTTL = 30
})
end
return res:response(405, { error = "Method not allowed" })
end)
server:Routes("/search", function(req, res)
if req.method == "GET" then
return res:response(200, {
query = req.query or {},
note = "Repeated query keys become arrays"
})
end
return res:response(405, { error = "Method not allowed" })
end)
server:Routes("/users/:id", function(req, res)
if req.method == "GET" then
return res:response(200, {
userId = req.params and req.params.id,
source = "dynamic route"
})
end
return res:response(405, { error = "Method not allowed" })
end)
server:Routes("/api/protected", function(req, res)
if req.method == "GET" then
return res:response(200, {
message = "Access granted",
authorization = req.headers["authorization"]
})
end
return res:response(405, { error = "Method not allowed" })
end)
server:Routes("/reload", function(req, res)
if req.method == "GET" then
local messageProvider = require("examples.hot_reload_message")
return res:response(200, messageProvider.get())
end
return res:response(405, { error = "Method not allowed" })
end)
print(("=== Complete Example running at %s://%s:%d ==="):format(enableHttps and "https" or "http", address, port))
print("Try: /, /health, /search, /users/:id, /api/protected, /reload")
server:Run()