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,40 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"dao.go",
"token.go",
],
importpath = "go-common/app/admin/live/live-admin/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/live/live-admin/conf:go_default_library",
"//app/service/live/av/api/liverpc:go_default_library",
"//app/service/live/relation/api/liverpc:go_default_library",
"//library/cache/redis:go_default_library",
"//library/log:go_default_library",
"//library/net/rpc/liverpc: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,23 @@
package dao
import (
"go-common/app/admin/live/live-admin/conf"
avApi "go-common/app/service/live/av/api/liverpc"
"go-common/library/net/rpc/liverpc"
)
// AvApi liveRpc room-service api
var AvApi *avApi.Client
// InitAPI init all service APIs
func InitAPI() {
AvApi = avApi.New(getConf("av"))
}
func getConf(appName string) *liverpc.ClientConfig {
c := conf.Conf.LiveRpc
if c != nil {
return c[appName]
}
return nil
}

View File

@@ -0,0 +1,54 @@
package dao
import (
"context"
"go-common/library/cache/redis"
"go-common/library/log"
"go-common/app/admin/live/live-admin/conf"
relationApi "go-common/app/service/live/relation/api/liverpc"
)
// Dao dao
type Dao struct {
c *conf.Config
// mc *memcache.Pool
redis *redis.Pool
// db *xsql.DB
Relation *relationApi.Client
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
// mc: memcache.NewPool(c.Memcache),
redis: redis.NewPool(c.Redis),
// db: xsql.NewMySQL(c.MySQL),
Relation: relationApi.New(getConf("relation")),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
// d.mc.Close()
d.redis.Close()
// d.db.Close()
}
// Ping dao ping
func (d *Dao) Ping(ctx context.Context) (err error) {
if err = d.pingRedis(ctx); err != nil {
log.Error("Failed to ping redis: %v", err)
}
return
}
func (d *Dao) pingRedis(ctx context.Context) (err error) {
conn := d.redis.Get(ctx)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}

View File

@@ -0,0 +1,58 @@
package dao
import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_defaultExpiration = 10
_tokenNamespace = "upload_token:"
)
// RequestUploadToken generates a token for subsequent upload.
// Token will expire in a specific duration.
func (d *Dao) RequestUploadToken(ctx context.Context, bucket, operator string, now int64) (token string, err error) {
token = genToken(bucket, operator, now)
conn := d.redis.Get(ctx)
defer conn.Close()
nsToken := namespaceToken(token)
if _, err = conn.Do("SETEX", nsToken, _defaultExpiration, "1"); err != nil {
log.Error("conn.Do(SETEX %s %d) failure(%v)", nsToken, _defaultExpiration, err)
}
return
}
// VerifyUploadToken verifies if a token is legal.
func (d *Dao) VerifyUploadToken(ctx context.Context, token string) bool {
conn := d.redis.Get(ctx)
defer conn.Close()
nsToken := namespaceToken(token)
valid, err := redis.Bool(conn.Do("GET", nsToken))
if err != nil && err != redis.ErrNil {
log.Warn("conn.Do(GET %s) failure(%v)", nsToken, err)
return false
}
return valid
}
func genToken(bucket, operator string, now int64) string {
sha := sha1.New()
sha.Write([]byte(fmt.Sprintf("i love bilibili:%s:%s:%d", bucket, operator, now)))
return fmt.Sprintf("%s:%d", hex.EncodeToString(sha.Sum([]byte(""))), now)
}
// Avoid key collision.
func namespaceToken(token string) string {
return _tokenNamespace + token
}