-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.lua
More file actions
70 lines (55 loc) · 1.66 KB
/
app.lua
File metadata and controls
70 lines (55 loc) · 1.66 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
-- app.lua
local lapis = require("lapis")
local app = lapis.Application()
local db = require("lapis.db")
local function isConnectedToDB()
local res = db.query("SELECT NOW() as time")
if res and #res > 0 then
print("Successfully connected to the database. Current time is: " .. res[1].time)
else
print("Failed to connect to the database.")
end
end
app:get("/", function(self)
isConnectedToDB()
return { render = "index" }
end)
lapis.Application.set_cookie = function(self, name, value, opts)
local defaults = {
path = "/",
httponly = true,
secure = self.req.parsed_url.scheme == "https",
samesite = "None"
}
for k, v in pairs(defaults) do
if opts[k] == nil then
opts[k] = v
end
end
return lapis.Application.super.set_cookie(self, name, value, opts)
end
local function modify_cookies_for_samesite(self)
local cookies = self.res.headers["Set-Cookie"]
if cookies then
-- If there's only one cookie being set, Lua returns it as a string.
-- Convert it to a table for consistency.
if type(cookies) == "string" then
cookies = {cookies}
end
for i, cookie in ipairs(cookies) do
-- Only modify the session cookie
if cookie:match("^lapis_session=") then
-- Check if the SameSite attribute is missing. If it is, add it.
if not cookie:match("SameSite=") then
cookies[i] = cookie .. "; SameSite=None; Secure"
end
end
end
-- Update the headers
self.res.headers["Set-Cookie"] = cookies
end
end
app:before_filter(modify_cookies_for_samesite)
-- Include the routes
app:include("routes")
return app