Categorygithub.com/zhuyst/shorturl-service
repositorypackage
1.1.0
Repository: https://github.com/zhuyst/shorturl-service.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

短URL生成服务

短URL生成服务,使用snowflake生成key,使用redis存储映射。

项目主要依赖

直接使用

  1. 生成一个短URL:
curl -X POST \
  https://d.zhuyst.cc/new \
  -d 'url=https://github.com/zhuyst/shorturl-service'
  
{"code":200,"message":"OK","url":"https://d.zhuyst.cc/4dUaeq5"}
  1. 在浏览器中输入d.zhuyst.cc/4dUaeq5

在原有服务添加短URL服务

  1. 安装服务
go get -u github.com/zhuyst/shorturl-service
  1. 引入服务
import "github.com/zhuyst/shorturl-service"
  1. 使用go-redisGin启动服务
package main

import (
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis"
	"github.com/zhuyst/shorturl-service"
	"log"
	"regexp"
)

func main() {
	redisClient := redis.NewClient(&redis.Options{
		Addr: "redis:6379",
	})

	if err := redisClient.Ping().Err(); err != nil {
		log.Fatalf("redisClient ping FAIL: %s", err.Error())
		return
	}

	r := gin.Default()
	if err := shorturl_service.InitRouter(r, redisClient, &shorturl_service.Option{
		Domain:        "d.zhuyst.cc",
		ServiceUri:    "/",
		LongUrlRegexp: regexp.MustCompile("https://.*"),
	}); err != nil {
		log.Fatalf("shorturl_service init FAIL: %s", err.Error())
	}

	if err := r.Run(":8080"); err != nil {
		log.Fatalf("gin init FAIL: %s", err.Error())
	}
}

从零搭建一个短URL服务

  1. clone项目
git clone https://github.com/zhuyst/shorturl-service.git
  1. 修改example\main.goOption相关配置
if err := shorturl_service.InitRouter(r, redisClient, &shorturl_service.Option{
		Domain:        "d.zhuyst.cc",
		ServiceUri:    "/",
		LongUrlRegexp: regexp.MustCompile("https://.*"),
	}); err != nil {
	log.Fatalf("shorturl_service init FAIL: %s", err.Error())
}
  1. 使用docker-compose启动项目
docker-compose up -d