yago

package module
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2020 License: MIT Imports: 36 Imported by: 4

README

Yago Scaffold - Web开发脚手架

avatar

目录

文档

Yago 文档

安装

首先你需要安装 Go (version 1.11+), 然后执行go get 安装yago
 go get github.com/hulklab/yago/yago

依赖

go >= 1.11(由于使用了 go mod 管理版本依赖)

如果想在GOPATH下用mod, 请设置 GO111MODULE=on 则在 GOPATH/src 目录下使用 go get 时也默认采用 go mod
export GO111MODULE=on

快速开始

1. 用 yago 在当前目录创建你的项目 myapp
yago init -a myapp
2. 进入目录初始化
cd myapp/
go mod init
3. 构建
go build
4. 创建属于自己的配置文件,并启动
sh env.init.sh yourname
./myapp
5. 控制是否需要在此机器上开启 task 任务,有两种方式
  • 修改配置文件中的 app.task_enable,默认为开启
  • 修改环境变量 export {{配置文件中的app_name}}_APP_TASK_ENABLE=1, 1 表示开启,0 表示关闭,配置文件与环境变量同时存在时环境变量生效
Goland 配置 mod
  1. Preferences -> Go -> Go modules(vgo)

  1. 如果还有标红的提示,点击 Sync packages

感谢

gin cron cobra xorm logrus beego

Documentation

Index

Constants

View Source
const (
	ResponseKey = "__Resp__"
	ErrorKey    = "__Error__"
)

Variables

View Source
var (
	E            = Err("1=") // custom error
	ErrParam     = Err("2=")
	ErrSign      = Err("3=Sign failed")
	ErrAuth      = Err("4=Auth failed")
	ErrForbidden = Err("5=Forbidden")
	ErrNotLogin  = Err("6=User not login")
	ErrSystem    = Err("7=System error")
	ErrOperate   = Err("8=")
	ErrUnknown   = Err("9=Unknown error")
)
View Source
var CmdRouterMap = make(map[string]*CmdRouter)
View Source
var Component = new(components)
View Source
var RpcServer *grpc.Server
View Source
var TaskCloseChan = make(chan int)
View Source
var TaskRouterList []*TaskRouter

Functions

func AddCmdRouter

func AddCmdRouter(use, short string, action CmdHandlerFunc, args ...ICmdArg)

func AddHttpRouter

func AddHttpRouter(url, method string, action HttpHandlerFunc, md ...interface{})

func AddTaskRouter

func AddTaskRouter(spec string, action TaskHandlerFunc)

func GetAppName added in v1.4.0

func GetAppName() string

func GetLang added in v1.3.0

func GetLang() string

func GetTranslator added in v1.3.0

func GetTranslator(lang ...string) ut.Translator

func Hostname

func Hostname() string

func HttpUse added in v1.6.0

func HttpUse(middleware ...HttpHandlerFunc)

func IsEnvDev added in v1.1.2

func IsEnvDev() bool

func IsEnvProd added in v1.1.2

func IsEnvProd() bool

func RestartApp added in v1.1.8

func RestartApp() error

func SetHttpNoRouter added in v1.2.9

func SetHttpNoRouter(action HttpHandlerFunc)

func WrapErr added in v1.5.0

func WrapErr(ye Err, err error) error

返回业务报错(业务报错给接口展示),包裹系统错误(系统错误转到日志记录)

Types

type App

type App struct {
	// 是否开启debug模式
	DebugMode bool

	// http run mode
	HttpRunMode string
	// 开启http服务
	HttpEnable bool
	// http路由配置
	HttpRouters []*HttpRouter

	// https 证书配置
	HttpSslOn    bool
	HttpCertFile string
	HttpKeyFile  string
	// http html 模版配置
	HttpViewRender bool
	HttpViewPath   string
	HttpStaticPath string
	// http cors 跨域配置
	HttpCorsAllowAllOrigins  bool
	HttpCorsAllowOrigins     []string
	HttpCorsAllowMethods     []string
	HttpCorsAllowHeaders     []string
	HttpCorsExposeHeaders    []string
	HttpCorsAllowCredentials bool
	HttpCorsMaxAge           time.Duration
	// http gzip 压缩
	HttpGzipOn    bool
	HttpGzipLevel int
	// http pprof
	HttpPprof bool

	// 开启task服务
	TaskEnable bool
	// task路由配置
	TaskRouters []*TaskRouter

	// rpc
	RpcEnable bool
	// contains filtered or unexported fields
}

