goo

package
v1.1.4 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: 22 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 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 Redis

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

func ValidationMessage

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

Types

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

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 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 Router added in v1.1.4

func Router() Option

不记录日志的path

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) 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(opt ...Option) *Server

func (*Server) Run added in v1.0.37

func (s *Server) Run(addr 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