-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_example.lua
More file actions
82 lines (69 loc) · 2.27 KB
/
pipeline_example.lua
File metadata and controls
82 lines (69 loc) · 2.27 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
--- Pipeline Example
--- Run: lua ./examples/pipeline_example.lua
--- Test: curl http://localhost:8084/api/public
--- Test: curl http://localhost:8084/api/protected
--- Test: curl -H "Authorization: Bearer token123" http://localhost:8084/api/protected
local PudimServer = require("PudimServer")
local server = PudimServer:Create{
Port = 8084,
Address = "localhost",
ServiceName = "Pipeline Example",
Middlewares = {}
}
-- Pipeline handler 1: Request logger
server:UseHandler{
name = "logger",
Handler = function(req, res, next)
print(("[%s] %s %s"):format(os.date("%H:%M:%S"), req.method, req.path))
local response = next()
print(("[%s] %s %s -> done"):format(os.date("%H:%M:%S"), req.method, req.path))
return response
end
}
-- Pipeline handler 2: Auth check (short-circuits if no token on /api/protected)
server:UseHandler{
name = "auth",
Handler = function(req, res, next)
if req.path == "/api/protected" and not req.headers["authorization"] then
return res:response(401, { error = "Unauthorized: missing Authorization header" })
end
return next()
end
}
-- Pipeline handler 3: Add custom header to all responses
server:UseHandler{
name = "custom-header",
Handler = function(req, res, next)
local response = next()
if response then
-- Inject a custom header before the body
response = response:gsub(
"(\r\n\r\n)",
"\r\nX-Powered-By: PudimServer%1",
1
)
end
return response
end
}
-- Routes
server:Routes("/api/public", function(req, res)
if req.method == "GET" then
return res:response(200, { msg = "This is public!" })
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, {
msg = "Welcome! You are authenticated.",
token = req.headers["authorization"]
})
end
return res:response(405, { error = "Method not allowed" })
end)
print("=== Pipeline Example running at http://localhost:8084 ===")
print("Try: curl http://localhost:8084/api/public")
print("Try: curl http://localhost:8084/api/protected (401 - blocked by auth)")
print('Try: curl -H "Authorization: Bearer token123" http://localhost:8084/api/protected')
server:Run()