-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
205 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ dist | |
subs | ||
test | ||
logs | ||
dist/ | ||
sub2clash.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package controller | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"github.com/gin-gonic/gin" | ||
"sub2clash/model" | ||
"sub2clash/utils/database" | ||
"sub2clash/validator" | ||
"time" | ||
) | ||
|
||
func ShortLinkGenHandler(c *gin.Context) { | ||
// 从请求中获取参数 | ||
var params validator.ShortLinkGenValidator | ||
if err := c.ShouldBind(¶ms); err != nil { | ||
c.String(400, "参数错误: "+err.Error()) | ||
} | ||
// 生成短链接 | ||
//hash := utils.RandomString(6) | ||
shortLink := sha256.Sum224([]byte(params.Url)) | ||
hash := hex.EncodeToString(shortLink[:]) | ||
// 存入数据库 | ||
database.DB.FirstOrCreate( | ||
&model.ShortLink{ | ||
Hash: hash, | ||
Url: params.Url, | ||
LastRequestTime: -1, | ||
}, | ||
) | ||
// 返回短链接 | ||
c.String(200, hash) | ||
} | ||
|
||
func ShortLinkGetHandler(c *gin.Context) { | ||
// 获取动态路由 | ||
hash := c.Param("hash") | ||
// 查询数据库 | ||
var shortLink model.ShortLink | ||
result := database.DB.Where("hash = ?", hash).First(&shortLink) | ||
// 更新最后访问时间 | ||
shortLink.LastRequestTime = time.Now().Unix() | ||
database.DB.Save(&shortLink) | ||
// 重定向 | ||
if result.Error != nil { | ||
c.String(404, "未找到短链接") | ||
return | ||
} | ||
c.Redirect(302, "/"+shortLink.Url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package model | ||
|
||
type ShortLink struct { | ||
Hash string `gorm:"primary_key"` | ||
Url string | ||
LastRequestTime int64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package database | ||
|
||
import ( | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"sub2clash/model" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func ConnectDB() error { | ||
db, err := gorm.Open(sqlite.Open("sub2clash.db"), &gorm.Config{}) | ||
if err != nil { | ||
return err | ||
} | ||
DB = db | ||
err = db.AutoMigrate(&model.ShortLink{}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package utils | ||
|
||
import "math/rand" | ||
|
||
func RandomString(length int) string { | ||
// 生成随机字符串 | ||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
var result []byte | ||
for i := 0; i < length; i++ { | ||
result = append(result, charset[rand.Intn(len(charset))]) | ||
} | ||
return string(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package validator | ||
|
||
type ShortLinkGenValidator struct { | ||
Url string `form:"url" binding:"required"` | ||
} | ||
|
||
type ShortLinkGetValidator struct { | ||
Hash string `form:"hash" binding:"required"` | ||
} |
Oops, something went wrong.