-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrouteWeb.go
182 lines (156 loc) · 5.16 KB
/
routeWeb.go
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
177
178
179
180
181
182
// Copyright (c) 2023 gpress Authors.
//
// This file is part of gpress.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/cloudwego/hertz/pkg/app"
)
// init 初始化函数
func init() {
//初始化静态文件
initStaticFS()
// 异常页面
h.GET("/error", funcError)
// 默认首页
h.GET("/", funcIndex)
h.GET("/page/:pageNo", funcIndex)
h.GET("/page/:pageNo/", funcIndex)
// 查看标签
h.GET("/tag/:urlPathParam", funcListTags)
h.GET("/tag/:urlPathParam/", funcListTags)
h.GET("/tag/:urlPathParam/page/:pageNo", funcListTags)
h.GET("/tag/:urlPathParam/page/:pageNo/", funcListTags)
//初始化导航菜单路由
initCategoryRoute()
}
// funcIndex 模板首页
func funcIndex(ctx context.Context, c *app.RequestContext) {
data := warpRequestMap(c)
cHtml(c, http.StatusOK, "index.html", data)
}
// funcError 错误页面
func funcError(ctx context.Context, c *app.RequestContext) {
cHtml(c, http.StatusOK, "error.html", nil)
}
// funcListCategory 导航菜单数据列表
func funcListCategory(ctx context.Context, c *app.RequestContext) {
data := warpRequestMap(c)
urlPathParam := c.Param("urlPathParam")
if urlPathParam == "" { //导航菜单路径访问,例如:/web
urlPathParam = c.GetString("urlPathParam")
}
data["UrlPathParam"] = urlPathParam
templateFile, err := findThemeTemplate(ctx, tableCategoryName, urlPathParam)
if err != nil || templateFile == "" {
templateFile = "category.html"
}
cHtml(c, http.StatusOK, templateFile, data)
}
// funcListTags 标签列表
func funcListTags(ctx context.Context, c *app.RequestContext) {
data := warpRequestMap(c)
urlPathParam := c.Param("urlPathParam")
data["UrlPathParam"] = urlPathParam
cHtml(c, http.StatusOK, "tag.html", data)
}
// funcOneContent 查询一篇文章
func funcOneContent(ctx context.Context, c *app.RequestContext) {
data := warpRequestMap(c)
urlPathParam := c.Param("urlPathParam")
if urlPathParam == "" { //导航菜单路径访问,例如:/web/nginx-use-hsts
urlPathParam = c.GetString("urlPathParam")
}
data["UrlPathParam"] = urlPathParam
templateFile, err := findThemeTemplate(ctx, tableContentName, urlPathParam)
if err != nil || templateFile == "" {
templateFile = "content.html"
}
cHtml(c, http.StatusOK, templateFile, data)
}
// initCategoryRoute 初始化导航菜单的映射路径
func initCategoryRoute() {
categories, _ := findAllCategory(context.Background())
for i := 0; i < len(categories); i++ {
category := categories[i]
categoryID := category.Id
addCategoryRoute(categoryID)
}
}
// addCategoryRoute 增加导航菜单的路由
func addCategoryRoute(categoryID string) {
// 处理重复注册路由的panic,不对外抛出
defer func() {
if r := recover(); r != nil {
panicMessage := fmt.Sprintf("%s", r)
FuncLogPanic(nil, errors.New(panicMessage))
}
}()
//导航菜单的访问映射
h.GET(funcTrimSuffixSlash(categoryID), addListCategoryRoute(categoryID))
h.GET(categoryID, addListCategoryRoute(categoryID))
//导航菜单分页数据的访问映射
h.GET(categoryID+"page/:pageNo", addListCategoryRoute(categoryID))
h.GET(categoryID+"page/:pageNo/", addListCategoryRoute(categoryID))
//导航菜单下文章的访问映射
h.GET(categoryID+":contentURI", addOneContentRoute(categoryID))
h.GET(categoryID+":contentURI/", addOneContentRoute(categoryID))
}
// addListCategoryRoute 增加导航菜单的GET请求路由,用于自定义设置导航的路由
func addListCategoryRoute(categoryID string) app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
c.Set("urlPathParam", categoryID)
funcListCategory(ctx, c)
}
}
// addOneContentRoute 增加内容的GET请求路由
func addOneContentRoute(categoryID string) app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
contentURI := c.Param("contentURI")
key := categoryID + contentURI
c.Set("urlPathParam", key)
funcOneContent(ctx, c)
}
}
// warpRequestMap 包装请求参数为map
func warpRequestMap(c *app.RequestContext) map[string]interface{} {
pageNoStr := c.Param("pageNo")
if pageNoStr == "" {
pageNoStr = c.GetString("pageNo")
}
if pageNoStr == "" {
//pageNoStr = c.DefaultQuery("pageNo", "1")
pageNoStr = "1"
}
pageNo, _ := strconv.Atoi(pageNoStr)
q := strings.TrimSpace(c.Query("q"))
data := make(map[string]interface{}, 0)
data["pageNo"] = pageNo
data["q"] = q
//设置用户角色,0是访客,1是管理员
userType, ok := c.Get(userTypeKey)
if ok {
data[userTypeKey] = userType
} else {
data[userTypeKey] = 0
}
return data
}