This repository was archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouter.lua
More file actions
127 lines (110 loc) · 3.34 KB
/
Copy pathrouter.lua
File metadata and controls
127 lines (110 loc) · 3.34 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
-- 业务路由管理
local post_model = require("app.models.post")
local utils = require("app.libs.utils")
local config = require("app.config.config")
return function(app)
-- welcome to lor!
app:get("/", function(req, res, next)
local result = post_model:test()
local data = {
posts = result
}
res:render("index", data)
end)
-- hello world!
app:get("/markdown", function(req, res, next)
-- session validor
local user = req.session.get("me")
local name = user.username
if not name then
res:redirect("/login")
return
end
res:render("markdown")
end)
-- new post
app:post("/markdown", function(req, res, next)
-- session validor
local user = req.session.get("me")
local name = user.username
if not name then
res:redirect("/login")
return
end
local title = req.body.title
local content = req.body.content
-- post data is not
if not title or not content or titile == "" or content == "" then
local data = {
message = "title or content is not"
}
res:render("markdown", data)
return
end
-- insert
local result, err = post_model:new(title, content)
-- insert is err
if not result or err then
local data = {
message = "save post error"
}
res:render("markdown", data)
return
end
-- save sucess
res:redirect("/")
end)
-- get post
app:get("/post/:id", function(req, res, next)
local post_id = req.params.id
if not post_id then
res:redirect("/")
return
end
local result, err = post_model:get(post_id)
if not result or err or type(result) ~= "table" or #result ~= 1 then
res:send("无法查找到该文章")
return
end
local data = {
post = result[1]
}
res:render("post", data)
end)
-- login get
app:get("/login", function(req, res, next)
res:render("login")
end)
-- login post
app:post("/login", function(req, res, next)
local username = req.body.name
local password = req.body.password
if not username or not password or username == "" or password == "" then
local data = {
message = "username or password is not"
}
res:render("login", data)
return
end
-- username && password
enuname = utils.encode(username .. "#username" .. config.pwd_secret)
enpassword = utils.encode(password .. "#password" .. config.pwd_secret)
if enuname ~= config.auth.username or enpassword ~= config.auth.password then
local data = {
message = "username or password error"
}
res:render("login", data)
return
end
-- save session
req.session.set("me",{
username = enuname
})
-- go index
res:redirect("/")
end)
-- render html, visit "/view" or "/view?name=foo&desc=bar
app:get("/about", function(req, res, next)
res:render("about", data)
end)
end