Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"service_test.go",
"short_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/shorturl/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"service.go",
"short.go",
],
importpath = "go-common/app/interface/main/shorturl/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/shorturl/conf:go_default_library",
"//app/interface/main/shorturl/dao:go_default_library",
"//app/interface/main/shorturl/model:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//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,29 @@
package service
import (
"context"
"go-common/app/interface/main/shorturl/conf"
shortdao "go-common/app/interface/main/shorturl/dao"
"go-common/library/log"
)
// Service service struct
type Service struct {
shortd *shortdao.Dao
}
// New new service
func New(c *conf.Config) (s *Service) {
s = &Service{
shortd: shortdao.New(c),
}
return
}
// Ping ping service.
func (s *Service) Ping(c context.Context) (err error) {
if err = s.shortd.Ping(c); err != nil {
log.Error("s.dao.Ping error(%v)", err)
}
return
}

View File

@@ -0,0 +1,28 @@
package service
import (
"flag"
"path/filepath"
"time"
"go-common/app/interface/main/shorturl/conf"
)
var (
s *Service
)
func init() {
dir, _ := filepath.Abs("../cmd/shorturl-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
time.Sleep(time.Second)
}
func WithService(f func(s *Service)) func() {
return func() {
f(s)
}
}

View File

@@ -0,0 +1,145 @@
package service
import (
"context"
"time"
"go-common/app/interface/main/shorturl/model"
"go-common/library/ecode"
"go-common/library/log"
xtime "go-common/library/time"
)
// ShortCache get short by cache
func (s *Service) ShortCache(c context.Context, short string) (su *model.ShortUrl, err error) {
if su, err = s.shortd.Cache(c, short); err != nil {
log.Error("s.shrtd.Cache(%s) error(%v)", short, err)
return
}
if su != nil {
return
}
if su, err = s.shortd.Short(c, short); err != nil {
log.Error("s.shortd.Short(%s) error(%v)", short, err)
return
}
// add cache
if su == nil {
s.shortd.AddCache(func() {
s.shortd.SetEmptyCache(context.TODO(), short)
})
return
}
s.shortd.AddCache(func() {
s.shortd.SetCache(context.TODO(), su)
})
return
}
// ShortByID model.ShortUrl by short id
func (s *Service) ShortByID(c context.Context, id int64) (su *model.ShortUrl, err error) {
su, err = s.shortd.ShortbyID(c, id)
if err != nil || su == nil {
log.Error("ShortByID.Get(%d) error(%v)", id, err)
err = ecode.ShortURLNotFound
return
}
return
}
// Add new url to db
func (s *Service) Add(c context.Context, mid int64, long string) (short string, err error) {
su := &model.ShortUrl{
Long: long,
Mid: mid,
State: model.StateNormal,
CTime: xtime.Time(time.Now().Unix()),
}
ss := model.Generate(long) // detemine no repeat
for _, str := range ss {
su.Short = str
if su.ID, err = s.shortd.InShort(c, su); err != nil {
log.Error("s.shortd.InShort error(%v)", err)
err = ecode.ServerErr
return
}
if su.ID == 0 {
var shortRes *model.ShortUrl
// already exist
if shortRes, err = s.shortd.Short(c, str); err == nil && shortRes != nil {
if shortRes.Long == long {
if _, err = s.shortd.UpdateState(c, shortRes.ID, mid, model.StateNormal); err != nil {
break
}
short = shortRes.Short
break
} else {
continue
}
}
} else {
short = str
break
}
}
if short == "" {
err = ecode.ShortURLNotFound
return
}
s.shortd.AddCache(func() {
var su, err = s.shortd.Short(context.TODO(), short)
if err == nil && su != nil {
s.shortd.SetCache(context.TODO(), su)
}
})
return
}
// ShortUpdate model.ShortUpdate
func (s *Service) ShortUpdate(c context.Context, id, mid int64, long string) (err error) {
if _, err = s.shortd.ShortUp(c, id, mid, long); err != nil {
log.Error("s.shortd.ShortUp error(%v)", err)
return
}
s.shortd.AddCache(func() {
var short *model.ShortUrl
if short, err = s.shortd.ShortbyID(context.TODO(), id); err != nil {
log.Error("s.shortd.ShortByID(%d) error(%v)", id, err)
return
}
s.shortd.SetCache(context.TODO(), short)
})
return
}
// ShortDel model.ShortDel
func (s *Service) ShortDel(c context.Context, id, mid int64, now time.Time) (err error) {
if _, err = s.shortd.UpdateState(c, id, mid, model.StateDelted); err != nil {
log.Error("s.shortd.ShortDel error(%v)", err)
}
s.shortd.AddCache(func() {
var short *model.ShortUrl
if short, err = s.shortd.ShortbyID(context.TODO(), id); err != nil {
log.Error("s.shortd.ShortByID(%d) error(%v)", id, err)
return
}
s.shortd.SetCache(context.TODO(), short)
})
return
}
// ShortCount model.ShortCount count
func (s *Service) ShortCount(c context.Context, mid int64, long string) (count int, err error) {
if count, err = s.shortd.ShortCount(c, mid, long); err != nil {
log.Error("s.shortd.ShortState error(%v)", err)
}
return
}
// ShortLimit get short_url list
func (s *Service) ShortLimit(c context.Context, pn, ps int, mid int64, long string) (res []*model.ShortUrl, err error) {
if res, err = s.shortd.ShortLimit(c, (pn-1)*ps, ps, mid, long); err != nil {
log.Error("s.shortd.ShortLimit error(%v)", err)
}
return
}

View File

@@ -0,0 +1,58 @@
package service
import (
"testing"
"time"
"context"
. "github.com/smartystreets/goconvey/convey"
)
func TestService_ShortCache(t *testing.T) {
Convey("ShortCache", t, WithService(func(s *Service) {
_, err := s.ShortCache(context.TODO(), "http://b23.tv/EbUzmu")
So(err, ShouldBeNil)
}))
}
func TestService_ShortByID(t *testing.T) {
Convey("ShortByID", t, WithService(func(s *Service) {
_, err := s.ShortByID(context.TODO(), 1)
So(err, ShouldBeNil)
}))
}
func TestService_Add(t *testing.T) {
Convey("Add", t, WithService(func(s *Service) {
_, err := s.Add(context.TODO(), 279, "http://www.baidu.com")
So(err, ShouldBeNil)
}))
}
func TestService_ShortUpdate(t *testing.T) {
Convey("ShortUpdate", t, WithService(func(s *Service) {
err := s.ShortUpdate(context.TODO(), 1, 279, "http://www.baidu.com")
So(err, ShouldBeNil)
}))
}
func TestService_ShortDel(t *testing.T) {
Convey("ShortDel", t, WithService(func(s *Service) {
err := s.ShortDel(context.TODO(), 1, 279, time.Now())
So(err, ShouldBeNil)
}))
}
func TestService_ShortCount(t *testing.T) {
Convey("ShortCount", t, WithService(func(s *Service) {
_, err := s.ShortCount(context.TODO(), 279, "http://www.baidu.com")
So(err, ShouldBeNil)
}))
}
func TestService_ShortLimit(t *testing.T) {
Convey("ShortLimit", t, WithService(func(s *Service) {
_, err := s.ShortLimit(context.TODO(), 1, 20, 279, "http://www.baidu.com")
So(err, ShouldBeNil)
}))
}