short

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2020 License: MIT Imports: 13 Imported by: 0

README

short

Use murmur hash function.

URL shortening service.

Usage

example

package main

import (
	"fmt"
	"github.com/golang/glog"
	"github.com/googege/short"
	"os"
)

var (
	path    = "https://t.cn/"
	longURL = "https://github.com/googege/GOFamily/blob/master/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/%E7%AE%97%E6%B3%95/%E7%AE%97%E6%B3%95%E9%A2%98/leetcode/1.md"
)

func main() {
	db := short.NewMapDB()
	// add db engine to short.
	s := short.NewShort(db)
	//
	shortURL, err := s.ShortAdd(longURL)
	if err != nil {
		glog.Error(err)
	} else {
		fmt.Println(shortURL)
	}
	//
	longURL, err = s.ShortFind(path + shortURL)
	// if http
	//http.Redirect(nil,nil,longURL,302
	//)
	if err != nil {
		glog.Error(err)
	} else {
		fmt.Println("longURL:", longURL)
	}
	//
	shortURL, err = s.ShortFind("a")
	if err != nil {
		glog.Error(err)
	} else {
		fmt.Println("short: ", shortURL)
	}
	//
	file, err := os.Getwd()
	if err != nil {
		glog.Error(err)
	}
	if err = s.SetQR(path, 256, file+"/text.png"); err != nil {
		glog.Error(err)
	}
}


HERE

items description
Sponsor me p
aliyun vps aliyun vps,China's largest cloud computing manufacturer, the world's top 5 cloud computing service providers
VPS vps fast and cheap just 2-3.75$/mo
WeChat public account p
My wechat (Please mark:form Github) p
My bilibili b
MyYouTube YouTube

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	Add(v *Node) (shortURL string, err error) // add v to db
	Delete(shortURL string)                   // delete v
	Change(vi *Node, r string) error          // change v to vi
	Find(shortURL string) (string, error)     // find v from db.
}

type MapDB

type MapDB struct {
	Value map[string]string
}

func NewMapDB

func NewMapDB() *MapDB

func (*MapDB) Add

func (m *MapDB) Add(v *Node) (string, error)

func (*MapDB) Change

func (m *MapDB) Change(n *Node, positionShortURL string) error

change the longurl's shorturl to positionShortURL.

func (*MapDB) Delete

func (m *MapDB) Delete(shortURL string)

func (*MapDB) Find

func (m *MapDB) Find(shortURL string) (string, error)

find the short url

type MySqlDB

type MySqlDB struct {
	// contains filtered or unexported fields
}

func NewMySqlDB

func NewMySqlDB(dataSourName string) *MySqlDB

func (*MySqlDB) Add

func (m *MySqlDB) Add(v *Node) (shortURL string, err error)

add

func (*MySqlDB) Change

func (m *MySqlDB) Change(vi *Node, r string) error

func (*MySqlDB) Delete

func (m *MySqlDB) Delete(shortURL string)

func (*MySqlDB) Find

func (m *MySqlDB) Find(shortURL string) (string, error)

type MySqlDBValue

type MySqlDBValue struct {
	ShortURL string `mysql:"short_url"`
	LongURL  string `mysql:"short_url"`
}

type Node

type Node struct {
	LongValue  string // long string
	ShortValue string // short string
	// contains filtered or unexported fields
}

func NewShort

func NewShort(db DB) *Node

func (*Node) SetQR

func (n *Node) SetQR(url string, size int, file string) (err error)

set shortURL to a QR CODE

func (*Node) ShortAdd

func (n *Node) ShortAdd(longURL string) (string, error)

short the long url to a small one in hash method.

func (*Node) ShortChange

func (n *Node) ShortChange(newShortURL string) error

change a new short URL.

func (*Node) ShortDelete

func (n *Node) ShortDelete(shortURL string)

delete the shortURL,and it's long url

func (*Node) ShortFind

func (n *Node) ShortFind(shortURL string) (string, error)

find the longurl's shortURL.

type RedisDB

type RedisDB struct {
	// contains filtered or unexported fields
}

func NewRedisDB

func NewRedisDB(options *redis.Options) *RedisDB

new redisdb

func (*RedisDB) Add

func (rd *RedisDB) Add(v *Node) (shortURL string, err error)

func (*RedisDB) Change

func (rd *RedisDB) Change(vi *Node, r string) error

func (*RedisDB) Delete

func (rd *RedisDB) Delete(shortURL string)

func (*RedisDB) Find

func (rd *RedisDB) Find(shortURL string) (string, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL