Upgrade golang
This commit is contained in:
parent
f1cc1cf099
commit
26a3c17aa5
2
.github/workflows/go.yml
vendored
2
.github/workflows/go.yml
vendored
@ -17,6 +17,8 @@ jobs:
|
|||||||
id: go
|
id: go
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
- name: Install dependencies
|
||||||
|
run: apt-get install gcc-aarch64-linux-gnu
|
||||||
- name: Build
|
- name: Build
|
||||||
run: /bin/sh -c "chmod +x release.sh && bash release.sh"
|
run: /bin/sh -c "chmod +x release.sh && bash release.sh"
|
||||||
- name: Upload
|
- name: Upload
|
||||||
|
|||||||
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module github.com/CareyWang/MyUrls
|
module github.com/CareyWang/MyUrls
|
||||||
|
|
||||||
go 1.13
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.5.0
|
github.com/gin-gonic/gin v1.5.0
|
||||||
|
|||||||
179
main.go
179
main.go
@ -4,15 +4,16 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/gomodule/redigo/redis"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/gomodule/redigo/redis"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
@ -35,7 +36,7 @@ type redisPoolConf struct {
|
|||||||
const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
const defaultPort int = 8002
|
const defaultPort int = 8002
|
||||||
const defaultExpire = 90
|
const defaultExpire = 180
|
||||||
const defaultRedisConfig = "127.0.0.1:6379"
|
const defaultRedisConfig = "127.0.0.1:6379"
|
||||||
|
|
||||||
const defaultLockPrefix = "myurls:lock:"
|
const defaultLockPrefix = "myurls:lock:"
|
||||||
@ -58,7 +59,7 @@ func main() {
|
|||||||
|
|
||||||
port := flag.Int("port", defaultPort, "服务端口")
|
port := flag.Int("port", defaultPort, "服务端口")
|
||||||
domain := flag.String("domain", "", "短链接域名,必填项")
|
domain := flag.String("domain", "", "短链接域名,必填项")
|
||||||
ttl := flag.Int("ttl", defaultExpire, "短链接有效期,单位(天),默认90天。")
|
ttl := flag.Int("ttl", defaultExpire, "短链接有效期,单位(天),默认180天。")
|
||||||
conn := flag.String("conn", defaultRedisConfig, "Redis连接,格式: host:port")
|
conn := flag.String("conn", defaultRedisConfig, "Redis连接,格式: host:port")
|
||||||
passwd := flag.String("passwd", "", "Redis连接密码")
|
passwd := flag.String("passwd", "", "Redis连接密码")
|
||||||
https := flag.Int("https", 1, "是否返回 https 短链接")
|
https := flag.Int("https", 1, "是否返回 https 短链接")
|
||||||
@ -151,6 +152,90 @@ func main() {
|
|||||||
router.Run(fmt.Sprintf(":%d", *port))
|
router.Run(fmt.Sprintf(":%d", *port))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 短链接转长链接
|
||||||
|
func shortToLong(shortKey string) string {
|
||||||
|
redisClient = redisPool.Get()
|
||||||
|
defer redisClient.Close()
|
||||||
|
|
||||||
|
longUrl, _ := redis.String(redisClient.Do("get", shortKey))
|
||||||
|
|
||||||
|
// 获取到长链接后,续命1天。每天仅允许续命1次。
|
||||||
|
if longUrl != "" {
|
||||||
|
renew(shortKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
return longUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
// 长链接转短链接
|
||||||
|
func longToShort(longUrl string, ttl int) string {
|
||||||
|
redisClient = redisPool.Get()
|
||||||
|
defer redisClient.Close()
|
||||||
|
|
||||||
|
// 是否生成过该长链接对应短链接
|
||||||
|
_existsKey, _ := redis.String(redisClient.Do("get", longUrl))
|
||||||
|
if _existsKey != "" {
|
||||||
|
_, _ = redisClient.Do("expire", _existsKey, ttl)
|
||||||
|
|
||||||
|
log.Println("Hit cache: " + _existsKey)
|
||||||
|
return _existsKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重试三次
|
||||||
|
var shortKey string
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
shortKey = generate(7)
|
||||||
|
|
||||||
|
_existsLongUrl, _ := redis.String(redisClient.Do("get", shortKey))
|
||||||
|
if _existsLongUrl == "" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if shortKey != "" {
|
||||||
|
_, _ = redisClient.Do("mset", shortKey, longUrl, longUrl, shortKey)
|
||||||
|
|
||||||
|
_, _ = redisClient.Do("expire", shortKey, ttl)
|
||||||
|
_, _ = redisClient.Do("expire", longUrl, secondsPerDay)
|
||||||
|
}
|
||||||
|
|
||||||
|
return shortKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// 续命
|
||||||
|
func renew(shortKey string) {
|
||||||
|
redisClient = redisPool.Get()
|
||||||
|
defer redisClient.Close()
|
||||||
|
|
||||||
|
// 加锁
|
||||||
|
lockKey := defaultLockPrefix + shortKey
|
||||||
|
lock, _ := redis.Int(redisClient.Do("setnx", lockKey, 1))
|
||||||
|
if lock == 1 {
|
||||||
|
// 设置锁过期时间
|
||||||
|
_, _ = redisClient.Do("expire", lockKey, defaultRenewal*secondsPerDay)
|
||||||
|
|
||||||
|
// 续命
|
||||||
|
ttl, _ := redis.Int(redisClient.Do("ttl", shortKey))
|
||||||
|
if ttl != -1 {
|
||||||
|
_, _ = redisClient.Do("expire", shortKey, ttl+defaultRenewal*secondsPerDay)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 产生一个63位随机整数
|
||||||
|
func generate(bits int) string {
|
||||||
|
b := make([]byte, bits)
|
||||||
|
|
||||||
|
currentTime := time.Now().UnixNano()
|
||||||
|
rand.Seed(currentTime)
|
||||||
|
|
||||||
|
for i := range b {
|
||||||
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义 logger
|
||||||
func Logger() *logrus.Logger {
|
func Logger() *logrus.Logger {
|
||||||
logFilePath := ""
|
logFilePath := ""
|
||||||
if dir, err := os.Getwd(); err == nil {
|
if dir, err := os.Getwd(); err == nil {
|
||||||
@ -191,6 +276,7 @@ func Logger() *logrus.Logger {
|
|||||||
return logger
|
return logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件日志
|
||||||
func LoggerToFile() gin.HandlerFunc {
|
func LoggerToFile() gin.HandlerFunc {
|
||||||
logger := Logger()
|
logger := Logger()
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
@ -242,69 +328,7 @@ func LoggerToFile() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 短链接转长链接
|
// redis 连接池
|
||||||
func shortToLong(shortKey string) string {
|
|
||||||
redisClient = redisPool.Get()
|
|
||||||
defer redisClient.Close()
|
|
||||||
|
|
||||||
longUrl, _ := redis.String(redisClient.Do("get", shortKey))
|
|
||||||
|
|
||||||
// 获取到长链接后,续命1天。每天仅允许续命1次。
|
|
||||||
if longUrl != "" {
|
|
||||||
renew(shortKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
return longUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
// 长链接转短链接
|
|
||||||
func longToShort(longUrl string, ttl int) string {
|
|
||||||
redisClient = redisPool.Get()
|
|
||||||
defer redisClient.Close()
|
|
||||||
|
|
||||||
// 是否生成过该长链接对应短链接
|
|
||||||
_existsKey, _ := redis.String(redisClient.Do("get", longUrl))
|
|
||||||
if _existsKey != "" {
|
|
||||||
_, _ = redisClient.Do("expire", _existsKey, ttl)
|
|
||||||
|
|
||||||
log.Println("Hit cache: " + _existsKey)
|
|
||||||
return _existsKey
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重试三次
|
|
||||||
var shortKey string
|
|
||||||
for i := 0; i < 3; i++ {
|
|
||||||
shortKey = generate(7)
|
|
||||||
|
|
||||||
_existsLongUrl, _ := redis.String(redisClient.Do("get", shortKey))
|
|
||||||
if _existsLongUrl == "" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if shortKey != "" {
|
|
||||||
_, _ = redisClient.Do("mset", shortKey, longUrl, longUrl, shortKey)
|
|
||||||
|
|
||||||
_, _ = redisClient.Do("expire", shortKey, ttl)
|
|
||||||
_, _ = redisClient.Do("expire", longUrl, secondsPerDay)
|
|
||||||
}
|
|
||||||
|
|
||||||
return shortKey
|
|
||||||
}
|
|
||||||
|
|
||||||
// 产生一个63位随机整数
|
|
||||||
func generate(bits int) string {
|
|
||||||
b := make([]byte, bits)
|
|
||||||
|
|
||||||
currentTime := time.Now().UnixNano()
|
|
||||||
rand.Seed(currentTime)
|
|
||||||
|
|
||||||
for i := range b {
|
|
||||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
|
||||||
}
|
|
||||||
return string(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func initRedisPool() {
|
func initRedisPool() {
|
||||||
// 建立连接池
|
// 建立连接池
|
||||||
redisPool = &redis.Pool{
|
redisPool = &redis.Pool{
|
||||||
@ -326,22 +350,3 @@ func initRedisPool() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func renew(shortKey string) {
|
|
||||||
redisClient = redisPool.Get()
|
|
||||||
defer redisClient.Close()
|
|
||||||
|
|
||||||
// 加锁
|
|
||||||
lockKey := defaultLockPrefix + shortKey
|
|
||||||
lock, _ := redis.Int(redisClient.Do("setnx", lockKey, 1))
|
|
||||||
if lock == 1 {
|
|
||||||
// 设置锁过期时间
|
|
||||||
_, _ = redisClient.Do("expire", lockKey, defaultRenewal*secondsPerDay)
|
|
||||||
|
|
||||||
// 续命
|
|
||||||
ttl, _ := redis.Int(redisClient.Do("ttl", shortKey))
|
|
||||||
if ttl != -1 {
|
|
||||||
_, _ = redisClient.Do("expire", shortKey, ttl+defaultRenewal*secondsPerDay)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user