From 036907fba433b426ea270640329be1d04a89fe38 Mon Sep 17 00:00:00 2001 From: Nite07 Date: Thu, 9 May 2024 12:58:33 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Parse=20short=20link?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- api/handler/short_link.go | 26 ++++++++++++++++++++++++++ api/route.go | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 1c3564c..172928b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "type": "go", "request": "launch", "mode": "debug", - "program": "${workspaceFolder}/main.go", + "program": "${workspaceFolder}", "output": "${workspaceFolder}/dist/main.exe", "buildFlags": "-ldflags '-X sub2clash/constant.Version=dev'" } diff --git a/api/handler/short_link.go b/api/handler/short_link.go index 0e87a19..1eb865a 100644 --- a/api/handler/short_link.go +++ b/api/handler/short_link.go @@ -135,3 +135,29 @@ func GetRawConfHandler(c *gin.Context) { // 返回响应内容 c.String(http.StatusOK, string(all)) } + +func GetRawConfUriHandler(c *gin.Context) { + // 获取动态路由参数 + hash := c.Query("hash") + password := c.Query("password") + + if strings.TrimSpace(hash) == "" { + c.String(http.StatusBadRequest, "参数错误") + return + } + + // 查询数据库中的短链接 + shortLink, err := database.FindShortLinkByHash(hash) + if err != nil { + c.String(http.StatusNotFound, "未找到短链接或密码错误") + return + } + + // 校验密码 + if shortLink.Password != "" && shortLink.Password != password { + c.String(http.StatusNotFound, "未找到短链接或密码错误") + return + } + + c.String(http.StatusOK, shortLink.Url) +} diff --git a/api/route.go b/api/route.go index 256ea9d..0861945 100644 --- a/api/route.go +++ b/api/route.go @@ -44,4 +44,5 @@ func SetRoute(r *gin.Engine) { r.GET("/s/:hash", handler.GetRawConfHandler) r.POST("/short", handler.GenerateLinkHandler) r.PUT("/short", handler.UpdateLinkHandler) + r.GET("/short", handler.GetRawConfUriHandler) }