goo

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: May 30, 2022 License: Apache-2.0 Imports: 21 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

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

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

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.Orm

func FlagInit added in v1.0.66

func FlagInit()

func Handler

func Handler(controller iController) gin.HandlerFunc

定义控制器调用实现

func LoadConfig

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

func Redis

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

func ValidationMessage

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

Types

type Context

type Context struct {
	*gin.Context
	// contains filtered or unexported fields
}

func NewContext added in v1.0.43

func NewContext(c *gin.Context) (ctx *Context)

func (*Context) AbortWithStatusJSON added in v1.0.43

func (ctx *Context) AbortWithStatusJSON(code int, rsp *Response, v ...interface{})

中止并返回json数据

func (*Context) JSON added in v1.0.43

func (ctx *Context) JSON(code int, rsp *Response, v ...interface{})

返回json数据

type Env added in v1.0.2

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

type HandlerFunc added in v1.0.36

type HandlerFunc func(ctx *Context)

type Option

type Option struct {
	Name  string
	Value interface{}
}

func BaseDirOption added in v1.0.43

func BaseDirOption(v string) Option

根路径

func CorsHeadersOption added in v1.0.43

func CorsHeadersOption(v ...string) Option

跨域字段

func NewOption

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

func NoAccessPathsOption

func NoAccessPathsOption(v ...string) Option

不允许访问的路径

func NoLogPathsOption

func NoLogPathsOption(v ...string) Option

不记录日志的路径

func PProfEnable added in v1.0.53

func PProfEnable(flag bool) Option

性能分析

func ServerNameOption added in v1.0.43

func ServerNameOption(v string) Option

服务名称

type Response

type Response struct {
	Code    int32         `json:"code"`
	Message string        `json:"message"`
	Data    interface{}   `json:"data"`
	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) String

func (rsp *Response) String() string

type Server added in v1.0.37

type Server struct {
	*gin.Engine
	// contains filtered or unexported fields
}

定义web服务

func NewServer

func NewServer(opts ...Option) *Server

func (*Server) Router added in v1.0.38

func (s *Server) Router(fn func(s *Server)) *Server

路由

func (*Server) Run added in v1.0.37

func (s *Server) Run(addr string)

启动服务

func (*Server) Use added in v1.0.37

func (s *Server) Use(handlers ...HandlerFunc) *Server

中间件

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