Create & Init Project...
This commit is contained in:
50
app/interface/main/app-resource/service/static/BUILD
Normal file
50
app/interface/main/app-resource/service/static/BUILD
Normal file
@ -0,0 +1,50 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_test",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["static_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
rundir = ".",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//app/interface/main/app-resource/conf:go_default_library",
|
||||
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["static.go"],
|
||||
importpath = "go-common/app/interface/main/app-resource/service/static",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/interface/main/app-resource/conf:go_default_library",
|
||||
"//app/interface/main/app-resource/dao/egg:go_default_library",
|
||||
"//app/interface/main/app-resource/model:go_default_library",
|
||||
"//app/interface/main/app-resource/model/static:go_default_library",
|
||||
"//library/ecode:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//vendor/github.com/dgryski/go-farm: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"],
|
||||
)
|
94
app/interface/main/app-resource/service/static/static.go
Normal file
94
app/interface/main/app-resource/service/static/static.go
Normal file
@ -0,0 +1,94 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"go-common/app/interface/main/app-resource/conf"
|
||||
eggdao "go-common/app/interface/main/app-resource/dao/egg"
|
||||
"go-common/app/interface/main/app-resource/model"
|
||||
"go-common/app/interface/main/app-resource/model/static"
|
||||
"go-common/library/ecode"
|
||||
"go-common/library/log"
|
||||
|
||||
farm "github.com/dgryski/go-farm"
|
||||
)
|
||||
|
||||
const (
|
||||
_initVersion = "static_version"
|
||||
)
|
||||
|
||||
var (
|
||||
_emptyStatics = []*static.Static{}
|
||||
)
|
||||
|
||||
// Service static service.
|
||||
type Service struct {
|
||||
dao *eggdao.Dao
|
||||
tick time.Duration
|
||||
cache map[int8][]*static.Static
|
||||
staticPath string
|
||||
}
|
||||
|
||||
// New new a static service.
|
||||
func New(c *conf.Config) (s *Service) {
|
||||
s = &Service{
|
||||
dao: eggdao.New(c),
|
||||
tick: time.Duration(c.Tick),
|
||||
cache: map[int8][]*static.Static{},
|
||||
staticPath: c.StaticJSONFile,
|
||||
}
|
||||
now := time.Now()
|
||||
s.loadCache(now)
|
||||
go s.loadCachepro()
|
||||
return
|
||||
}
|
||||
|
||||
// Static return statics
|
||||
func (s *Service) Static(plat int8, build int, ver string, now time.Time) (res []*static.Static, version string, err error) {
|
||||
var (
|
||||
tmps = s.cache[plat]
|
||||
)
|
||||
for _, tmp := range tmps {
|
||||
if model.InvalidBuild(build, tmp.Build, tmp.Condition) {
|
||||
continue
|
||||
}
|
||||
res = append(res, tmp)
|
||||
}
|
||||
if len(res) == 0 {
|
||||
res = _emptyStatics
|
||||
}
|
||||
if version = s.hash(res); version == ver {
|
||||
err = ecode.NotModified
|
||||
res = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Service) hash(v []*static.Static) string {
|
||||
bs, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
log.Error("json.Marshal error(%v)", err)
|
||||
return _initVersion
|
||||
}
|
||||
return strconv.FormatUint(farm.Hash64(bs), 10)
|
||||
}
|
||||
|
||||
// loadCache update egg
|
||||
func (s *Service) loadCache(now time.Time) {
|
||||
tmp, err := s.dao.Egg(context.TODO(), now)
|
||||
if err != nil {
|
||||
log.Error("s.dao.Egg error(%v)", err)
|
||||
return
|
||||
}
|
||||
s.cache = tmp
|
||||
}
|
||||
|
||||
func (s *Service) loadCachepro() {
|
||||
for {
|
||||
time.Sleep(s.tick)
|
||||
s.loadCache(time.Now())
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go-common/app/interface/main/app-resource/conf"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
var (
|
||||
s *Service
|
||||
)
|
||||
|
||||
func WithService(f func(s *Service)) func() {
|
||||
return func() {
|
||||
f(s)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
dir, _ := filepath.Abs("../../cmd/app-resource-test.toml")
|
||||
flag.Set("conf", dir)
|
||||
conf.Init()
|
||||
s = New(conf.Conf)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
// go test -conf="../../cmd/app-resource-test.toml" -v -test.run TestStatic
|
||||
func TestStatic(t *testing.T) {
|
||||
Convey("get static data", t, WithService(func(s *Service) {
|
||||
res, ver, err := s.Static(1, 22222, "", time.Now())
|
||||
result, err := json.Marshal(res)
|
||||
fmt.Printf("test static (%v) \n", string(result))
|
||||
So(res, ShouldNotBeEmpty)
|
||||
So(ver, ShouldNotBeEmpty)
|
||||
So(err, ShouldBeNil)
|
||||
}))
|
||||
}
|
Reference in New Issue
Block a user