goo

package
v1.1.214 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: Apache-2.0 Imports: 27 Imported by: 1

README

mysql

func main() {
	goo_db.Init(goo_context.Cancel(), goo_db.Config{
		Name:   "",
		Driver: "mysql",
		Master:      "root:123456@tcp(192.168.1.100:3306)/ttxian",
		Slaves:      []string{"root:123456@tcp(192.168.1.100:3307)/ttxian"},
		LogModel:    true,
		MaxIdle:     10,
		MaxOpen:     100,
		AutoPing:    true,
		LogFilePath: "",
		LogFileName: "",
	})

	m := map[string]string{}
	exist, err := goo.DB().Table("s_user").Get(&m)
	if err != nil {
		goo_log.Fatal(err.Error())
	}
	if !exist {
		goo_log.Fatal("no data")
	}
	goo_log.Debug(m["account"])

	var wg sync.WaitGroup
	wg.Add(1)
	goo_utils.AsyncFunc(func() {
		defer wg.Done()
		<-goo_context.Cancel().Done()
	})
	wg.Wait()
}

redis

func main() {
	goo_redis.Init(goo_context.Cancel(), goo_redis.Config{
		Name:     "",
		Addr:     "192.168.1.100:6379",
		Password: "123456",
		DB:       0,
		Prefix:   "tt",
		AutoPing: true,
	})

	err := goo.Redis().Set("name", "hnatao", 3*time.Second).Err()
	if err != nil {
		goo_log.Fatal(err.Error())
	}

	name := goo.Redis().Get("name").String()
	goo_log.Debug(name)

	var wg sync.WaitGroup
	wg.Add(1)
	goo_utils.AsyncFunc(func() {
		defer wg.Done()
		<-goo_context.Cancel().Done()
	})
	wg.Wait()
}

Token

func main() {
	tokenStr, err := goo.CreateToken("1111", 100)
	if err != nil {
		goo_log.Fatal(err.Error())
	}
	goo_log.Debug(tokenStr)

	token, err := goo.ParseToken(tokenStr, "1111")
	if err != nil {
		goo_log.Fatal(err.Error())
	}
	goo_log.Debug(token)
}

grpc

var cfg = goo_etcd.Config{
    User: "test",
    Password: "123456",
    Endpoints: []string{"localhost:23791", "localhost:23792", "localhost:23793"},
}

func init() {
	goo_etcd.Init(cfg)
}

func main() {
	s := goo.NewGRPCServer(goo_grpc.Config{
		ENV:         "test",
		ServiceName: "lpro/grpc-user",
		Version:     "v100",
		Addr:        "127.0.0.1:10011",
	}).Register2Etcd(goo_etcd.CLI())

	pb_user_v1.RegisterGetterServer(s.Server, &Server{})

	s.Serve()
}

server

type User struct {
	Age int `form:"age"`
}

func (u User) DoHandle(ctx *goo.Context) *goo.Response {
	if err := ctx.ShouldBind(&u); err != nil {
		return goo.Error(5001, "参数错误", err.Error())
	}
	return goo.Success(u.Age)
}

func main() {
	s := goo.NewServer()

	s.GET("/", goo.Handler(User{}))

	s.Run(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Version     string
	VersionFlag = flag.Bool("v", false, "version")

	HelpFlag = flag.Bool("h", false, "help")
)

Functions

func ClientIP added in v1.1.106

func ClientIP(c *gin.Context) string

客户端IP

func CreateToken

func CreateToken(appId string, openid int64) (tokenStr string, err error)

func DB

func DB(names ...string) *goo_db.Client

func ES added in v1.1.163

func ES() *goo_es.ESClient

func FlagInit added in v1.0.66

func FlagInit()

func GrpcContext added in v1.1.4

func GrpcContext(c *gin.Context) context.Context

func Handler

func Handler(controller iController) gin.HandlerFunc

定义控制器调用实现

func LoadConfig

func LoadConfig(yamlFile string, conf interface{}) (err error)

func Mongo added in v1.1.41

func Mongo(names ...string) *mongo.Database

func MongoClient added in v1.1.44

func MongoClient(names ...string) *goo_mongo.Client

func Redis

func Redis(names ...string) *goo_redis.Client

func RequestBody added in v1.1.106

func RequestBody(c *gin.Context) interface{}

请求数据

func RequestId added in v1.1.106

func RequestId(c *gin.Context) string

唯一ID

func ValidationMessage

func ValidationMessage(err error, messages map[string]string) string

Types

type Encryption added in v1.1.78

type Encryption struct {
	Key    string
	Secret string
}

func (*Encryption) Decode added in v1.1.78

func (enc *Encryption) Decode(str string) (b []byte, err error)

func (*Encryption) Encode added in v1.1.78

func (enc *Encryption) Encode(b []byte) (str string, err error)

type Env added in v1.0.2

type Env string
const (
	PRODUCTION  Env = "production"
	SIM         Env = "sim"
	TEST        Env = "test"
	DEVELOPMENT Env = "development"
)

func (Env) String added in v1.1.3

func (env Env) String() string

func (Env) Tag added in v1.1.19

func (env Env) Tag() string

type LocalUpload added in v1.1.4

type LocalUpload struct {
}

func (LocalUpload) Upload added in v1.1.4

func (lu LocalUpload) Upload(c *gin.Context, uploadDir string) *Response

type Option

type Option interface {
	// contains filtered or unexported methods
}

func CorsHeaderOption added in v1.1.4

func CorsHeaderOption(corsHeaders ...string) Option

跨域

func EnableEncryptionOption added in v1.1.14

func EnableEncryptionOption(encryptKey, encryptSecret string, excludeUris ...string) Option

启用加密传输

func EnvOption added in v1.1.4

func EnvOption(env Env) Option

运行环境

func NoAccessPathsOption

func NoAccessPathsOption(noAccessPaths ...string) Option

禁止访问的path

func NoLogPathsOption

func NoLogPathsOption(noLogPaths ...string) Option

不记录日志的path

func PProfEnableOption added in v1.1.4

func PProfEnableOption(pprofEnable bool) Option

开启分析

func ServerNameOption added in v1.0.43

func ServerNameOption(serverName string) Option

服务名称

type Response

type Response struct {
	Code    int32         `json:"code"`
	Message string        `json:"message"`
	Data    interface{}   `json:"data,omitempty"`
	Errors  []interface{} `json:"-"`
}

func Error

func Error(code int32, message string, v ...interface{}) *Response

func ErrorWithValidate added in v1.0.44

func ErrorWithValidate(err error, messages map[string]string) *Response

func Success

func Success(data interface{}) *Response

func (*Response) Copy added in v1.1.99

func (rsp *Response) Copy() *Response

func (*Response) String

func (rsp *Response) String() string

type Server added in v1.0.37

type Server struct {
	*gin.Engine
}

定义web服务

func NewServer

func NewServer(opt ...Option) *Server

func (*Server) Run added in v1.0.37

func (s *Server) Run(addr string)

启动服务

type Token

type Token struct {
	AppId     string `json:"app_id"`
	OpenId    int64  `json:"data"`
	NonceStr  string `json:"nonce"`
	Timestamp int64  `json:"ts"`
}

func ParseToken

func ParseToken(tokenStr, appId string) (token *Token, err error)

func (*Token) Bytes

func (t *Token) Bytes() []byte

func (*Token) String

func (t *Token) String() string

Jump to

Keyboard shortcuts

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