跳转到内容

core — API 快速参考

本页列出 github.com/spcent/plumego/core 导出的每个公开符号。已知该包时用本页快速查阅;需要边界说明和使用模式时请读 Core Primer

import "github.com/spcent/plumego/core"

type AppConfig struct {
Addr string
TLS TLSConfig
Router RouterConfig
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
HTTP2Enabled bool
DrainInterval time.Duration
}
type TLSConfig struct {
Enabled bool
CertFile string
KeyFile string
}
type RouterConfig struct {
MethodNotAllowed bool
}
type AppDependencies struct {
Logger log.Logger
}

func New(cfg AppConfig, deps AppDependencies) *App

创建并返回新的 Appcfg 通常是 DefaultConfig() 的结果加上字段覆盖。

func DefaultConfig() AppConfig

返回带有生产默认值的 AppConfig

字段默认值
Addr:8080
ReadTimeout30s
ReadHeaderTimeout5s
WriteTimeout30s
IdleTimeout60s
MaxHeaderBytes1 MiB
HTTP2Enabledtrue
DrainInterval500ms

func (a *App) Get(path string, handler http.Handler) *router.Route
func (a *App) Post(path string, handler http.Handler) *router.Route
func (a *App) Put(path string, handler http.Handler) *router.Route
func (a *App) Delete(path string, handler http.Handler) *router.Route
func (a *App) Patch(path string, handler http.Handler) *router.Route
func (a *App) Any(path string, handler http.Handler) *router.Route
func (a *App) AddRoute(method, path string, handler http.Handler, opts ...router.RouteOption) *router.Route
func (a *App) Use(middlewares ...func(http.Handler) http.Handler)

必须在 Prepare 之前调用。中间件按注册顺序运行。


func (a *App) Prepare() error

冻结路由表并构建处理链。必须在 Server 之前调用。

func (a *App) Server() (*http.Server, error)

返回已配置的 *http.Server。调用方负责 ListenAndServeShutdown

func (a *App) Run()

合并路径:调用 Prepare 然后启动服务器。

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

优雅停机,排空飞行中的连接。

func (a *App) URL(name string, pairs ...string) string

将命名路由解析为 URL 字符串。