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

17
app/common/BUILD Normal file
View File

@@ -0,0 +1,17 @@
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/common/live:all-srcs",
"//app/common/openplatform:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

5
app/common/OWNERS Normal file
View File

@@ -0,0 +1,5 @@
# See the OWNERS docs at https://go.k8s.io/owners
labels:
- common
- new-project

17
app/common/live/BUILD Normal file
View File

@@ -0,0 +1,17 @@
package(default_visibility = ["//visibility:public"])
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/common/live/library:all-srcs",
],
tags = ["automanaged"],
)

View File

@@ -0,0 +1 @@
# changelog

View File

@@ -0,0 +1,13 @@
# Owner
liuzhen
liugang
kuangxibin
zhaohailin
# Author
# Reviewer
liuzhen
liugang
kuangxibin
zhaohailin

16
app/common/live/OWNERS Normal file
View File

@@ -0,0 +1,16 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- kuangxibin
- liugang
- liuzhen
- zhaohailin
labels:
- common
- library
- live
reviewers:
- kuangxibin
- liugang
- liuzhen
- zhaohailin

View File

@@ -0,0 +1,2 @@
# live-common

View File

@@ -0,0 +1,25 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/common/live/library/lancer:all-srcs",
"//app/common/live/library/lrucache:all-srcs",
"//app/common/live/library/mengde:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,29 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["lancer.go"],
importpath = "go-common/app/common/live/library/lancer",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["//library/log: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,198 @@
package lancer
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
"runtime"
"strconv"
"strings"
"sync"
"time"
"go-common/library/log"
)
const (
kLancerMinimumLength = 6
kLancerLengthPartBegin = 2
kLancerLengthPartEnd = 6
)
type LancerLogStream struct {
conn net.Conn
addr string
pool sync.Pool
dataChannel chan *LancerData
quit chan struct{}
wg sync.WaitGroup
splitter string
replacer string
timeout time.Duration
}
type LancerData struct {
bytes.Buffer
logid string
isAppend bool
lancer *LancerLogStream
}
func NewLancerLogStream(address string, capacity int, timeout time.Duration) *LancerLogStream {
c, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
log.Error("[Lancer]Dial lancer %s error:%+v", address, err)
c = nil
}
lancer := &LancerLogStream{
conn: c,
addr: address,
pool: sync.Pool{
New: func() interface{} {
return new(LancerData)
},
},
dataChannel: make(chan *LancerData, capacity),
quit: make(chan struct{}),
splitter: "\u0001",
replacer: "|",
timeout: timeout,
}
lancer.wg.Add(1)
go lancer.processor()
return lancer
}
func (lancer *LancerLogStream) Close() {
close(lancer.dataChannel)
close(lancer.quit)
lancer.wg.Wait()
}
func (lancer *LancerLogStream) processor() {
defer func() {
if lancer.conn != nil {
lancer.conn.Close()
}
lancer.wg.Done()
}()
var lastFail *LancerData
PROCESSOR:
for {
select {
case <-lancer.quit:
return
default:
}
if lastFail != nil {
if err := lancer.write(lastFail.Bytes()); err != nil {
runtime.Gosched()
continue PROCESSOR
}
lancer.pool.Put(lastFail)
lastFail = nil
}
for b := range lancer.dataChannel {
if err := lancer.write(b.Bytes()); err != nil {
lastFail = b
runtime.Gosched()
continue PROCESSOR
}
lancer.pool.Put(b)
}
return
}
}
func (lancer *LancerLogStream) write(b []byte) error {
if lancer.conn == nil {
c, err := net.DialTimeout("tcp", lancer.addr, lancer.timeout)
if err != nil {
log.Error("[Lancer]Dial %s error:%+v", lancer.addr, err)
return err
}
lancer.conn = c
}
_, err := lancer.conn.Write(b)
if err != nil {
log.Error("[Lancer]Conn write error:%+v", err)
lancer.conn.Close()
lancer.conn = nil
}
return err
}
func (lancer *LancerLogStream) NewLancerData(logid string, token string) *LancerData {
ld := lancer.pool.Get().(*LancerData)
ld.Reset()
ld.lancer = lancer
ld.isAppend = false
ld.logid = logid
ld.Write([]byte{0xAC, 0xBE})
ld.Write([]byte{0, 0, 0, 0})
ld.Write([]byte{0, 1})
header := fmt.Sprintf("logId=%s&timestamp=%d&token=%s&version=1.1", logid,
time.Now().UnixNano()/int64(time.Millisecond), token)
headerLength := uint16(len(header))
ld.Write([]byte{byte(headerLength >> 8), byte(headerLength)})
ld.Write([]byte(header))
return ld
}
func (ld *LancerData) PutString(v string) *LancerData {
ld.splitter()
ld.WriteString(strings.Replace(v, ld.lancer.splitter, ld.lancer.replacer, -1))
return ld
}
func (ld *LancerData) PutTimestamp(v time.Time) *LancerData {
return ld.PutInt(v.Unix())
}
func (ld *LancerData) PutUint(v uint64) *LancerData {
ld.splitter()
ld.WriteString(strconv.FormatUint(v, 10))
return ld
}
func (ld *LancerData) PutInt(v int64) *LancerData {
ld.splitter()
ld.WriteString(strconv.FormatInt(v, 10))
return ld
}
func (ld *LancerData) PutFloat(v float64) *LancerData {
ld.splitter()
ld.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
return ld
}
func (ld *LancerData) PutBool(v bool) *LancerData {
ld.splitter()
ld.WriteString(strconv.FormatBool(v))
return ld
}
func (ld *LancerData) splitter() {
if ld.isAppend {
ld.WriteString(ld.lancer.splitter)
}
ld.isAppend = true
}
func (ld *LancerData) Commit() error {
if ld.Len() < kLancerMinimumLength {
return errors.New("protocol error")
}
l := uint32(ld.Len()) - kLancerMinimumLength
binary.BigEndian.PutUint32(ld.Bytes()[kLancerLengthPartBegin:kLancerLengthPartEnd], l)
select {
case ld.lancer.dataChannel <- ld:
return nil
default:
ld.lancer.pool.Put(ld)
return errors.New("lancer channel is full")
}
}

