Documentation
¶
Overview ¶
Package app 核心功能的实现
Index ¶
- type App
- func (app *App) AddCompresses(m map[string]compress.WriterFunc) error
- func (app *App) AddMessages(status int, msgs map[int]string)
- func (app *App) AddMiddlewares(m middleware.Middleware)
- func (app *App) AddService(f ServiceFunc, title string)
- func (app *App) Close() error
- func (app *App) Delete(path string, h http.Handler) *mux.Prefix
- func (app *App) DeleteFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix
- func (app *App) ErrorHandlers() *errorhandler.ErrorHandler
- func (app *App) Get(path string, h http.Handler) *mux.Prefix
- func (app *App) GetFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix
- func (app *App) Handle(path string, h http.Handler, methods ...string) error
- func (app *App) HandleFunc(path string, h func(w http.ResponseWriter, r *http.Request), methods ...string) error
- func (app *App) IsDebug() bool
- func (app *App) LocalPrinter(tag language.Tag, opts ...xmessage.Option) *xmessage.Printer
- func (app *App) Location() *time.Location
- func (app *App) Logs() *logs.Logs
- func (app *App) Messages(p *xmessage.Printer) map[int]string
- func (app *App) Mimetypes() *mimetype.Mimetypes
- func (app *App) Mux() *mux.Mux
- func (app *App) NewResult(code int) Result
- func (app *App) Patch(path string, h http.Handler) *mux.Prefix
- func (app *App) PatchFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix
- func (app *App) Path(p string) string
- func (app *App) Post(path string, h http.Handler) *mux.Prefix
- func (app *App) PostFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix
- func (app *App) Prefix(prefix string) *mux.Prefix
- func (app *App) Put(path string, h http.Handler) *mux.Prefix
- func (app *App) PutFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix
- func (app *App) Run() (err error)
- func (app *App) Scheduled() *scheduled.Server
- func (app *App) Services() []*Service
- func (app *App) Shutdown(ctx context.Context) error
- func (app *App) URL(path string) string
- func (app *App) Uptime() time.Time
- type GetResultFunc
- type JobFunc
- type Result
- type Service
- type ServiceFunc
- type ServiceState
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type App ¶
App 程序运行实例
func (*App) AddCompresses ¶ added in v0.25.0
func (app *App) AddCompresses(m map[string]compress.WriterFunc) error
AddCompresses 添加压缩处理函数
func (*App) AddMessages ¶ added in v0.26.0
AddMessages 添加一组错误信息。
status 指定了该错误代码反馈给客户端的 HTTP 状态码; msgs 中,键名表示的是该错误的错误代码; 键值表示具体的错误描述内容。
func (*App) AddMiddlewares ¶
func (app *App) AddMiddlewares(m middleware.Middleware)
AddMiddlewares 设置全局的中间件,可多次调用。
func (*App) AddService ¶ added in v0.27.0
func (app *App) AddService(f ServiceFunc, title string)
AddService 添加新的服务
f 表示服务的运行函数; title 是对该服务的简要说明。
func (*App) DeleteFunc ¶ added in v0.27.0
DeleteFunc 指定一个 Delete 请求
func (*App) ErrorHandlers ¶ added in v0.21.0
func (app *App) ErrorHandlers() *errorhandler.ErrorHandler
ErrorHandlers 错误处理功能
func (*App) HandleFunc ¶ added in v0.27.0
func (app *App) HandleFunc(path string, h func(w http.ResponseWriter, r *http.Request), methods ...string) error
HandleFunc 指定一个请求
func (*App) LocalPrinter ¶ added in v0.25.0
LocalPrinter 获取本地化的输出对象
func (*App) Run ¶ added in v0.28.0
Run 执行监听程序。
当调用 Shutdown 关闭服务时,会等待其完成未完的服务,才返回 http.ErrServerClosed
type GetResultFunc ¶ added in v0.26.0
GetResultFunc 用于生成 Result 接口对象的函数
type Result ¶ added in v0.26.0
type Result interface {
error
// 添加详细的内容
Add(key, val string)
// 是否存在详细的错误信息
//
// 如果有通过 Add 添加内容,那么应该返回 true
HasDetail() bool
// HTTP 状态码
//
// 最终会经此值作为 HTTP 状态会返回给用户
Status() int
}
Result 提供了自定义错误码的功能
比如类似以下的错误内容:
{
'message': 'error message',
'code': 4000001,
'detail':[
{'field': 'username': 'message': '已经存在相同用户名'},
{'field': 'username': 'message': '已经存在相同用户名'},
]
}
用户可以根据自己的需求,在出错时,展示自定义的错误码以及相关的错误信息。 其中通过 Add 和 Set 可以添加具体的字段错误信息。
可以在 internal/resulttest 查看 Result 的实现方式。
type Service ¶ added in v0.27.0
type Service struct {
Title string
// contains filtered or unexported fields
}
Service 服务模型
type ServiceFunc ¶ added in v0.27.0
ServiceFunc 服务实际需要执行的函数
实现者需要正确处理 ctx.Done 事件,调用者可能会主动取消函数执行; 如果是通 ctx 取消的,应该返回其错误信息。
type ServiceState ¶ added in v0.27.0
type ServiceState int8
ServiceState 服务的状态值
const ( ServiceStoped ServiceState = iota // 当前处理停止状态,默认状态 ServiceRunning // 正在运行 ServiceFailed // 出错,不再执行后续操作 )
几种可能的状态值
func (ServiceState) String ¶ added in v0.27.0
func (s ServiceState) String() string