Documentation
¶
Index ¶
Constants ¶
View Source
const ( // ErrCodeOK 代表业务正常。 ErrCodeOK = 0 // ErrCodeBadRequest 代表上游请求参数不合法。 ErrCodeBadRequest = 1 // ErrCodeInvalidError 代表业务返回了一个错误的 error 类型。 // 业务应该始终使用 `Error()` 方法返回错误,而不能直接返回一个普通的 error。 ErrCodeInvalidError = 2 // ErrCodeServerPanic 代表业务代码崩溃,框架抓住这个错误并返回错误信息。 ErrCodeServerPanic = 3 )
View Source
const ( // DefaultMaxHeaderBytes 是默认的 HTTP header 大小。 DefaultMaxHeaderBytes = http.DefaultMaxHeaderBytes )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { Addr string `config:"addr"` // 服务器监听的地址。 ReadTimeout time.Duration `config:"read_timeout"` // ReadTimeout 设置读 HTTP 数据超时。 ReadHeaderTimeout time.Duration `config:"read_header_timeout"` // ReadHeaderTimeout 设置读 HTTP header 超时。 WriteTimeout time.Duration `config:"write_timeout"` // WriteTimeout 设置写超时。 IdleTimeout time.Duration `config:"idle_timeout"` // IdleTimeout 设置空闲超时。 MaxHeaderBytes int `config:"max_header_bytes"` // MaxHeaderBytes 设置 HTTP header 最大大小,默认是 DefaultMaxHeaderBytes。 Debug bool `config:"debug"` // Debug 表示是否处于调试状态。 PingURI string `config:"ping_uri"` // PingURI 表示用作探针的 uri 地址,这个接口会在服务正常的时候返回 HTTP 200 OK。 }
Config 是 HTTP server 的配置。
type Handler ¶
type Handler interface{}
Handler 代表一个处理函数。
Handler 支持的函数签名格式:
- func(ctx context.Context, req *T) (res *U, err error):最推荐的业务函数签名形式。 其中 `T` 和 `U` 是请求和应答的结构类型。
- func(writer http.ResponseWriter, req *http.Request):如果需要使用更底层的能力,例如传输文件,可以使用这种形式。 这个签名跟 http.HandlerFunc 一致。
- http.Handler:也可以直接注册一个 http.Handler 实例,应用场景同上。
type Method ¶
type Method string
Method 代表 HTTP 请求方法。
type Router ¶
type Router interface { SubRouter(uri string, handlers ...Handler) (Router, error) Handle(method Method, uri string, handlers ...Handler) error HandleAny(uri string, handlers ...Handler) error }
Router 代表一个路由器实现,Routes 可以向 Router 注册路由信息。
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 代表一个 HTTP 服务。
func (*Server) MustAddRoutes ¶
MustAddRoutes 将 routes 路由信息添加到路有里面去,如果过程中发生任何错误,直接 panic。 由于一般来说 routes 格式错误都是程序 bug,所以这个函数可以简化业务代码,无需额外判断一个 error。
Click to show internal directories.
Click to hide internal directories.