core — API 快速参考
core — API 快速参考
Section titled “core — API 快速参考”本页列出 github.com/spcent/plumego/core 导出的每个公开符号。已知该包时用本页快速查阅;需要边界说明和使用模式时请读 Core Primer。
import "github.com/spcent/plumego/core"AppConfig
Section titled “AppConfig”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}TLSConfig
Section titled “TLSConfig”type TLSConfig struct { Enabled bool CertFile string KeyFile string}RouterConfig
Section titled “RouterConfig”type RouterConfig struct { MethodNotAllowed bool}AppDependencies
Section titled “AppDependencies”type AppDependencies struct { Logger log.Logger}func New(cfg AppConfig, deps AppDependencies) *App创建并返回新的 App。cfg 通常是 DefaultConfig() 的结果加上字段覆盖。
DefaultConfig
Section titled “DefaultConfig”func DefaultConfig() AppConfig返回带有生产默认值的 AppConfig。
| 字段 | 默认值 |
|---|---|
Addr | :8080 |
ReadTimeout | 30s |
ReadHeaderTimeout | 5s |
WriteTimeout | 30s |
IdleTimeout | 60s |
MaxHeaderBytes | 1 MiB |
HTTP2Enabled | true |
DrainInterval | 500ms |
路由注册方法
Section titled “路由注册方法”Get / Post / Put / Delete / Patch / Any
Section titled “Get / Post / Put / Delete / Patch / Any”func (a *App) Get(path string, handler http.Handler) *router.Routefunc (a *App) Post(path string, handler http.Handler) *router.Routefunc (a *App) Put(path string, handler http.Handler) *router.Routefunc (a *App) Delete(path string, handler http.Handler) *router.Routefunc (a *App) Patch(path string, handler http.Handler) *router.Routefunc (a *App) Any(path string, handler http.Handler) *router.RouteAddRoute
Section titled “AddRoute”func (a *App) AddRoute(method, path string, handler http.Handler, opts ...router.RouteOption) *router.Routefunc (a *App) Use(middlewares ...func(http.Handler) http.Handler)必须在 Prepare 之前调用。中间件按注册顺序运行。
Prepare
Section titled “Prepare”func (a *App) Prepare() error冻结路由表并构建处理链。必须在 Server 之前调用。
Server
Section titled “Server”func (a *App) Server() (*http.Server, error)返回已配置的 *http.Server。调用方负责 ListenAndServe 和 Shutdown。
func (a *App) Run()合并路径:调用 Prepare 然后启动服务器。
Shutdown
Section titled “Shutdown”func (a *App) Shutdown(ctx context.Context) error优雅停机,排空飞行中的连接。
func (a *App) URL(name string, pairs ...string) string将命名路由解析为 URL 字符串。
- Core Primer — 边界说明、起点检查表和所有权示例
- 英文完整版 — 含所有方法签名和详细说明