yc

package module
v0.0.0-...-e120e2b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2023 License: BSD-3-Clause Imports: 18 Imported by: 0

README

yc

远界云微服务开发框架

OSCS Status

Requirements

  • protoc v3.12.4
  • protoc-gen-go v1.28
  • protoc-gen-go-grpc v.2

Prepare

Install protoc
  1. Go plugins for the protocol compiler
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
  1. Install protoc You can get the compiled binary file at protoc-3.12.4

For OSX

wget https://github.com/protocolbuffers/protobuf/releases/download/v3.12.4/protoc-3.12.4-osx-x86_64.zip
mkdir -p $GOPATH/bin/protoc.d/protoc-3.12.4
mv protoc-3.12.4-osx-x86_64.zip $GOPATH/bin/protoc.d
cd $GOPATH/bin/protoc.d/protoc-3.12.4/ && unzip protoc-3.12.4-osx-x86_64.zip 
cd $GOPATH/bin
if [ -f protoc ]; then rm protoc; fi 
ln -s $GOPATH/bin/protoc.d/protoc-3.12.4/bin/protoc protoc

For Linux

wget https://github.com/protocolbuffers/protobuf/releases/download/v3.12.4/protoc-3.12.4-linux-x86_64.zip
mkdir -p $GOPATH/bin/protoc.d/protoc-3.12.4
mv protoc-3.12.4-linux-x86_64.zip $GOPATH/bin/protoc.d
cd $GOPATH/bin/protoc.d/protoc-3.12.4/ && unzip protoc-3.12.4-linux-x86_64.zip 
cd $GOPATH/bin
if [ -f protoc ]; then rm protoc; fi 
ln -s $GOPATH/bin/protoc.d/protoc-3.12.4/bin/protoc protoc
Model Usage

waiting

Validator Usage

Add @v: xxx above field definition in *.proto file, and 'xxx' means validator formula, such as:

  • length.min
  • length.max
  • min
  • max
  • pattern
  • datetime
  • date
  • time
  • timestamp.log
  • password
  • required
  • in

framework will check all validator in the message with suffix Request,

e.g.

syntax="proto3";

message LoginRequest {
  // @v: length.min=10
  // @msg: invalid userName: {{.UserName}}
  string userName = 1;
}

Notice:

@msg means yc will response the specified error message defined by go-template

For examples:

1.length.min or length.max

// @v: length.min=10
// @msg: 长度最小值为: 10
List []int

// @v: length.max=10
// @msg: 长度最大值为:10
List []int

2. min or max

@msg 后面跟着一个 go template,可以通过双重花括号获取当前的值

// @v: min=10
// @msg: 最小值为10,当前值为{{.Money}}
Money float64

// @v: max=10
// @msg: 最大值为10,当前值为{{.Money}}
Money float64

3. pattern

// @v: pattern=^([\\d])$
// @msg: invalid password
Password string

4. required

表示某个参数必传,没有 @msg 就获取系统默认错误信息,(指针不能为空)

// @v: required
Pagination *dt.Pagination

5. in

表示检查取值范围,通过逗号分割表示集合

// @v: in=1,3,4
Type int64

6. password

密码检查,如果密码规则有特别要求,可以用pattern

Documentation

Index

Constants

View Source
const (
	SignMethodUnknown = SignMethod("unknown")
	SignMethodMd5     = SignMethod("md5")
	SignMethodSha1    = SignMethod("sha1")
	SignMethodSha256  = SignMethod("sha256")
	SignMethodSha512  = SignMethod("sha512")
)
View Source
const (
	HeaderContextCallerService = "X-Rpc-Caller-Service"
	HeaderContextCallerLevel   = "X-Rpc-Caller-Level"
	HeaderContextAccessKey     = "X-Rpc-Access-Key"
	HeaderContextToken         = "X-Rpc-Token"
	HeaderContextSign          = "X-Rpc-Sign"
	HeaderContextSignMethod    = "X-Rpc-Sign-Method"
	HeaderContextUserId        = "X-Rpc-User-Id"
	HeaderContextTimestamp     = "X-Rpc-Timestamp"
)
View Source
const (
	DefaultRpcPort       = 20443
	DefaultHttpPort      = 20080
	DefaultCallerLevel   = 1000
	SignExpiresInSeconds = 15
)
View Source
const (
	DateTimeLayout = "2006-01-02 15:04:05"
	DateLayout     = "2006-01-02"
	TimeLayout     = "15:04:05"
	LogTimestamp   = "2006-01-02 15:04:05.000"
)
View Source
const Version = "v0.0.2"

