yago

package module
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2019 License: MIT Imports: 25 Imported by: 4

README

如果 go get 不动,请自行加梯子

export GOPROXY=https://goproxy.io
# 注意该梯子不支持私有repo,私有repo请去掉GOPROXY
export GOPROXY=

依赖

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

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

开始

// 1. 获取 yago
go get github.com/hulklab/yago/yago

// 2. 用 yago 在当前目录创建你的项目 myapp
yago init -a myapp

// 3. 进入目录初始化
cd myapp/
go mod init

// 4. build
go build

// 5. 启动
./myapp -c conf/app.toml

// 5.1. 创建属于自己的配置文件,并启动
sh env.init.sh yourname
./myapp

// 6. 控制是否需要在此机器上开启 task 任务,有两种方式
// 修改配置文件中的 app.task_enable,默认为开启
// 修改环境变量 export {{配置文件中的app_name}}_APP_TASK_ENABLE=1, 1 表示开启,0 表示关闭,配置文件与环境变量同时存在时环境变量生效

目录结构

├── app
│   ├── g
│   │   └── errors.go
│   ├── modules
│   │   └── home
│   │       ├── homecmd
│   │       │   └── home.go
│   │       ├── homedao
│   │       │   └── home.go
│   │       ├── homehttp
│   │       │   └── home.go
│   │       ├── homemodel
│   │       │   └── home.go
│   │       ├── homerpc
│   │       │   ├── home.go
│   │       │   ├── home_test.go
│   │       │   │   └── homepb
│   │       │   │       ├── home.pb.go
│   │       │   │       └── home.proto
│   │       │   └── README.md
│   │       └── hometask
│   │           └── home.go
│   ├── route
│   │   ├── route.go
│   └── third
│       └── homeapi
│           ├── home.go
│           └── protobuf
│               └── homepb
│                   ├── home.pb.go
│                   └── home.proto
├── conf
│   └── app.toml
├── main.go
└── tools
    └── build.sh

路由

1. http 路由
@reference example/app/modules/home/homehttp/home.go
2. cmd 路由
@reference example/app/modules/home/homecmd/home.go
3. task 路由
@reference example/app/modules/home/hometask/home.go
4. rpc 路由
@reference example/app/modules/home/homerpc/home.go

配置

  1. 位置: conf/app.toml
  2. 解析: conf.go
  3. 使用: @reference libs/orm/orm.go line 29

组件

  1. 全局容器: com.go
  2. 使用: @reference libs/rds/redis_test.go

模块

1. 模块基础目录
dao model http rpc task cmd
2. 创建新模块

在项目根目录下,使用yago创建模块 新创建的模块会自动将路由加载到myapp/routes中

cd myapp
yago new -m newmodule

错误

# 系统级错误定义处
error.go
# 使用
@reference example/app/modules/homehttp/home.go::AddAction

Third

  1. 目录规范 @see example/app/third
  2. http-api 使用样例 @reference example/app/third/homeapi/home.go

Goland 使用 mod

  1. Preferences -> Go -> Go modules(vgo)
  2. 如果还有标红的提示,点击 Sync packages

Documentation

Index

Constants

View Source
const (
	// 1-1000 系统错误, 1000 - 9999 业务公共错误, 10000 - .... 业务错误
	OK           = Err("0")
	E            = Err("1=") // 自定义错误信息
	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")
)

Variables

View Source
var CmdRouterMap = make(map[string]*CmdRouter)
View Source
var Component = new(Components)
View Source
var GlobalCloseChan = make(chan int)
View Source
var HttpRouterMap = make(map[string]*HttpRouter)
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 ...CmdArg)

func AddHttpRouter

func AddHttpRouter(url, method string, action HttpHandlerFunc, h HttpInterface)

func AddTaskRouter

func AddTaskRouter(spec string, action TaskHandlerFunc)

func Hostname

func Hostname() string

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

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

	// 开启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

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 struct {
	Name      string
	Shorthand string
	Value     string
	Usage     string
	Required  bool
}

type CmdHandlerFunc

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

cmd

type CmdRouter

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

type Components

type Components struct {
	// contains filtered or unexported fields
}

func (*Components) Ins

func (c *Components) Ins(key string, f func() interface{}) interface{}

type Ctx

type Ctx struct {
	*gin.Context
}

func NewCtx

func NewCtx(c *gin.Context) *Ctx

func (*Ctx) RequestBool

func (c *Ctx) RequestBool(key string, def ...bool) bool

func (*Ctx) RequestFileContent added in v1.1.4

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

func (*Ctx) RequestFloat64

func (c *Ctx) RequestFloat64(key string, def ...float64) (float64, error)

func (*Ctx) RequestInt

func (c *Ctx) RequestInt(key string, def ...int) (int, error)

func (*Ctx) RequestInt64

func (c *Ctx) RequestInt64(key string, def ...int64) (int64, error)

func (*Ctx) RequestSliceInt

func (c *Ctx) RequestSliceInt(key string, def ...int) []int

func (*Ctx) RequestSliceInt64

func (c *Ctx) RequestSliceInt64(key string, def ...int64) []int64

func (*Ctx) RequestSliceString

func (c *Ctx) RequestSliceString(key string, def ...string) []string

func (*Ctx) RequestString

func (c *Ctx) RequestString(key string, def ...string) string

func (*Ctx) SetData

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

func (*Ctx) SetDataOrErr

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

func (*Ctx) SetError

func (c *Ctx) SetError(err Err, msgEx ...string)

func (*Ctx) Validate

func (c *Ctx) Validate() error

type Err

type Err string

func NewErr

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

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

func (Err) Error

func (e Err) Error() string

func (Err) GetError

func (e Err) GetError() (int, string)

func (Err) HasErr

func (e Err) HasErr() bool

type HttpHandlerFunc

type HttpHandlerFunc func(c *Ctx)

type HttpInterface

type HttpInterface interface {
	Labels() validator.Label
	Rules() []validator.Rule
	BeforeAction(c *Ctx) Err
}

type HttpRouter

type HttpRouter struct {
	Url    string
	Method string
	Action HttpHandlerFunc
	// contains filtered or unexported fields
}

http

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
}

Directories

Path Synopsis
common
Code generated by yago.
Code generated by yago.
app/modules/home/homerpc/homepb
Package app_homepb is a generated protocol buffer package.
Package app_homepb is a generated protocol buffer package.
app/third/homeapi/homepb
Package app_homepb is a generated protocol buffer package.
Package app_homepb is a generated protocol buffer package.
libs
arr
orm
rds
str

Jump to

Keyboard shortcuts

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