Content-Length: 317412 | pFad | http://github.com/zhuyst/shorturl-service

99 GitHub - zhuyst/shorturl-service: 短URL生成服务,使用snowflake生成key,使用redis存储映射
Skip to content

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

License

Notifications You must be signed in to change notification settings

zhuyst/shorturl-service

Repository files navigation

短URL生成服务

Build Status Go Report Card codecov

短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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/zhuyst/shorturl-service

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy