Skip to content

a short link module by go(go实现的一个短链服务模块)

License

Notifications You must be signed in to change notification settings

Uyouii/shortlink

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ShortLink

a short link go module.

usage

init

import (
	"sync"

	"github.com/uyouii/shortlink"
	shortlink_mysql_db "github.com/uyouii/shortlink/dao/mysql_db"
)

var ShortLinkGeneragor *shortlink.ShortLinkGenerator
var once sync.Once

func Init() {
	once.Do(func() {
		ShortLinkGeneragor = shortlink.GetShortLinkGenerator().WithMysql(&shortlink_mysql_db.MysqlDbConfig{
			User:           "root",
			DatabaseName:   "short_link_db",
			Password:       "asdfgh",
			PartitionCount: 16,
		})
	})
}

gen short link

	shortLink, err = global.ShortLinkGeneragor.GenShortLink(ctx, req.Link)

redirect with gin

func getRawLink(ctx context.Context, shortLinkPath string) (string, error) {
	infof, errorf := common.GetLogFuns(ctx)

	shortLinkInfo, err := global.ShortLinkGeneragor.GetByShortLinkPath(ctx, shortLinkPath)
	if err != nil {
		errorf("get by short link failed, err: %v, shortlinkpath: %v", err, shortLinkPath)
		return "", common.GetErrorWithMsg(common.ERROR_SYSTEM_ERROR, fmt.Sprintf("%v", err))
	}

	infof("short link info: %+v", shortLinkInfo)

	return shortLinkInfo.RawLink, nil
}

func RedirectShortLink(c *gin.Context) {
	ctx := context.Background()
	infof, errorf := common.GetLogFuns(ctx)

	shortLinkPath := c.Param("path")
	if shortLinkPath == "" {
		errorf("invalid path")
		SetError(c, common.GetError(common.ERROR_INVALID_PARAMETERS))
		return
	}
	infof("short link path: %v", shortLinkPath)

	shortLinkPath = strings.TrimPrefix(shortLinkPath, "/")

	rawLink, err := getRawLink(ctx, shortLinkPath)
	if err != nil {
		SetError(c, err)
		return
	}

	infof("rediret to %v", rawLink)

	c.Redirect(http.StatusFound, rawLink)
}

Features

  • mysql db
    • support table partition
    • support temperory short link
    • reuse expired shortlink path
  • cache
    • redis cache
    • local cache

About

a short link module by go(go实现的一个短链服务模块)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages