bird

package module
v0.0.0-...-ac7d4c8 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 21 Imported by: 0

README

bird web 开发脚手架

功能
  • 日志记录
  • 队列任务
  • 定时任务
  • 配置工具
  • 简易权限
签名中间件用法

header 请求参数

  • header['Sign'] = 签名
签名算法 Sign=key+token
组成部分 数据长度 说明
key 10~40 个字符 唯一值
token 64 个字符 sha256(key+secret)
  • 注 secret 由后端提供
let secret="adf78asdf5678adf456asdf3456adsf456asf";
let key=rand(10,20); //字符数可不固定
let token=sha256(key+secret) //64位字符
header['Sign']=key+token;
结果有可能为  3d970875ca2c25abbe2bfc3683db80bb170201ef5d8f4449fd06812f53dc3d970875ca2c25abbe2bfc3683db807a81

config 的 tag 说明

  • image 图片上传
  • image-qrcode 二维码图片上传

自动配置结构: mode类型 选择类型的: - none 不显示 - single 单选 - select-show-desc 多选,选中后将desc展示出来 该类型返回结构:[ { desc: "http://www.miaopay.nczhike.net" label: "秒支付测试" value: 5 } ]

Documentation

Index

Constants

View Source
const (
	BIRD_CLASS_FREE    = 1 //系统(自由鸟)
	BIRD_CLASS_ROUTER  = 2 //路由
	BIRD_CLASS_QUEUE   = 3 //队列
	BIRD_CLASS_CRONTAB = 4 //定时任务
	BIRD_CLASS_EVENT   = 5 //钩子
)

划分鸟类

View Source
const (
	LOG_DEBUG = 1
	LOG_INFO  = 2
	LOG_WARN  = 3
	LOG_FATAL = 4
)
View Source
const (
	FORMAT_DATE     = "2006-01-02"
	FORMAT_DATETIME = "2006-01-02 15:04:05"
)

日期格式

View Source
const (
	CONFIG_DIR  = "./config"
	RUNTIME_DIR = "./runtime"
)
View Source
const (
	HOOK_SYNC  = 0 //同步(默认)
	HOOK_ASYNC = 1 //异步
)
View Source
const GIN_KEY_BIRD = "gin_key_bird"

存鸟类的key

Variables

View Source
var OrmEngine *xorm.Engine

Functions

func ConfigDomain

func ConfigDomain() *configDomain

配置处理域

func CoreDomain

func CoreDomain() *coreDomain

func CrontabDomain

func CrontabDomain() *crontabDomain

定时任务功能域

func DictDomain

func DictDomain() *dictDomain

func ExceptionDomain

func ExceptionDomain() *exceptionDomain

func GetBirdClassName

func GetBirdClassName(classId int, name string) (string, string)

获取鸟类名

func GetConfig

func GetConfig() conf

func HookDomain

func HookDomain() *hookDomain

func Md5

func Md5(args ...interface{}) string

加盐md5

func ModuleDomain

func ModuleDomain() *moduleDomain

模块处理域

func PermissionDomain

func PermissionDomain() *permissionDomain

权限处理域

func QueueDomain

func QueueDomain() *queueDomain

func RouteDomain

func RouteDomain() *routeDomain

func SetLogHandler

func SetLogHandler(l Logger)

设置日志处理器

func SetQueueTaskStore

func SetQueueTaskStore(t func(b *Bird) TaskStoreInterface)

设置队列任务获取器

func Sha1

func Sha1(args ...interface{}) string

加盐sha1

Types

type AppInterface

type AppInterface interface {
	Init(*gin.Engine)                                 //站点初始化执行方法(可以放一些中间件处理的东西)
	Modules() []ModuleInterface                       //加载需要的模块
	PermissionHandler(*gin.Context, *Permission) bool //权限处理(使用Throw中断)
	Install(*Bird)                                    //安装入口
	Debug() func()                                    //调试方法,如果此方法不为空,则不启动web服务且优先执行,专门用于紧急调试使用
}

应用接口

type BaseQuery

type BaseQuery struct {
	Page  int `bind:"page" form:"page"`
	Limit int `bind:"limit" form:"limit"`
}

查询结构

type Bird

type Bird struct {
	orm.Engine          //集成orm引擎
	Context    *Context //请求上下文(如果不是http请求则为nil)

	sync.Mutex
	// contains filtered or unexported fields
}

鸟上下文,所有控制器部分都贯穿着这只鸟,建议所有模型、组件都继承这只鸟 可以更方便实现事务的同步

func NewBird

func NewBird() *Bird

创建一只鸟,方便单独使用

func (*Bird) ConfigInit

func (this *Bird) ConfigInit(confPtr ConfigInterface) ConfigInterface

初始化配置,并自动获取配置(为避免程序运行中修改配置,获取的对象值为克隆值)

func (*Bird) LogDebug

func (this *Bird) LogDebug(msg string, arg ...interface{})

给鸟类增加日志保存处理

func (*Bird) LogDebugf

func (this *Bird) LogDebugf(msg string, fomrat string, arg ...interface{})

func (*Bird) LogFatal

func (this *Bird) LogFatal(msg string, arg ...interface{})

func (*Bird) LogFatalf

func (this *Bird) LogFatalf(msg string, fomrat string, arg ...interface{})

func (*Bird) LogInfo

func (this *Bird) LogInfo(msg string, arg ...interface{})

func (*Bird) LogInfof

func (this *Bird) LogInfof(msg string, fomrat string, arg ...interface{})

func (*Bird) LogWarn

func (this *Bird) LogWarn(msg string, arg ...interface{})

func (*Bird) LogWarnf

func (this *Bird) LogWarnf(msg string, fomrat string, arg ...interface{})

func (*Bird) SetHandler

func (this *Bird) SetHandler(name string) *Bird

设置操作人

func (*Bird) Single

func (this *Bird) Single(name string, createFunc func() interface{}) interface{}

单例对象

type ComponentInterface

type ComponentInterface interface {
	Models() []ModelInterface //数据库模型
}

组件接口

type ConfigInfo

type ConfigInfo struct {
	ModuleName string `json:"moduleName"` //模块名
	Name       string `json:"name"`       //配置名称
	Alias      string `json:"alias"`      //配置别名
	Filename   string `json:"file"`       //文件
	// contains filtered or unexported fields
}

查询结构

type ConfigInterface

type ConfigInterface interface {
	Default() ConfigInterface //默认配置
	Name() string             //配置名称
	Alias() string            //配置别名
	Validate() error          //验证
}

配置接口

type Configer

type Configer interface {
	Config() ConfigInterface
}

配置组件接口(用于判断组件是否包含配置)

type Context

type Context struct {
	*Bird   //继承鸟类
	Context *gin.Context
}

升级版context

func NewContext

func NewContext(c *gin.Context) *Context

包装一个升级版的context 继承了小鸟、gin的context

func (*Context) BindCookie

func (this *Context) BindCookie(objectPtr interface{}) *Context

绑定cookie

func (*Context) BindForm

func (this *Context) BindForm(objectPtr interface{}) *Context

绑定form

func (*Context) BindHead

func (this *Context) BindHead(objectPtr interface{}) *Context

绑定head

func (*Context) BindJSON

func (this *Context) BindJSON(objectPtr interface{}) *Context

绑定json

func (*Context) BindQuery

func (this *Context) BindQuery(objectPtr interface{}) *Context

绑定query

func (*Context) Code

func (this *Context) Code(code int)

只返回状态码

func (*Context) JSON

func (this *Context) JSON(code int, res interface{}, msg string)

json输出

func (*Context) JWTClaims

func (this *Context) JWTClaims() *middleware.Claims

获取jwt的Claims对象

func (*Context) Msg

func (this *Context) Msg(code int, msg string)

错误返回(只输出msg)

func (*Context) MultiHandler

func (this *Context) MultiHandler(key string, expireMinutes, times int, fn func())

当key在有效期expireMinutes内被调用了times次则执行fn

func (*Context) Res

func (this *Context) Res(code int, data interface{})

成功返回(只输出data)

func (*Context) Session

func (this *Context) Session() sessions.Session

获取session对象

func (*Context) UniqueHandler

func (this *Context) UniqueHandler(key string, fn func())

session单例处理器,可以处理阅读、点击之类的数据 如果session里面未保存key,则执行fn

type CrontabInfo

type CrontabInfo struct {
	ModuleName string `json:"moduleName"`
	Name       string `json:"name"`       //名称
	Expression string `json:"expression"` //表达式
	// contains filtered or unexported fields
}

查询结构

type CrontabInterface

type CrontabInterface interface {
	Name() string       //任务名称
	Expression() string //定时时间表达式
	Handler(*Bird)      //执行方法
}

定时任务接口

type Dict

type Dict struct {
	Alias      string      `json:"alias"`      //字典别名
	Name       string      `json:"name"`       //字典名称
	ModuleName string      `json:"moduleName"` //字典所在模块名
	Items      []*DictItem `json:"items"`      //字典
}

字典

func (*Dict) AddItem

func (this *Dict) AddItem(constName string, label string, value interface{}, desc string) *Dict

添加元素

func (*Dict) Label

func (this *Dict) Label(value interface{}) string

获取label名

type DictItem

type DictItem struct {
	Const string      `json:"const"` //常量名
	Label string      `json:"label"` //名称
	Value interface{} `json:"value"` //值
	Desc  string      `json:"desc"`  //描述
}

字典列表

type EventInfo

type EventInfo struct {
	HookId     string `json:"hookId"`
	Name       string `json:"name"` //事件名称
	ModuleName string `json:"moduleName"`
	// contains filtered or unexported fields
}

查询结构

type EventInterface

type EventInterface interface {
	HookId() string                       //事件id
	Handler(b *Bird, args ...interface{}) //事件处理方法
}

事件接口

type Exception

type Exception = try.Exception

异常接口

type ExceptionBind

type ExceptionBind struct {
	Field string
	try.ExceptionBase
}

数据绑定错误

func (*ExceptionBind) Name

func (this *ExceptionBind) Name() string

type ExceptionInfo

type ExceptionInfo struct {
	Id         string `json:"id"`         //异常id
	Name       string `json:"name"`       //异常名称
	Code       int    `json:"code"`       //返回的错误代码
	LogLevel   int    `json:"logLevel"`   //日志记录级别
	ModuleName string `json:"moduleName"` //模块名称
}

异常类型

type ExceptionJWT

type ExceptionJWT struct {
	try.ExceptionBase
}

jwt异常【未登录】

func (*ExceptionJWT) Name

func (this *ExceptionJWT) Name() string

type ExceptionPermission

type ExceptionPermission struct {
	try.ExceptionBase
}

权限错误

func (*ExceptionPermission) Name

func (this *ExceptionPermission) Name() string

type H

type H map[string]X

hash

type Hook

type Hook struct {
	Id       string `json:"id"`   //钩子id
	Name     string `json:"name"` //钩子名称
	Mode     int    `json:"mode"` //钩子类型(1=同步,2=异步)
	ArgsDesc string `json:"desc"` //参数说明
}

钩子

func (*Hook) Put

func (this *Hook) Put(args ...interface{})

放置钩子,如果是异步钩子则开票协程执行

func (*Hook) PutOnBird

func (this *Hook) PutOnBird(b *Bird, args ...interface{})

type HookInfo

type HookInfo struct {
	HookId     string       `json:"hookId"`     //编号
	Name       string       `json:"name"`       //名称
	ModuleName string       `json:"moduleName"` //所属模块名
	Mode       int          `json:"mode"`       //钩子类型
	ModeName   string       `json:"modeName"`   //钩子类型
	ArgsDesc   string       `json:"argsDesc"`   //参数说明
	Events     []*EventInfo `json:"events"`     //已经注册的事件
}

查询结构

type JWT

type JWT struct {
	middleware.JWT
}

jwt处理器,本文件的JWT结构用于应用层直接继承使用

func (*JWT) Duration

func (this *JWT) Duration() time.Duration

登录时间

func (*JWT) Error

func (this *JWT) Error(*gin.Context)

func (*JWT) Issuer

func (this *JWT) Issuer() string

发行者

func (*JWT) Secret

func (this *JWT) Secret() string

设置密匙

type Log

type Log struct {
	BirdId    int64       //保证是一个请求发起的
	Msg       string      //日志标题
	BirdClass int         //鸟类
	BirdName  string      //鸟名
	Level     int         //日志等级
	Handler   string      //操作人
	IP        string      //操作ip
	Data      interface{} //错误内容
}

日志结构

type Logger

type Logger interface {
	Save(log *Log)
}

日志处理器接口

type ModelInterface

type ModelInterface interface {
	TableName() string
}

模型接口

type ModuleInfo

type ModuleInfo struct {
	Name       string `json:"name"`
	Components int    `json:"components"` //组件数量
	Dicts      int    `json:"dicts"`      //字典数量
	Events     int    `json:"events"`     //注册事件数量
	Hooks      int    `json:"hooks"`      //钩子数量
	Tables     int    `json:"tables"`     //数据库数量
	Configs    int    `json:"configs"`    //配置列表
	// contains filtered or unexported fields
}

type ModuleInterface

type ModuleInterface interface {
	Install(*Bird)
	ModuleName() string               //模块名称
	Init(*gin.Engine)                 //模块初始化,此处可放置路由
	Exceptions() []Exception          //异常列表
	Crontabs() []CrontabInterface     //定时任务
	Queues() []QueueInterface         //队列
	Components() []ComponentInterface //组件
	Hooks() []*Hook                   //钩子列表(注册后的事件才能使用)
	Events() []EventInterface         //事件列表(自动寻找对应钩子)
	Dicts() []*Dict                   //数据字典
}

模块接口

type Permission

type Permission struct {
	Id     string        `json:"id"`     //权限id
	Pid    string        `json:"pid"`    //权限上级id
	Name   string        `json:"name"`   //权限名
	Childs []*Permission `json:"childs"` //子权限
}

权限

func (*Permission) Child

func (this *Permission) Child(id string, name string) *Permission

生成子权限 id不能重复

func (*Permission) Watch

func (this *Permission) Watch(fn gin.HandlerFunc) gin.HandlerFunc

监视并拦截,fn可以为nil

type QueueInfo

type QueueInfo struct {
	ModuleName string `json:"moduleName"`
	Alias      string `json:"alias"` //队列别名
	Name       string `json:"name"`  //名称
	// contains filtered or unexported fields
}

查询结构

type QueueInterface

type QueueInterface interface {
	Name() string         //任务名称【唯一】
	Alias() string        //任务别名【唯一】
	Handler(*Bird, *Task) //执行方法
}

队列任务接口

type RouteInfo

type RouteInfo struct {
	Method string `json:"method"`
	Path   string `json:"path"`
}

type Task

type Task struct {
	QueueId    int64  //队列id
	QueueAlias string //队列别名
	Arg        string //参数
	Err        string //错误
}

队列任务

func (*Task) GetArgument

func (this *Task) GetArgument(ptr interface{})

反序列化参数,ptr=参数指针

type TaskStoreInterface

type TaskStoreInterface interface {
	TaskGet() *Task        //获取一个话题的任务
	TaskDone(*Bird, *Task) //完成一个任务
	TaskErr(*Bird, *Task)  //任务失败
}

队列获取器接口

type X

type X interface{}

任意类型

Directories

Path Synopsis
assets
config
配置文件的读取和写入 目前仅支持json格式配置文件
配置文件的读取和写入 目前仅支持json格式配置文件
orm
异常处理
异常处理

Jump to

Keyboard shortcuts

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