app

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2019 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package app 核心功能的实现

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	http.Server
	// contains filtered or unexported fields
}

App 程序运行实例

func New

func New(conf *webconfig.WebConfig, get GetResultFunc) *App

New 声明一个新的 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

func (app *App) AddMessages(status int, msgs map[int]string)

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) Close

func (app *App) Close() error

Close 关闭服务。

无论配置文件如果设置,此函数都是直接关闭服务,不会等待。

func (*App) Delete added in v0.27.0

func (app *App) Delete(path string, h http.Handler) *mux.Prefix

Delete 指定个 Delete 请求处理

func (*App) DeleteFunc added in v0.27.0

func (app *App) DeleteFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix

DeleteFunc 指定一个 Delete 请求

func (*App) ErrorHandlers added in v0.21.0

func (app *App) ErrorHandlers() *errorhandler.ErrorHandler

ErrorHandlers 错误处理功能

func (*App) Get added in v0.27.0

func (app *App) Get(path string, h http.Handler) *mux.Prefix

Get 指定一个 GET 请求

func (*App) GetFunc added in v0.27.0

func (app *App) GetFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix

GetFunc 指定一个 GET 请求

func (*App) Handle added in v0.27.0

func (app *App) Handle(path string, h http.Handler, methods ...string) error

Handle 添加一个路由项

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) IsDebug

func (app *App) IsDebug() bool

IsDebug 是否处于调试模式

func (*App) LocalPrinter added in v0.25.0

func (app *App) LocalPrinter(tag language.Tag, opts ...xmessage.Option) *xmessage.Printer

LocalPrinter 获取本地化的输出对象

func (*App) Location added in v0.26.0

func (app *App) Location() *time.Location

Location 当前设置的时区信息

func (*App) Logs

func (app *App) Logs() *logs.Logs

Logs 返回 logs.Logs 实例

func (*App) Messages added in v0.21.0

func (app *App) Messages(p *xmessage.Printer) map[int]string

Messages 错误信息列表

p 用于返回特定语言的内容。如果为空,则表示返回原始值。

func (*App) Mimetypes

func (app *App) Mimetypes() *mimetype.Mimetypes

Mimetypes 返回 mimetype.Mimetypes

func (*App) Mux

func (app *App) Mux() *mux.Mux

Mux 返回相关的 mux.Mux 实例

func (*App) NewResult added in v0.26.0

func (app *App) NewResult(code int) Result

NewResult 查找指定代码的错误信息

func (*App) Patch added in v0.27.0

func (app *App) Patch(path string, h http.Handler) *mux.Prefix

Patch 指定个 Patch 请求处理

func (*App) PatchFunc added in v0.27.0

func (app *App) PatchFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix

PatchFunc 指定一个 Patch 请求

func (*App) Path added in v0.22.0

func (app *App) Path(p string) string

Path 生成路径部分的地址

基于 app.webConfig.URL 中的路径部分。

func (*App) Post added in v0.27.0

func (app *App) Post(path string, h http.Handler) *mux.Prefix

Post 指定个 POST 请求处理

func (*App) PostFunc added in v0.27.0

func (app *App) PostFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix

PostFunc 指定一个 Post 请求

func (*App) Prefix added in v0.27.0

func (app *App) Prefix(prefix string) *mux.Prefix

Prefix 声明一个 Prefix 实例。

func (*App) Put added in v0.27.0

func (app *App) Put(path string, h http.Handler) *mux.Prefix

Put 指定个 Put 请求处理

func (*App) PutFunc added in v0.27.0

func (app *App) PutFunc(path string, h func(w http.ResponseWriter, r *http.Request)) *mux.Prefix

PutFunc 指定一个 Put 请求

func (*App) Run added in v0.28.0

func (app *App) Run() (err error)

Run 执行监听程序。

当调用 Shutdown 关闭服务时,会等待其完成未完的服务,才返回 http.ErrServerClosed

func (*App) Scheduled added in v0.27.0

func (app *App) Scheduled() *scheduled.Server

Scheduled 获取 scheduled.Server 实例

func (*App) Services added in v0.27.0

func (app *App) Services() []*Service

Services 返回所有的服务列表

func (*App) Shutdown

func (app *App) Shutdown(ctx context.Context) error

Shutdown 关闭所有服务。

根据配置文件中的配置项,决定当前是直接关闭还是延时之后关闭。

func (*App) URL

func (app *App) URL(path string) string

URL 构建一条基于 app.webconfig.URL 的完整 URL

type GetResultFunc added in v0.26.0

type GetResultFunc func(status, code int, message string) Result

GetResultFunc 用于生成 Result 接口对象的函数

type JobFunc added in v0.27.0

type JobFunc = scheduled.JobFunc

JobFunc 定时任务执行的函数

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 可以添加具体的字段错误信息。

实现者需要确定实现相关的 mimetype 输出方式。

可以查看 internal/resulttest 查看 Result 的实现方式。

type Service added in v0.27.0

type Service struct {
	Title string
	// contains filtered or unexported fields
}

Service 服务模型

func (*Service) Err added in v0.27.0

func (srv *Service) Err() error

Err 上次的错误信息,不会清空。

func (*Service) Run added in v0.27.0

func (srv *Service) Run()

Run 开始执行该服务

func (*Service) State added in v0.27.0

func (srv *Service) State() ServiceState

State 获取当前服务的状态

func (*Service) Stop added in v0.27.0

func (srv *Service) Stop()

Stop 停止服务。

type ServiceFunc added in v0.27.0

type ServiceFunc func(ctx context.Context) error

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

Jump to

Keyboard shortcuts

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