View File

@@ -0,0 +1,43 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"lrucache_test.go",
"synccache_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = [
"lrucache.go",
"synccache.go",
],
importpath = "go-common/app/common/live/library/lrucache",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
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,139 @@
package lrucache
// Element - node to store cache item
type Element struct {
prev, next *Element
Key interface{}
Value interface{}
}
// Next - fetch older element
func (e *Element) Next() *Element {
return e.next
}
// Prev - fetch newer element
func (e *Element) Prev() *Element {
return e.prev
}
// LRUCache - a data structure that is efficient to insert/fetch/delete cache items [both O(1) time complexity]
type LRUCache struct {
cache map[interface{}]*Element
head *Element
tail *Element
capacity int
}
// New - create a new lru cache object
func New(capacity int) *LRUCache {
return &LRUCache{make(map[interface{}]*Element), nil, nil, capacity}
}
// Put - put a cache item into lru cache
func (lc *LRUCache) Put(key interface{}, value interface{}) {
if e, ok := lc.cache[key]; ok {
e.Value = value
lc.refresh(e)
return
}
if lc.capacity == 0 {
return
} else if len(lc.cache) >= lc.capacity {
// evict the oldest item
delete(lc.cache, lc.tail.Key)
lc.remove(lc.tail)
}
e := &Element{nil, lc.head, key, value}
lc.cache[key] = e
if len(lc.cache) != 1 {
lc.head.prev = e
} else {
lc.tail = e
}
lc.head = e
}
// Get - get value of key from lru cache with result
func (lc *LRUCache) Get(key interface{}) (interface{}, bool) {
if e, ok := lc.cache[key]; ok {
lc.refresh(e)
return e.Value, ok
}
return nil, false
}
// Delete - delete item by key from lru cache
func (lc *LRUCache) Delete(key interface{}) {
if e, ok := lc.cache[key]; ok {
delete(lc.cache, key)
lc.remove(e)
}
}
// Range - calls f sequentially for each key and value present in the lru cache
func (lc *LRUCache) Range(f func(key, value interface{}) bool) {
for i := lc.head; i != nil; i = i.Next() {
if !f(i.Key, i.Value) {
break
}
}
}
// Update - inplace update
func (lc *LRUCache) Update(key interface{}, f func(value *interface{})) {
if e, ok := lc.cache[key]; ok {
f(&e.Value)
lc.refresh(e)
}
}
// Front - get front element of lru cache
func (lc *LRUCache) Front() *Element {
return lc.head
}
// Back - get back element of lru cache
func (lc *LRUCache) Back() *Element {
return lc.tail
}
// Len - length of lru cache
func (lc *LRUCache) Len() int {
return len(lc.cache)
}
// Capacity - capacity of lru cache
func (lc *LRUCache) Capacity() int {
return lc.capacity
}
func (lc *LRUCache) refresh(e *Element) {
if e.prev != nil {
e.prev.next = e.next
if e.next == nil {
lc.tail = e.prev
} else {
e.next.prev = e.prev
}
e.prev = nil
e.next = lc.head
lc.head.prev = e
lc.head = e
}
}
func (lc *LRUCache) remove(e *Element) {
if e.prev == nil {
lc.head = e.next
} else {
e.prev.next = e.next
}
if e.next == nil {
lc.tail = e.prev
} else {
e.next.prev = e.prev
}
}

View File