func NewApp

func NewApp() *App

func (*App) Close added in v1.1.8

func (a *App) Close()

func (*App) Run

func (a *App) Run()

type AppConfig

type AppConfig struct {
	*viper.Viper
}
var Config *AppConfig

func NewAppConfig

func NewAppConfig(cfgPath string) *AppConfig

func (*AppConfig) Init added in v1.5.3

func (c *AppConfig) Init()

func (*AppConfig) ReadFileConfig added in v1.4.5

func (c *AppConfig) ReadFileConfig(cfgPath string) error

type Cmd

type Cmd struct {
	*cobra.Command
}

func NewCmd

func NewCmd() *Cmd

func (*Cmd) LoadCmdRouter

func (c *Cmd) LoadCmdRouter()

func (*Cmd) RunCmd

func (c *Cmd) RunCmd()

type CmdArg

type CmdArg = CmdStringArg

type CmdBoolArg added in v1.2.6

type CmdBoolArg struct {
	Name      string
	Shorthand string
	Usage     string
	Required  bool
	Value     bool
}

func (CmdBoolArg) SetFlag added in v1.2.6

func (c CmdBoolArg) SetFlag(cmd *cobra.Command)

type CmdDurationArg added in v1.2.6

type CmdDurationArg struct {
	Name      string
	Shorthand string
	Usage     string
	Required  bool
	Value     time.Duration
}

func (CmdDurationArg) SetFlag added in v1.2.6

func (c CmdDurationArg) SetFlag(cmd *cobra.Command)

type CmdFloat64Arg added in v1.2.6

type CmdFloat64Arg struct {
	Name      string
	Shorthand string
	Usage     string
	Required  bool
	Value     float64
}

func (CmdFloat64Arg) SetFlag added in v1.2.6

func (c CmdFloat64Arg) SetFlag(cmd *cobra.Command)

type CmdHandlerFunc

type CmdHandlerFunc func(cmd *cobra.Command, args []string)

cmd

type CmdInt64Arg added in v1.2.6

type CmdInt64Arg struct {
	Name      string
	Shorthand string
	Usage     string
	Required  bool
	Value     int64
}

func (CmdInt64Arg) SetFlag added in v1.2.6

func (c CmdInt64Arg) SetFlag(cmd *cobra.Command)

type CmdIntArg added in v1.2.6

type CmdIntArg struct {
	Name      string
	Shorthand string
	Usage     string
	Required  bool
	Value     int
}

func (CmdIntArg) SetFlag added in v1.2.6

func (c CmdIntArg) SetFlag(cmd *cobra.Command)

type CmdIntSliceArg added in v1.2.6

type CmdIntSliceArg struct {
	Name      string
	Shorthand string
	Usage     string
	Required  bool
	Value     []int
}

func (CmdIntSliceArg) SetFlag added in v1.2.6

func (c CmdIntSliceArg) SetFlag(cmd *cobra.Command)

type CmdRouter

type CmdRouter struct {
	Use    string
	Short  string
	Action CmdHandlerFunc
	Args   []ICmdArg
}

type CmdStringArg added in v1.2.6

type CmdStringArg struct {
	Name      string
	Shorthand string
	Usage     string
	Required  bool
	Value     string
}

func (CmdStringArg) SetFlag added in v1.2.6

func (c CmdStringArg) SetFlag(cmd *cobra.Command)

type CmdStringSliceArg added in v1.2.6

type CmdStringSliceArg struct {
	Name      string
	Shorthand string
	Usage     string
	Required  bool
	Value     []string
}

func (CmdStringSliceArg) SetFlag added in v1.2.6

func (c CmdStringSliceArg) SetFlag(cmd *cobra.Command)

type Ctx

type Ctx struct {
	*gin.Context
}

func (*Ctx) Copy added in v1.5.0

func (c *Ctx) Copy() *Ctx

func (*Ctx) GetError added in v1.5.0

func (c *Ctx) GetError() error

func (*Ctx) GetFileContent added in v1.4.0

func (c *Ctx) GetFileContent(key string) ([]byte, error)

func (*Ctx) GetResponse added in v1.1.9

