goo

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: Apache-2.0 Imports: 19 Imported by: 1

README

定时任务

func main() {
	var wg sync.WaitGroup

	goo.Crond(1*time.Second, func() {
		fmt.Println(time.Now().Format("15:04:05"))
	})

	goo.Crond(3*time.Second, func() {
		fmt.Println(time.Now().Format("15:04:05"))
	})

	goo.CronMinute(func() {
		fmt.Println(time.Now().Format("15:04:05"))
	})

	goo.CronHour(func() {
		fmt.Println(time.Now().Format("15:04:05"))
	})

	goo.CronDay(func() {
		fmt.Println(time.Now().Format("15:04:05"))
	})

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

	wg.Wait()
}

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

This section is empty.

Functions

func CreateToken

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

func CronDay

func CronDay(fns ...func())

*

  • 每天执行一次

func CronHour

func CronHour(fns ...func())

*

  • 每小时执行一次

func CronMinute

func CronMinute(fns ...func())

*

  • 每分钟执行一次

func Crond

func Crond(d time.Duration, fns ...func())

*

  • 定期执行任务

func DB

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

func GRPCContext

func GRPCContext(ctx *Context) context.Context

func Handler

func Handler(controller iController) gin.HandlerFunc

定义控制器调用实现

func LoadConfig

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

func NewGRPCServer

func NewGRPCServer(cfg goo_grpc.Config) *goo_grpc.Server

func NewServer

func NewServer(opts ...Option) *server

func Redis

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

func ValidationMessage

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

Types

type Context

type Context struct {
	*gin.Context
}

type Env added in v1.0.2

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

type Option

type Option struct {
	Name  string
	Value interface{}
}

func CorsHeaderKeysOption

func CorsHeaderKeysOption(headerKeys ...string) Option

func NewOption

func NewOption(name string, value interface{}) Option

func NoAccessPathsOption

func NoAccessPathsOption(paths ...string) Option

func NoLogPathsOption

func NoLogPathsOption(paths ...string) Option

type Response

type Response struct {
	Status  int           `json:"status"`
	Code    int           `json:"code"`
	Message string        `json:"message"`
	Data    interface{}   `json:"data"`
	ErrMsg  []interface{} `json:"-"`
}

func Error

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

func Success

func Success(data interface{}) *Response

func (*Response) String

func (rsp *Response) String() string

type Token

type Token struct {
	AppId     string
	OpenId    int64
	NonceStr  string
	Timestamp int64
}

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