Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"calc.go",
"const.go",
"shorturl.go",
],
importpath = "go-common/app/interface/main/shorturl/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["//library/time:go_default_library"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,49 @@
package model
import (
"crypto/md5"
"encoding/hex"
"strconv"
)
// TODO move to model
const (
shortUrlLength = 6
)
var (
chars = [62]string{
"a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x",
"y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D",
"E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z",
}
)
// generate short url from long url
func Generate(long string) [4]string {
var resUrl [4]string
h := md5.New()
h.Write([]byte(long))
hexstr := hex.EncodeToString(h.Sum(nil))
for i := 0; i < 4; i++ {
start := i * 8
end := start + 8
s := hexstr[start:end]
hexInt, _ := strconv.ParseInt(s, 16, 64)
hexInt = 0x3FFFFFFF & hexInt
var out string = ""
for n := 0; n < shortUrlLength; n++ {
index := 0x0000003D & hexInt
out += chars[index]
hexInt = hexInt >> 5
}
resUrl[i] = out
}
return resUrl
}

View File

@@ -0,0 +1,10 @@
package model
const (
// EnvPro is pro.
EnvPro = "pro"
// EnvTest is env.
EnvTest = "test"
// EnvDev is env.
EnvDev = "dev"
)

View File

@@ -0,0 +1,31 @@
package model
import xtime "go-common/library/time"
const (
StateNormal = 0
StateDelted = 1
)
type ShortUrl struct {
ID int64 `json:"id"`
Mid int64 `json:"mid"`
Short string `json:"short"`
Long string `json:"long"`
State int8 `json:"state"`
CTime xtime.Time `json:"-"`
MTime xtime.Time `json:"-"`
CreateTime string `json:"ctime"`
}
type Param struct {
ID int64 `form:"id"`
Mid int64 `form:"mid"`
Uri string `form:"url"`
Pn string `form:"pn"`
Ps string `form:"ps"`
}
func (s *ShortUrl) FormatDate() {
s.CreateTime = s.CTime.Time().Format("2006-01-02 15:04:05")
}