From 1c5e17a4abec5a34b74508ee6a7baea8205385bf Mon Sep 17 00:00:00 2001 From: nite07 Date: Wed, 13 Mar 2024 16:28:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=A3=E6=9E=90=E7=9F=AD=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/handler/short_link.go | 25 ++++++++++++++++- api/route.go | 14 ++++++---- api/static/index.html | 12 ++++---- api/static/index.js | 59 ++++++++++++++++++++++++++++++++------- 4 files changed, 87 insertions(+), 23 deletions(-) diff --git a/api/handler/short_link.go b/api/handler/short_link.go index 1d82b42..de00e49 100644 --- a/api/handler/short_link.go +++ b/api/handler/short_link.go @@ -70,7 +70,30 @@ func ShortLinkGenHandler(c *gin.Context) { c.String(200, hash) } -func ShortLinkGetHandler(c *gin.Context) { +func ShortLinkGetUrlHandler(c *gin.Context) { + var params validator.ShortLinkGetValidator + if err := c.ShouldBindQuery(¶ms); err != nil { + c.String(400, "参数错误: "+err.Error()) + return + } + if strings.TrimSpace(params.Hash) == "" { + c.String(400, "参数错误") + return + } + var shortLink model.ShortLink + result := database.FindShortLinkByHash(params.Hash, &shortLink) + if result.Error != nil { + c.String(404, "未找到短链接") + return + } + if shortLink.Password != "" && shortLink.Password != params.Password { + c.String(403, "密码错误") + return + } + c.String(200, shortLink.Url) +} + +func ShortLinkGetConfigHandler(c *gin.Context) { // 获取动态路由 hash := c.Param("hash") password := c.Query("password") diff --git a/api/route.go b/api/route.go index 4c02550..6d5c940 100644 --- a/api/route.go +++ b/api/route.go @@ -53,14 +53,18 @@ func SetRoute(r *gin.Engine) { handler.SubHandler(c) }, ) - r.POST( - "/short", func(c *gin.Context) { - handler.ShortLinkGenHandler(c) + r.GET( + "/s/:hash", func(c *gin.Context) { + handler.ShortLinkGetConfigHandler(c) }, ) r.GET( - "/s/:hash", func(c *gin.Context) { - handler.ShortLinkGetHandler(c) + "/short", func(c *gin.Context) { + handler.ShortLinkGetUrlHandler(c) + }) + r.POST( + "/short", func(c *gin.Context) { + handler.ShortLinkGenHandler(c) }, ) r.PUT( diff --git a/api/static/index.html b/api/static/index.html index 68fb48f..27d6d65 100644 --- a/api/static/index.html +++ b/api/static/index.html @@ -13,7 +13,6 @@ src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"> -