@@ -0,0 +1,257 @@
package lrucache
import (
"container/list"
"testing"
)
type Elem struct {
key int
value string
}
func Test_New(t *testing.T) {
lc := New(5)
if lc.Len() != 0 {
t.Error("case 1 failed")
}
}
func Test_Put(t *testing.T) {
lc := New(0)
lc.Put(1, "1")
if lc.Len() != 0 {
t.Error("case 1.1 failed")
}
lc = New(5)
lc.Put(1, "1")
lc.Put(2, "2")
lc.Put(1, "3")
if lc.Len() != 2 {
t.Error("case 2.1 failed")
}
l := list.New()
l.PushBack(&Elem{1, "3"})
l.PushBack(&Elem{2, "2"})
e := l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 2.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 2.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
lc.Put(3, "4")
lc.Put(4, "5")
lc.Put(5, "6")
lc.Put(2, "7")
if lc.Len() != 5 {
t.Error("case 3.1 failed")
}
l = list.New()
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{5, "6"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
l.PushBack(&Elem{1, "3"})
rl := list.New()
rl.PushBack(&Elem{1, "3"})
rl.PushBack(&Elem{3, "4"})
rl.PushBack(&Elem{4, "5"})
rl.PushBack(&Elem{5, "6"})
rl.PushBack(&Elem{2, "7"})
e = l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 3.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 3.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
e = rl.Front()
for c := lc.Back(); c != nil; c = c.Prev() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 3.4 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 3.5 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
lc.Put(6, "8")
if lc.Len() != 5 {
t.Error("case 4.1 failed")
}
l = list.New()
l.PushBack(&Elem{6, "8"})
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{5, "6"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
e = l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 4.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 4.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
}
func Test_Get(t *testing.T) {
lc := New(2)
lc.Put(1, "1")
lc.Put(2, "2")
if v, _ := lc.Get(1); v != "1" {
t.Error("case 1.1 failed")
}
lc.Put(3, "3")
if lc.Len() != 2 {
t.Error("case 1.2 failed")
}
l := list.New()
l.PushBack(&Elem{3, "3"})
l.PushBack(&Elem{1, "1"})
e := l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 1.3 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 1.4 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
}
func Test_Delete(t *testing.T) {
lc := New(5)
lc.Put(3, "4")
lc.Put(4, "5")
lc.Put(5, "6")
lc.Put(2, "7")
lc.Put(6, "8")
lc.Delete(5)
l := list.New()
l.PushBack(&Elem{6, "8"})
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
if lc.Len() != 4 {
t.Error("case 1.1 failed")
}
e := l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 1.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 1.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
lc.Delete(6)
l = list.New()
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
if lc.Len() != 3 {
t.Error("case 2.1 failed")
}
e = l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 2.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 2.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
lc.Delete(3)
l = list.New()
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{4, "5"})
if lc.Len() != 2 {
t.Error("case 3.1 failed")
}
e = l.Front()
for c := lc.Front(); c != nil; c = c.Next() {
v := e.Value.(*Elem)
if c.Key.(int) != v.key {
t.Error("case 3.2 failed: ", c.Key.(int), v.key)
}
if c.Value.(string) != v.value {
t.Error("case 3.3 failed: ", c.Value.(string), v.value)
}
e = e.Next()
}
}
func Test_Range(t *testing.T) {
lc := New(5)
lc.Put(3, "4")
lc.Put(4, "5")
lc.Put(5, "6")
lc.Put(2, "7")
lc.Put(6, "8")
l := list.New()
l.PushBack(&Elem{6, "8"})
l.PushBack(&Elem{2, "7"})
l.PushBack(&Elem{5, "6"})
l.PushBack(&Elem{4, "5"})
l.PushBack(&Elem{3, "4"})
e := l.Front()
lc.Range(
func(key, value interface{}) bool {
v := e.Value.(*Elem)
if key.(int) != v.key {
t.Error("case 1.1 failed: ", key.(int), v.key)
}
if value.(string) != v.value {
t.Error("case 1.2 failed: ", value.(string), v.value)
}
e = e.Next()
return true
})
if e != nil {
t.Error("case 1.3 failed: ", e.Value)
}
}

View File

@@ -0,0 +1,99 @@
package lrucache
import (
"hash/crc32"
"sync"
"time"
)
// hashCode hashes a string to a unique hashcode.
//
// crc32 returns a uint32, but for our use we need
// and non negative integer. Here we cast to an integer
// and invert it if the result is negative.
func hashCode(s string) (hc int) {
hc = int(crc32.ChecksumIEEE([]byte(s)))
if hc >= 0 {
return hc
}
if -hc >= 0 {
return -hc
}
// hc == MinInt
return hc
}
// SyncCache - concurrent cache structure
type SyncCache struct {
locks []sync.Mutex
caches []*LRUCache
mask int
timeout int64
}
type scValue struct {
Value interface{}
ts int64
}
func nextPowOf2(cap int) int {
if cap < 2 {
return 2
}
if cap&(cap-1) == 0 {
return cap
}
cap |= cap >> 1
cap |= cap >> 2
cap |= cap >> 4
cap |= cap >> 8
cap |= cap >> 16
return cap + 1
}
// NewSyncCache - create sync cache
// `capacity` is lru cache length of each bucket
// store `capacity * bucket` count of element in SyncCache at most
// `timeout` is in seconds
func NewSyncCache(capacity int, bucket int, timeout int64) *SyncCache {
size := nextPowOf2(bucket)
sc := SyncCache{make([]sync.Mutex, size), make([]*LRUCache, size), size - 1, timeout}
for i := range sc.caches {
sc.caches[i] = New(capacity)
}
return &sc
}
// Put - put a cache item into sync cache
func (sc *SyncCache) Put(key string, value interface{}) {
idx := hashCode(key) & sc.mask
sc.locks[idx].Lock()
sc.caches[idx].Put(key, &scValue{value, time.Now().Unix()})
sc.locks[idx].Unlock()
}
// Get - get value of key from sync cache with result
func (sc *SyncCache) Get(key string) (interface{}, bool) {
idx := hashCode(key) & sc.mask
sc.locks[idx].Lock()
v, b := sc.caches[idx].Get(key)
if !b {
sc.locks[idx].Unlock()
return nil, false
}
if time.Now().Unix()-v.(*scValue).ts >= sc.timeout {
sc.caches[idx].Delete(key)
sc.locks[idx].Unlock()
return nil, false
}
sc.locks[idx].Unlock()
return v.(*scValue).Value, b
}
// Delete - delete item by key from sync cache
func (sc *SyncCache) Delete(key string) {
idx := hashCode(key) & sc.mask
sc.locks[idx].Lock()
sc.caches[idx].Delete(key)
sc.locks[idx].Unlock()
}

View File

@@ -0,0 +1,85 @@
package lrucache
import (
"sync"
"testing"
"time"
)
func Test_hashCode(t *testing.T) {
/*if hashCode(-1) != 1 {
t.Error("case 1 failed")
}
if hashCode(0) != 0 {
t.Error("case 2 failed")
}
if hashCode(0x7FFFFFFF) != 0x7FFFFFFF {
t.Error("case 3 failed")
}*/
if hashCode("12345") != 3421846044 {
t.Error("case 4 failed")
}
if hashCode("abcdefghijklmnopqrstuvwxyz") != 1277644989 {
t.Error("case 5 failed")
}
/*if hashCode(123.45) != 123 {
t.Error("case 6 failed")
}
if hashCode(-15268.45) != 15268 {
t.Error("case 7 failed")
}*/
}
func Test_nextPowOf2(t *testing.T) {
if nextPowOf2(0) != 2 {
t.Error("case 1 failed")
}
if nextPowOf2(1) != 2 {
t.Error("case 2 failed")
}
if nextPowOf2(2) != 2 {
t.Error("case 3 failed")
}
if nextPowOf2(3) != 4 {
t.Error("case 4 failed")
}
if nextPowOf2(123) != 128 {
t.Error("case 5 failed")
}
if nextPowOf2(0x7FFFFFFF) != 0x80000000 {
t.Error("case 6 failed")
}
}
func Test_timeout(t *testing.T) {
sc := NewSyncCache(1, 2, 2)
sc.Put("1", "2")
if v, ok := sc.Get("1"); !ok || v != "2" {
t.Error("case 1 failed")
}
time.Sleep(2 * time.Second)
if _, ok := sc.Get("1"); ok {
t.Error("case 2 failed")
}
}
func Test_concurrent(t *testing.T) {
sc := NewSyncCache(1, 4, 2)
var wg sync.WaitGroup
for index := 0; index < 100000; index++ {
wg.Add(3)
go func() {
sc.Put("1", "2")
wg.Done()
}()
go func() {
sc.Get("1")
wg.Done()
}()
go func() {
sc.Delete("1")
wg.Done()
}()
}
wg.Wait()
}

View File

@@ -0,0 +1,29 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["mengde.go"],
importpath = "go-common/app/common/live/library/mengde",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["//library/log: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,309 @@
package mengde
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"go-common/library/log"
)
const (
kSvenCheckTimeout = time.Minute
kSvenGetTimeout = 30 * time.Second
kSvenHost = "http://config.bilibili.co"
kSvenCheckAPI = kSvenHost + "/config/v2/check"
kSvenGetAPI = kSvenHost + "/config/v2/get"
kDefaultHost = "invalid-host-name"
kDefaultIP = "127.0.0.1"
kCodeNotModified = -304
)
type MengdeClient struct {
treeID string
zone string
env string
build string
token string
host string
ip string
config atomic.Value
notify chan *MengdeConfig
changeSignal chan struct{}
httpClient *http.Client
ctx context.Context
cancel context.CancelFunc
closeSignal chan struct{}
configLoadWg sync.WaitGroup
configNotifyWg sync.WaitGroup
}
type MengdeConfig struct {
Version int
Config map[string]string
}
func NewMengdeClient(treeID string, zone string, env string, build string, token string) (*MengdeClient, error) {
host, err := os.Hostname()
if err != nil {
host = kDefaultHost
}
ip := kDefaultIP
addr, err := net.InterfaceAddrs()
if err == nil {
for _, a := range addr {
if n, ok := a.(*net.IPNet); ok && !n.IP.IsLoopback() && n.IP.To4() != nil {
ip = n.IP.String()
}
}
}
c := &MengdeClient{
treeID: treeID,
zone: zone,
env: env,
build: build,
token: token,
host: host,
ip: ip,
notify: make(chan *MengdeConfig),
changeSignal: make(chan struct{}, 1),
httpClient: new(http.Client),
closeSignal: make(chan struct{}),
}
c.ctx, c.cancel = context.WithCancel(context.Background())
version, config, err := c.svenConfigSync()
if err != nil {
return nil, err
}
c.config.Store(&MengdeConfig{
Version: version,
Config: config,
})
c.changeSignal <- struct{}{}
c.configLoadWg.Add(1)
go func() {
defer c.configLoadWg.Done()
c.configLoadProcess()
}()
c.configNotifyWg.Add(1)
go func() {
defer c.configNotifyWg.Done()
c.configNotifyProcess()
}()
return c, nil
}
func (c *MengdeClient) Close() {
close(c.closeSignal)
c.cancel()
c.configLoadWg.Wait()
close(c.changeSignal)
c.configNotifyWg.Wait()
}
func (c *MengdeClient) Config() *MengdeConfig {
result := &MengdeConfig{
Version: -1,
Config: make(map[string]string),
}
if config, ok := c.config.Load().(*MengdeConfig); ok {
result.Version = config.Version
for k, v := range config.Config {
result.Config[k] = v
}
}
return result
}
func (c *MengdeClient) ConfigNotify() <-chan *MengdeConfig {
return c.notify
}
func (c *MengdeClient) configLoadProcess() {
for {
select {
case <-c.closeSignal:
return
default:
}
current, ok := c.config.Load().(*MengdeConfig)
if !ok {
current = &MengdeConfig{
Version: -1,
}
}
version, err := c.svenCheckVersion(current.Version)
//config not modified
if version == current.Version {
log.Info("[sven]check version config not modified")
continue
}
if err != nil {
log.Error("[sven]check version error:%s", err.Error())
continue
}
if current.Version == version {
continue
}
config, err := c.svenGetConfig(version)
if err != nil {
log.Error(err.Error())
continue
}
c.config.Store(&MengdeConfig{
Version: version,
Config: config,
})
select {
case c.changeSignal <- struct{}{}:
default:
}
}
}
func (c *MengdeClient) configNotifyProcess() {
for range c.changeSignal {
select {
case <-c.closeSignal:
return
case c.notify <- c.Config():
}
}
}
func (c *MengdeClient) svenConfigSync() (int, map[string]string, error) {
version, err := c.svenCheckVersion(-1)
if err != nil {
return -1, nil, err
}
config, err := c.svenGetConfig(version)
if err != nil {
return -1, nil, err
}
return version, config, nil
}
func (c *MengdeClient) svenCheckVersion(version int) (int, error) {
var err error
req, err := http.NewRequest("GET", kSvenCheckAPI, nil)
if err != nil {
return -1, err
}
q := req.URL.Query()
q.Add("build", c.build)
q.Add("hostname", c.host)
q.Add("ip", c.ip)
q.Add("service", strings.Join([]string{c.treeID, c.env, c.zone}, "_"))
q.Add("token", c.token)
q.Add("version", strconv.Itoa(version))
req.URL.RawQuery = q.Encode()
ctx, cancel := context.WithTimeout(c.ctx, kSvenCheckTimeout)
defer cancel()
req = req.WithContext(ctx)
resp, err := c.httpClient.Do(req)
if err != nil {
return -1, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return -1, errors.New(fmt.Sprintf("http request URL:%s result:%s", req.URL.RawQuery, resp.Status))
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -1, err
}
var svenCheckRespBody struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Version int `json:"version"`
} `json:"data"`
}
if err = json.Unmarshal(result, &svenCheckRespBody); err != nil {
return -1, err
}
if svenCheckRespBody.Code != 0 {
if svenCheckRespBody.Code == kCodeNotModified {
return version, nil
}
return -1, errors.New(svenCheckRespBody.Message)
}
return svenCheckRespBody.Data.Version, nil
}
func (c *MengdeClient) svenGetConfig(version int) (map[string]string, error) {
var err error
req, err := http.NewRequest("GET", kSvenGetAPI, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("build", c.build)
q.Add("hostname", c.host)
q.Add("ip", c.ip)
q.Add("service", strings.Join([]string{c.treeID, c.env, c.zone}, "_"))
q.Add("token", c.token)
q.Add("version", strconv.Itoa(version))
req.URL.RawQuery = q.Encode()
ctx, cancel := context.WithTimeout(c.ctx, kSvenGetTimeout)
defer cancel()
req = req.WithContext(ctx)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("http request URL:%s result:%s", req.URL.RawQuery, resp.Status))
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var svenGetRespBody struct {
Code int `json:"code"`
Message string `json:"message"`
TTL int `json:"ttl"`
Data struct {
Version int `json:"version"`
MD5 string `json:"md5"`
Content string `json:"content"`
} `json:"data"`
}
if err = json.Unmarshal(result, &svenGetRespBody); err != nil {
return nil, err
}
if svenGetRespBody.Code != 0 {
return nil, errors.New(svenGetRespBody.Message)
}
var configContent []struct {
Name string `json:"name"`
Config string `json:"config"`
}
if err = json.Unmarshal([]byte(svenGetRespBody.Data.Content), &configContent); err != nil {
return nil, err
}
config := make(map[string]string)
for _, c := range configContent {
config[c.Name] = strings.TrimSpace(c.Config)
}
return config, nil
}