func (c *Ctx) GetResponse() (*ResponseBody, bool)

func (*Ctx) SetData

func (c *Ctx) SetData(data interface{})

func (*Ctx) SetDataOrErr

func (c *Ctx) SetDataOrErr(data interface{}, err interface{})

func (*Ctx) SetError

func (c *Ctx) SetError(err interface{})

type Err

type Err string

func NewErr

func NewErr(err interface{}, args ...interface{}) Err

生成通用错误, 接受通用的 error 类型或者是 string 类型 eg. yago.NewErr(yago.Forbidden, "you are not permitted") eg. yago.NewErr(errors.New("err occur")) eg. yago.NewErr("something is error") eg. yago.NewErr("%s is err","query")

func (Err) Code added in v1.1.9

func (e Err) Code() int

func (Err) Error

func (e Err) Error() string

func (Err) HasErr

func (e Err) HasErr() bool

Deprecated

func (Err) String added in v1.1.9

func (e Err) String() string

type HttpGlobalMiddleware added in v1.6.0

type HttpGlobalMiddleware []HttpHandlerFunc

func (*HttpGlobalMiddleware) Use added in v1.6.0

func (r *HttpGlobalMiddleware) Use(middleware ...HttpHandlerFunc)

type HttpGroupRouter added in v1.6.0

type HttpGroupRouter struct {
	Prefix         string
	GinGroup       *gin.RouterGroup
	Middleware     []HttpHandlerFunc
	HttpRouterList []*HttpRouter
	Parent         *HttpGroupRouter
	Children       map[string]*HttpGroupRouter
}

func NewHttpGroupRouter added in v1.6.0

func NewHttpGroupRouter(prefix string) *HttpGroupRouter

func (*HttpGroupRouter) Any added in v1.6.0

func (g *HttpGroupRouter) Any(url string, action HttpHandlerFunc, md ...interface{})

func (*HttpGroupRouter) Delete added in v1.6.0

func (g *HttpGroupRouter) Delete(url string, action HttpHandlerFunc, md ...interface{})

func (*HttpGroupRouter) Get added in v1.6.0

func (g *HttpGroupRouter) Get(url string, action HttpHandlerFunc, md ...interface{})

func (*HttpGroupRouter) Group added in v1.6.0

func (g *HttpGroupRouter) Group(prefix string) *HttpGroupRouter

func (*HttpGroupRouter) Head added in v1.6.0

func (g *HttpGroupRouter) Head(url string, action HttpHandlerFunc, md ...interface{})

func (*HttpGroupRouter) Options added in v1.6.0

func (g *HttpGroupRouter) Options(url string, action HttpHandlerFunc, md ...interface{})

func (*HttpGroupRouter) Patch added in v1.6.0

func (g *HttpGroupRouter) Patch(url string, action HttpHandlerFunc, md ...interface{})

func (*HttpGroupRouter) Post added in v1.6.0

func (g *HttpGroupRouter) Post(url string, action HttpHandlerFunc, md ...interface{})

func (*HttpGroupRouter) Put added in v1.6.0

func (g *HttpGroupRouter) Put(url string, action HttpHandlerFunc, md ...interface{})

func (*HttpGroupRouter) Use added in v1.6.0

func (g *HttpGroupRouter) Use(middleware ...HttpHandlerFunc)

type HttpHandlerFunc

type HttpHandlerFunc func(c *Ctx)

http

type HttpRouter

type HttpRouter struct {
	Group    *HttpGroupRouter
	Path     string
	Method   string
	Action   HttpHandlerFunc
	Metadata interface{}
}

func GetHttpRouters added in v1.6.0

func GetHttpRouters() []*HttpRouter

func (*HttpRouter) Url

func (h *HttpRouter) Url() string

type ICmdArg added in v1.2.6

type ICmdArg interface {
	SetFlag(cmd *cobra.Command)
}

type ResponseBody

type ResponseBody struct {
	ErrNo  int         `json:"errno"`
	ErrMsg string      `json:"errmsg"`
	Data   interface{} `json:"data"`
}

type TaskHandlerFunc

type TaskHandlerFunc func()

task

type TaskRouter

type TaskRouter struct {
	Spec   string
	Action TaskHandlerFunc
}

Jump to

Keyboard shortcuts

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