Skip to content

Commit e60a42c

Browse files
authored
feat: improve code import and commnet (#18)
* feat: improve code import and commnet * feat: improve code * fix: modify err * fix: modify table to
1 parent 225b7ee commit e60a42c

30 files changed

+269
-157
lines changed

controller/admin_controller.go

+39-24
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,32 @@ package controller
1111
import (
1212
"fmt"
1313
"net/http"
14+
"strconv"
15+
"strings"
16+
"time"
17+
1418
"ohurlshortener/core"
1519
"ohurlshortener/service"
1620
"ohurlshortener/utils"
1721
"ohurlshortener/utils/export"
18-
"strconv"
19-
"strings"
20-
"time"
2122

2223
"github.com/dchest/captcha"
2324
"github.com/gin-gonic/gin"
2425
)
2526

2627
const (
27-
DEFAULT_PAGE_NUM = 1
28-
DEFAULT_PAGE_SIZE = 20
28+
DefaultPageNum = 1
29+
DefaultPageSize = 20
2930
)
3031

32+
// LoginPage 登录页面
3133
func LoginPage(c *gin.Context) {
3234
c.HTML(http.StatusOK, "login.html", gin.H{
3335
"title": "登录 - ohUrlShortener",
3436
})
3537
}
3638

39+
// DoLogin 登录
3740
func DoLogin(c *gin.Context) {
3841
account := c.PostForm("account")
3942
password := c.PostForm("password")
@@ -56,7 +59,7 @@ func DoLogin(c *gin.Context) {
5659
return
5760
}
5861

59-
//验证码有效性验证
62+
// 验证码有效性验证
6063
if !captcha.VerifyString(captchaId, captchaText) {
6164
c.HTML(http.StatusOK, "login.html", gin.H{
6265
"title": "错误 - ohUrlShortener",
@@ -65,7 +68,7 @@ func DoLogin(c *gin.Context) {
6568
return
6669
}
6770

68-
//用户名密码有效性验证
71+
// 用户名密码有效性验证
6972
loginUser, err := service.Login(account, password)
7073
if err != nil || loginUser.IsEmpty() {
7174
c.HTML(http.StatusOK, "login.html", gin.H{
@@ -75,7 +78,7 @@ func DoLogin(c *gin.Context) {
7578
return
7679
}
7780

78-
//Write Cookie to browser
81+
// Write Cookie to browser
7982
cValue, err := AdminCookieValue(loginUser)
8083
if err != nil {
8184
c.HTML(http.StatusOK, "login.html", gin.H{
@@ -89,21 +92,25 @@ func DoLogin(c *gin.Context) {
8992
c.Redirect(http.StatusFound, "/admin/dashboard")
9093
}
9194

95+
// DoLogout 登出
9296
func DoLogout(c *gin.Context) {
9397
c.SetCookie("ohUrlShortenerAdmin", "", -1, "/", "", false, true)
9498
c.SetCookie("ohUrlShortenerCookie", "", -1, "/", "", false, true)
9599
c.Redirect(http.StatusFound, "/login")
96100
}
97101

102+
// ServeCaptchaImage 生成验证码
98103
func ServeCaptchaImage(c *gin.Context) {
99104
captcha.Server(200, 45).ServeHTTP(c.Writer, c.Request)
100105
}
101106

107+
// RequestCaptchaImage 请求验证码
102108
func RequestCaptchaImage(c *gin.Context) {
103109
imageId := captcha.New()
104110
c.JSON(http.StatusOK, core.ResultJsonSuccessWithData(imageId))
105111
}
106112

113+
// ChangeState 修改状态
107114
func ChangeState(c *gin.Context) {
108115
destUrl := c.PostForm("dest_url")
109116
enable := c.PostForm("enable")
@@ -128,6 +135,7 @@ func ChangeState(c *gin.Context) {
128135
c.JSON(http.StatusOK, core.ResultJsonSuccessWithData(result))
129136
}
130137

138+
// DeleteShortUrl 删除短链接
131139
func DeleteShortUrl(c *gin.Context) {
132140
url := c.PostForm("short_url")
133141
if utils.EmptyString(strings.TrimSpace(url)) {
@@ -144,6 +152,7 @@ func DeleteShortUrl(c *gin.Context) {
144152
c.JSON(http.StatusOK, core.ResultJsonSuccess())
145153
}
146154

155+
// GenerateShortUrl 生成短链接
147156
func GenerateShortUrl(c *gin.Context) {
148157
destUrl := c.PostForm("dest_url")
149158
memo := c.PostForm("memo")
@@ -165,17 +174,18 @@ func GenerateShortUrl(c *gin.Context) {
165174
c.JSON(http.StatusOK, core.ResultJsonSuccessWithData(json))
166175
}
167176

177+
// StatsPage 统计页面
168178
func StatsPage(c *gin.Context) {
169179
url := c.DefaultQuery("url", "")
170-
strPage := c.DefaultQuery("page", strconv.Itoa(DEFAULT_PAGE_NUM))
171-
strSize := c.DefaultQuery("size", strconv.Itoa(DEFAULT_PAGE_SIZE))
180+
strPage := c.DefaultQuery("page", strconv.Itoa(DefaultPageNum))
181+
strSize := c.DefaultQuery("size", strconv.Itoa(DefaultPageSize))
172182
page, err := strconv.Atoi(strPage)
173183
if err != nil {
174-
page = DEFAULT_PAGE_NUM
184+
page = DefaultPageNum
175185
}
176186
size, err := strconv.Atoi(strSize)
177187
if err != nil {
178-
size = DEFAULT_PAGE_SIZE
188+
size = DefaultPageSize
179189
}
180190
urls, err := service.GetPagedUrlIpCountStats(strings.TrimSpace(url), page, size)
181191
c.HTML(http.StatusOK, "stats.html", gin.H{
@@ -192,17 +202,18 @@ func StatsPage(c *gin.Context) {
192202
})
193203
}
194204

205+
// SearchStatsPage 查询统计页面
195206
func SearchStatsPage(c *gin.Context) {
196207
url := c.DefaultQuery("url", "")
197-
strPage := c.DefaultQuery("page", strconv.Itoa(DEFAULT_PAGE_NUM))
198-
strSize := c.DefaultQuery("size", strconv.Itoa(DEFAULT_PAGE_SIZE))
208+
strPage := c.DefaultQuery("page", strconv.Itoa(DefaultPageNum))
209+
strSize := c.DefaultQuery("size", strconv.Itoa(DefaultPageSize))
199210
page, err := strconv.Atoi(strPage)
200211
if err != nil {
201-
page = DEFAULT_PAGE_NUM
212+
page = DefaultPageNum
202213
}
203214
size, err := strconv.Atoi(strSize)
204215
if err != nil {
205-
size = DEFAULT_PAGE_SIZE
216+
size = DefaultPageSize
206217
}
207218
urls, err := service.GetPagedUrlIpCountStats(strings.TrimSpace(url), page, size)
208219
c.HTML(http.StatusOK, "search_stats.html", gin.H{
@@ -219,17 +230,18 @@ func SearchStatsPage(c *gin.Context) {
219230
})
220231
}
221232

233+
// UrlsPage 短链接列表页面
222234
func UrlsPage(c *gin.Context) {
223235
url := c.DefaultQuery("url", "")
224-
strPage := c.DefaultQuery("page", strconv.Itoa(DEFAULT_PAGE_NUM))
225-
strSize := c.DefaultQuery("size", strconv.Itoa(DEFAULT_PAGE_SIZE))
236+
strPage := c.DefaultQuery("page", strconv.Itoa(DefaultPageNum))
237+
strSize := c.DefaultQuery("size", strconv.Itoa(DefaultPageSize))
226238
page, err := strconv.Atoi(strPage)
227239
if err != nil {
228-
page = DEFAULT_PAGE_NUM
240+
page = DefaultPageNum
229241
}
230242
size, err := strconv.Atoi(strSize)
231243
if err != nil {
232-
size = DEFAULT_PAGE_SIZE
244+
size = DefaultPageSize
233245
}
234246
urls, err := service.GetPagesShortUrls(strings.TrimSpace(url), page, size)
235247
c.HTML(http.StatusOK, "urls.html", gin.H{
@@ -246,19 +258,20 @@ func UrlsPage(c *gin.Context) {
246258
})
247259
}
248260

261+
// AccessLogsPage 访问日志页面
249262
func AccessLogsPage(c *gin.Context) {
250263
url := c.DefaultQuery("url", "")
251-
strPage := c.DefaultQuery("page", strconv.Itoa(DEFAULT_PAGE_NUM))
252-
strSize := c.DefaultQuery("size", strconv.Itoa(DEFAULT_PAGE_SIZE))
264+
strPage := c.DefaultQuery("page", strconv.Itoa(DefaultPageNum))
265+
strSize := c.DefaultQuery("size", strconv.Itoa(DefaultPageSize))
253266
start := c.DefaultQuery("start", "")
254267
end := c.DefaultQuery("end", "")
255268
page, err := strconv.Atoi(strPage)
256269
if err != nil {
257-
page = DEFAULT_PAGE_NUM
270+
page = DefaultPageNum
258271
}
259272
size, err := strconv.Atoi(strSize)
260273
if err != nil {
261-
size = DEFAULT_PAGE_SIZE
274+
size = DefaultPageSize
262275
}
263276

264277
totalCount, distinctIpCount, err := service.GetAccessLogsCount(strings.TrimSpace(url), start, end)
@@ -281,6 +294,7 @@ func AccessLogsPage(c *gin.Context) {
281294
})
282295
}
283296

297+
// AccessLogsExport 导出访问日志
284298
func AccessLogsExport(c *gin.Context) {
285299
url := c.PostForm("url")
286300
logs, err := service.GetAllAccessLogs(strings.TrimSpace(url))
@@ -314,6 +328,7 @@ func AccessLogsExport(c *gin.Context) {
314328
c.Data(http.StatusOK, "pplication/octet-stream", fileContent)
315329
}
316330

331+
// DashboardPage 仪表盘页面
317332
func DashboardPage(c *gin.Context) {
318333
count, stats, err := service.GetSumOfUrlStats()
319334
if err != nil {

controller/api_controller.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ package controller
1111
import (
1212
"fmt"
1313
"net/http"
14+
"strconv"
15+
"strings"
16+
1417
"ohurlshortener/core"
1518
"ohurlshortener/service"
1619
"ohurlshortener/utils"
17-
"strconv"
18-
"strings"
1920

2021
"github.com/gin-gonic/gin"
2122
)
2223

2324
// APINewAdmin
2425
//
25-
//Add new admin user
26+
// Add new admin user
2627
func APINewAdmin(ctx *gin.Context) {
2728
account := ctx.PostForm("account")
2829
password := ctx.PostForm("password")
@@ -48,7 +49,7 @@ func APINewAdmin(ctx *gin.Context) {
4849

4950
// APIAdminUpdate
5051
//
51-
//Update password of given admin user
52+
// Update password of given admin user
5253
func APIAdminUpdate(ctx *gin.Context) {
5354
account := ctx.Param("account")
5455
password := ctx.PostForm("password")
@@ -103,7 +104,7 @@ func APIUrlInfo(ctx *gin.Context) {
103104
}
104105

105106
stat, err := service.GetShortUrlStats(strings.TrimSpace(url))
106-
if utils.EmptyString(strings.TrimSpace(url)) {
107+
if err != nil {
107108
ctx.JSON(http.StatusInternalServerError, core.ResultJsonError(err.Error()))
108109
return
109110
}
@@ -135,6 +136,7 @@ func APIUpdateUrl(ctx *gin.Context) {
135136
ctx.JSON(http.StatusOK, core.ResultJsonSuccessWithData(res))
136137
}
137138

139+
// APIDeleteUrl Delete Short Url
138140
func APIDeleteUrl(ctx *gin.Context) {
139141
url := ctx.Param("url")
140142
if utils.EmptyString(strings.TrimSpace(url)) {

controller/handlers.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,26 @@ import (
1212
"fmt"
1313
"log"
1414
"net/http"
15+
"strconv"
16+
"strings"
17+
1518
"ohurlshortener/core"
1619
"ohurlshortener/service"
1720
"ohurlshortener/storage"
1821
"ohurlshortener/utils"
19-
"strconv"
20-
"strings"
2122

2223
"github.com/gin-gonic/gin"
2324
)
2425

2526
const (
26-
authoriationHeaderKey = "Authorization"
27-
authoriationTypeBearer = "Bearer"
27+
authorizationHeaderKey = "Authorization"
28+
authorizationTypeBearer = "Bearer"
2829
)
2930

3031
// APIAuthHandler Authorization for /api
3132
func APIAuthHandler() gin.HandlerFunc {
3233
return func(ctx *gin.Context) {
33-
authHeader := ctx.GetHeader(authoriationHeaderKey)
34+
authHeader := ctx.GetHeader(authorizationHeaderKey)
3435
if utils.EmptyString(authHeader) {
3536
ctx.AbortWithStatusJSON(http.StatusUnauthorized, core.ResultJsonUnauthorized("Authorization Header is empty"))
3637
return
@@ -42,7 +43,7 @@ func APIAuthHandler() gin.HandlerFunc {
4243
return
4344
}
4445

45-
if fields[0] != authoriationTypeBearer {
46+
if fields[0] != authorizationTypeBearer {
4647
ctx.AbortWithStatusJSON(http.StatusUnauthorized, core.ResultJsonUnauthorized("Unsupported Authorization Type"))
4748
return
4849
}
@@ -63,6 +64,7 @@ func APIAuthHandler() gin.HandlerFunc {
6364
}
6465
}
6566

67+
// AdminCookieValue Generate cookie value for admin user
6668
func AdminCookieValue(user core.User) (string, error) {
6769
var result string
6870
data, err := utils.Sha256Of(user.Account + "a=" + user.Password + "=e" + strconv.Itoa(user.ID))
@@ -73,19 +75,20 @@ func AdminCookieValue(user core.User) (string, error) {
7375
return utils.Base58Encode(data), nil
7476
}
7577

78+
// AdminAuthHandler Authorization for /admin
7679
func AdminAuthHandler() gin.HandlerFunc {
7780
return func(c *gin.Context) {
7881
user, err := c.Cookie("ohUrlShortenerAdmin")
7982
if err != nil {
8083
c.AbortWithStatus(http.StatusUnauthorized)
81-
//c.AbortWithError(http.StatusFound, err)
84+
// c.AbortWithError(http.StatusFound, err)
8285
return
8386
}
8487

8588
cookie, err := c.Cookie("ohUrlShortenerCookie")
8689
if err != nil {
8790
c.AbortWithStatus(http.StatusUnauthorized)
88-
//c.Redirect(http.StatusFound, "/login")
91+
// c.Redirect(http.StatusFound, "/login")
8992
return
9093
}
9194

@@ -122,9 +125,10 @@ func AdminAuthHandler() gin.HandlerFunc {
122125
}
123126

124127
c.Next()
125-
} //end of func
128+
} // end of func
126129
}
127130

131+
// WebLogFormatHandler Customized log format for web
128132
func WebLogFormatHandler(server string) gin.HandlerFunc {
129133
return gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
130134
if !strings.HasPrefix(param.Path, "/assets") {
@@ -139,10 +143,10 @@ func WebLogFormatHandler(server string) gin.HandlerFunc {
139143
param.Request.UserAgent(),
140144
param.ErrorMessage,
141145
)
142-
} //end of if
146+
} // end of if
143147
return ""
144-
}) //end of formatter
145-
} //end of func
148+
}) // end of formatter
149+
} // end of func
146150

147151
func validateToken(token string) (bool, error) {
148152
users, err := storage.FindAllUsers()

controller/urls_controller.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ package controller
1010

1111
import (
1212
"net/http"
13+
1314
"ohurlshortener/service"
1415
"ohurlshortener/utils"
1516

1617
"github.com/gin-gonic/gin"
1718
)
1819

20+
// ShortUrlDetail 重定向到目标地址
1921
func ShortUrlDetail(c *gin.Context) {
2022
url := c.Param("url")
2123
if utils.EmptyString(url) {
@@ -50,6 +52,5 @@ func ShortUrlDetail(c *gin.Context) {
5052
}
5153

5254
go service.NewAccessLog(url, c.ClientIP(), c.Request.UserAgent(), c.Request.Referer())
53-
5455
c.Redirect(http.StatusFound, destUrl)
5556
}

core/access_log.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414
)
1515

16+
// AccessLog 访问日志
1617
type AccessLog struct {
1718
ID int64 `db:"id"`
1819
ShortUrl string `db:"short_url"`

0 commit comments

Comments
 (0)