View File

@@ -0,0 +1,19 @@
package(default_visibility = ["//visibility:public"])
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/common/openplatform/encoding:all-srcs",
"//app/common/openplatform/geetest:all-srcs",
"//app/common/openplatform/random:all-srcs",
],
tags = ["automanaged"],
)

View File

@@ -0,0 +1,10 @@
# v1.0.0
1.添加随机数方法
2.判断随机数位数
3.增加审核人
# v1.0.1
1. 增加加密方法
# v1.0.2
1. 增加极验sdk

View File

@@ -0,0 +1,8 @@
# Owner
liuzhan
huangshancheng
# Author
# Reviewer
qiuliang

View File

@@ -0,0 +1,11 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- huangshancheng
- liuzhan
labels:
- common
- library
- openplatform
reviewers:
- qiuliang

View File

@@ -0,0 +1,13 @@
# openplatform-common
# 项目简介
1.开放平台公共模块
# 编译环境
# 依赖包
# 编译执行

View File

@@ -0,0 +1,38 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["encoding_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["encoding.go"],
importpath = "go-common/app/common/openplatform/encoding",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
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,105 @@
package encoding
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"
)
//EncryptConfig 加密配置
type EncryptConfig struct {
Key string
IV string
}
//JSON JSON格式化增加对[]byte的支持
func JSON(o interface{}) string {
var v []byte
switch o1 := o.(type) {
case [][]byte:
o2 := make([]interface{}, len(o1))
for i := 0; i < len(o1); i++ {
o2[i] = string(o1[i])
}
v, _ = json.Marshal(o2)
case []interface{}:
for i := 0; i < len(o1); i++ {
if b, ok := o1[i].([]byte); ok {
o1[i] = string(b)
}
}
v, _ = json.Marshal(o1)
default:
v, _ = json.Marshal(o)
}
if len(v) > 0 {
return string(v)
}
return ""
}
//填充到BlockSize整数倍长度如果正好就是对的长度再多填充一个BlockSize长度
func pad(src []byte) []byte {
padding := aes.BlockSize - len(src)%aes.BlockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
//去除填充的字节
func unpad(src []byte) ([]byte, error) {
length := len(src)
if length == 0 {
return []byte{0}, nil
}
unpadding := int(src[length-1])
if unpadding > length {
return nil, errors.New("unpad error. This could happen when incorrect encryption key is used")
}
return src[:(length - unpadding)], nil
}
//Encrypt 加密
func Encrypt(text string, c *EncryptConfig) (string, error) {
if "" == text {
return "", nil
}
block, err := aes.NewCipher([]byte(c.Key))
if err != nil {
return "", err
}
msg := pad([]byte(text))
ciphertext := make([]byte, len(msg))
mode := cipher.NewCBCEncrypter(block, []byte(c.IV))
mode.CryptBlocks(ciphertext, msg)
finalMsg := base64.StdEncoding.EncodeToString(ciphertext)
return finalMsg, nil
}
//Decrypt 解密
func Decrypt(text string, c *EncryptConfig) (string, error) {
if "" == text {
return "", nil
}
block, err := aes.NewCipher([]byte(c.Key))
if err != nil {
return "", err
}
decodedMsg, err := base64.StdEncoding.DecodeString(text)
if err != nil {
return "", err
}
if (len(decodedMsg) % aes.BlockSize) != 0 {
return "", errors.New("blocksize must be multipe of decoded message length")
}
msg := decodedMsg
mode := cipher.NewCBCDecrypter(block, []byte(c.IV))
mode.CryptBlocks(msg, msg)
unpadMsg, err := unpad(msg)
if err != nil {
return "", err
}
return string(unpadMsg), nil
}

View File

@@ -0,0 +1,28 @@
package encoding
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
var c = &EncryptConfig{Key: "bilibili_key_GYl", IV: "biliBiliIv123456"}
const (
plat = "13291831554"
enc = "dWLVOucFkBBmmXmTm6so5A=="
)
func TestEncrypt(t *testing.T) {
convey.Convey("Encrypt", t, func() {
s, _ := Encrypt(plat, c)
convey.So(s, convey.ShouldEqual, enc)
})
}
func TestDecrypt(t *testing.T) {
convey.Convey("Decrypt", t, func() {
s, _ := Decrypt(enc, c)
convey.So(s, convey.ShouldEqual, plat)
})
}

View File

@@ -0,0 +1,19 @@
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/common/openplatform/geetest/conf:all-srcs",
"//app/common/openplatform/geetest/dao:all-srcs",
"//app/common/openplatform/geetest/model:all-srcs",
"//app/common/openplatform/geetest/service:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["conf.go"],
importpath = "go-common/app/common/openplatform/geetest/conf",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
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,48 @@
package conf
import (
"encoding/json"
"io/ioutil"
)
// Conf info.
var (
Conf *Config
)
// Config struct.
type Config struct {
// httpClinet
HTTPClient *HTTPClient
// host
Host *Host
// Secret
Secret *Secret
}
// HTTPClient conf.
type HTTPClient struct {
Dial int64
KeepAlive int64
}
// Host conf.
type Host struct {
Geetest string
}
// Secret of Geetest
type Secret struct {
CaptchaID string
PrivateKey string
}
// Init conf.
func Init() (err error) {
bs, err := ioutil.ReadFile("config.json")
if err != nil {
return
}
err = json.Unmarshal(bs, &Conf)
return
}

View File

@@ -0,0 +1,16 @@
{
"host":
{
"geetest":"http://api.geetest.com"
},
"httpclient":
{
"dial":1,
"keepAlive":1
},
"secret":
{
"captchaId":"",
"privateKey":""
}
}

View File

@@ -0,0 +1,29 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["geetest.go"],
importpath = "go-common/app/common/openplatform/geetest/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["//app/common/openplatform/geetest/model: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,133 @@
package dao
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"go-common/app/common/openplatform/geetest/model"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
_register = "http://api.geetest.com/register.php"
_validate = "http://api.geetest.com/validate.php"
)
// PreProcess preprocessing the geetest and get to challenge
func PreProcess(c context.Context, mid int64, ip, clientType string, newCaptcha int, captchaID string) (challenge string, err error) {
var (
bs []byte
params url.Values
)
params = url.Values{}
params.Set("user_id", strconv.FormatInt(mid, 10))
params.Set("new_captcha", strconv.Itoa(newCaptcha))
params.Set("client_type", clientType)
params.Set("ip_address", ip)
params.Set("gt", captchaID)
if bs, err = Get(c, _register, params); err != nil {
return
}
if len(bs) != 32 {
return
}
challenge = string(bs)
return
}
// Validate recheck the challenge code and get to seccode
func Validate(c context.Context, challenge, seccode, clientType, ip, captchaID string, mid int64) (res *model.ValidateRes, err error) {
var (
bs []byte
params url.Values
)
params = url.Values{}
params.Set("seccode", seccode)
params.Set("challenge", challenge)
params.Set("captchaid", captchaID)
params.Set("client_type", clientType)
params.Set("ip_address", ip)
params.Set("json_format", "1")
params.Set("sdk", "golang_3.0.0")
params.Set("user_id", strconv.FormatInt(mid, 10))
params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
if bs, err = Post(c, _validate, params); err != nil {
return
}
if err = json.Unmarshal(bs, &res); err != nil {
return
}
return
}
// NewRequest new a http request.
func NewRequest(method, uri string, params url.Values) (req *http.Request, err error) {
if method == "GET" {
req, err = http.NewRequest(method, uri+"?"+params.Encode(), nil)
} else {
req, err = http.NewRequest(method, uri, strings.NewReader(params.Encode()))
}
if err != nil {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return
}
// Do handler request
func Do(c context.Context, req *http.Request) (body []byte, err error) {
var res *http.Response
dialer := &net.Dialer{
Timeout: time.Duration(1 * int64(time.Second)),
KeepAlive: time.Duration(1 * int64(time.Second)),
}
transport := &http.Transport{
DialContext: dialer.DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: transport,
}
req = req.WithContext(c)
if res, err = client.Do(req); err != nil {
return
}
defer res.Body.Close()
if res.StatusCode >= http.StatusInternalServerError {
err = errors.New("http status code 5xx")
return
}
if body, err = ioutil.ReadAll(res.Body); err != nil {
return
}
return
}
// Get client.Get send GET request
func Get(c context.Context, uri string, params url.Values) (body []byte, err error) {
req, err := NewRequest("GET", uri, params)
if err != nil {
return
}
body, err = Do(c, req)
return
}
// Post client.Get send POST request
func Post(c context.Context, uri string, params url.Values) (body []byte, err error) {
req, err := NewRequest("POST", uri, params)
if err != nil {
return
}
body, err = Do(c, req)
return
}

View File

@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["geetest.go"],
importpath = "go-common/app/common/openplatform/geetest/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
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,14 @@
package model
//ProcessRes 拉起返回值
type ProcessRes struct {
Success int `json:"success"`
CaptchaID string `json:"gt"`
Challenge string `json:"challenge"`
NewCaptcha int `json:"new_captcha"`
}
//ValidateRes 验证返回值
type ValidateRes struct {
Seccode string `json:"seccode"`
}

View File

@@ -0,0 +1,32 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["geetest.go"],
importpath = "go-common/app/common/openplatform/geetest/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/common/openplatform/geetest/dao:go_default_library",
"//app/common/openplatform/geetest/model: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,53 @@
package service
import (
"context"
"crypto/md5"
"encoding/hex"
"go-common/app/common/openplatform/geetest/dao"
"go-common/app/common/openplatform/geetest/model"
"math/rand"
"strconv"
)
// PreProcess preprocessing the geetest and get to challenge
func PreProcess(c context.Context, mid int64, newCaptcha int, ip, clientType, captchaID, privateKey string) (res *model.ProcessRes, err error) {
var pre string
res = &model.ProcessRes{}
res.CaptchaID = captchaID
res.NewCaptcha = newCaptcha
if pre, err = dao.PreProcess(c, mid, ip, clientType, newCaptcha, captchaID); err != nil || pre == "" {
randOne := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
randTwo := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
challenge := hex.EncodeToString(randOne[:]) + hex.EncodeToString(randTwo[:])[0:2]
res.Challenge = challenge
return
}
res.Success = 1
slice := md5.Sum([]byte(pre + privateKey))
res.Challenge = hex.EncodeToString(slice[:])
return
}
// Validate recheck the challenge code and get to seccode
func Validate(c context.Context, challenge, validate, seccode, clientType, ip, captchaID, privateKey string, success int, mid int64) (stat bool) {
if len(validate) != 32 {
return
}
if success != 1 {
slice := md5.Sum([]byte(challenge))
stat = hex.EncodeToString(slice[:]) == validate
return
}
slice := md5.Sum([]byte(privateKey + "geetest" + challenge))
if hex.EncodeToString(slice[:]) != validate {
return
}
res, err := dao.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
if err != nil {
return
}
slice = md5.Sum([]byte(seccode))
stat = hex.EncodeToString(slice[:]) == res.Seccode
return
}

View File

@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["uniqid.go"],
importpath = "go-common/app/common/openplatform/random",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
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,35 @@
package random
import (
"math"
"math/rand"
"time"
)
var (
rnd *rand.Rand
ch chan int64
)
func init() {
rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
ch = make(chan int64, 1000)
go randomBase(ch)
}
func randomBase(c chan int64) {
for {
c <- rnd.Int63()
}
}
//Uniqid 随机数length是需要返回的长度只支持10~19位
func Uniqid(length int) int64 {
if length < 10 || length > 19 {
return 0
}
prefix := (time.Now().UnixNano() / 100000000) & 0x3fffffff
cut := int64(math.Pow10(length - 9))
suffix := <-ch % cut
return prefix*cut + suffix
}

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
#替换当前目录下文件中Id,Url这种不合规的字符
replacements="Id Url Sku Uid"
for i in $replacements; do
r=`echo $i|tr 'a-z' 'A-Z'`
find . -name "*.go"|xargs sed -ibak -E 's/'$i'(s?)([[:>:]]|[A-Z_])/'$r'\1\2/g'
done
find . -name "*.gobak"|xargs rm

View File

@@ -0,0 +1,107 @@
#!/usr/bin/env bash
#自动配置环境变量并使用测试配置运行go run或test加参数 -t可以项目任一目录执行都可以自动找到并运行对应的cmd/main.go
export DEPLOY_ENV=uat
export ZONE=sh001
export APP_ID
export APP_ROOT
grpcPort=9000
httpPort=8000
perfPort=2000
portOffset=0
getApp(){
declare -a dirs
n=0
wd=$PWD
depts=4
while true;do
cur=`basename $PWD`
if [ "$cur" == "" ] || [ "$cur" == "/" ]; then
break
fi
dirs[n]=$cur
if [ "${dirs[n]}" == "go-common" ] && [ "${dirs[n-1]}" == "app" ]; then
if [ $n -lt $depts ]; then
break
fi
for((i=n-1;i>=n-depts+1;i--));do
cd "${dirs[i]}"
done
export APP_ID=${dirs[n-depts]}
export APP_ROOT=$PWD/$APP_ID
portOffset=`ls -l|grep ^d|awk '{if($NF=="'$APP_ID'")print FNR}'`
cd $wd
return 0
fi
let n=n+1
cd ..
done
echo "must be run in go-common app directory" >&2
exit 1
}
getApp
let grpcPort=grpcPort+portOffset
let httpPort=httpPort+portOffset
let perfPort=perfPort+portOffset
export GRPC="tcp://0.0.0.0:"$grpcPort"/?timeout=1s&idle_timeout=60s"
export HTTP="tcp://0.0.0.0:"$httpPort"/?timeout=1s"
export HTTP_PERF="tcp://0.0.0.0:$perfPort"
conf=`find $APP_ROOT/cmd -name "*.toml"`
if [ ! -f "$conf" ]; then
echo "toml file not exist"
exit 1
fi
logdir=`sed -n '/^\[log\]/,$p' $conf|grep -m1 'dir *='|awk -F'=' '{print $2}'|cut -d'"' -f2`
if [ -n "$logdir" ] && [ ! -d "$logdir" ];then
mkdir -p "$logdir"
fi
while getopts t o; do
case "$o" in
t) test=1;;
esac
done
shift $((OPTIND-1))
target=$1
#运行main.go
if [ -z "$test" ]; then
if [ -z "$target" ]; then
target=$APP_ROOT/cmd/main.go
fi
echo "running "$APP_ID" on grpc port:"$grpcPort" and http port:"$httpPort
go run "$target" -conf "$conf"
exit 0
fi
methods=""
files=""
for i in $@;do
if echo $i|grep '^[a-zA-Z0-9_]\+$'>/dev/null; then
methods=$methods$i" "
else
files=$files$i" "
fi
done
if [ -n "$methods" ]; then
for m in $methods; do
go test -v -run $m -conf "$conf"
done
fi
if [ -n "$files" ];then
for f in $files; do
base=`echo $f|sed 's/_test\.go//'`
if [ "$base" != "$f" ] && ! echo ' '$files' '|grep ' '$base'\.go '>/dev/null; then
files=$files$base".go "
fi
done
go test -v $files -conf "$conf"
fi