Variables

View Source
var (
	MdContextCallerService = strings.ToLower(HeaderContextCallerService)
	MdContextCallerLevel   = strings.ToLower(HeaderContextCallerLevel)
	MdContextAccessKey     = strings.ToLower(HeaderContextAccessKey)
	MdContextToken         = strings.ToLower(HeaderContextToken)
	MdContextSign          = strings.ToLower(HeaderContextSign)
	MdContextSignMethod    = strings.ToLower(HeaderContextSignMethod)
	MdContextUserId        = strings.ToLower(HeaderContextUserId)
	MdContextTimestamp     = strings.ToLower(HeaderContextTimestamp)
)

Functions

func Collect

func Collect()

func GetAccessKey

func GetAccessKey(ctx context.Context) (accessKey string, found bool)

func GetCallerLevelFromContext

func GetCallerLevelFromContext(ctx context.Context) (int, error)

func GetCallerLevelFromMd

func GetCallerLevelFromMd(md metadata.MD) (level int, err error)

func GetIncomingHeader

func GetIncomingHeader(ctx context.Context, extra ...metadata.MD) metadata.MD

func GetIncomingUserId

func GetIncomingUserId(ctx context.Context) (userId *dt.ID, err error)

func GetOutgoingHeader

func GetOutgoingHeader(ctx context.Context, extra ...metadata.MD) metadata.MD

func GetTokenFromContext

func GetTokenFromContext(ctx context.Context) (token string, err error)

func IsValidSignMethod

func IsValidSignMethod(signMethod SignMethod) bool

func MergeMetadata

func MergeMetadata(base metadata.MD, md ...metadata.MD) metadata.MD

func MergeRequestHeaderFromMetadata

func MergeRequestHeaderFromMetadata(req *http.Request, md ...metadata.MD)

func ParseJwt

func ParseJwt(ctx context.Context) (*jwt.Claims, error)

func Retry

func Retry(f func() (isEnd bool, err error), times ...int) error

func RpcCall

func RpcCall(ctx context.Context, host string, queryPath string, data proto.Message, out interface{}, md metadata.MD, signHandler RpcSignContentHandler) (code int, err error)

func SetIncomingUserId

func SetIncomingUserId(ctx context.Context, userId *dt.ID) (next context.Context)

Types

type EntrypointDispatcher

type EntrypointDispatcher interface {
	SelectOne() string
}

func NewRandomEntrypointDispatcher

func NewRandomEntrypointDispatcher(entrypointCandidates []string) EntrypointDispatcher

type RandomEntrypointDispatcher

type RandomEntrypointDispatcher struct {
	// contains filtered or unexported fields
}

func (*RandomEntrypointDispatcher) SelectOne

func (t *RandomEntrypointDispatcher) SelectOne() string

type RpcSignContentHandler

type RpcSignContentHandler interface {
	Sum(data []byte) (method SignMethod, signResult string, err error)
	GetAccessKey() string
}

func NewDefaultSignHandler

func NewDefaultSignHandler(accessKey string, secret string) RpcSignContentHandler

type Sha256SignHandler

type Sha256SignHandler struct {
	// contains filtered or unexported fields
}

func (*Sha256SignHandler) GetAccessKey

func (t *Sha256SignHandler) GetAccessKey() string

func (*Sha256SignHandler) Sum

func (t *Sha256SignHandler) Sum(data []byte) (method SignMethod, signResult string, err error)

type SignMethod

type SignMethod string

Directories

Path Synopsis
cmd
yc
cos
Code generated by yc@v0.0.1
Code generated by yc@v0.0.1
cos/db
Code generated by yc@v0.0.1.
Code generated by yc@v0.0.1.
cos/server
Code generated by yc@v0.0.1
Code generated by yc@v0.0.1
cos/server/http
Code generated by yc@v0.0.1.
Code generated by yc@v0.0.1.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL