Documentation ¶
Index ¶
- type Context
- func (c *Context) BindJSON(val any) error
- func (c *Context) FormValue(key string) StringValue
- func (c *Context) PathValue(key string) StringValue
- func (c *Context) QueryValue(key string) StringValue
- func (c *Context) Render(tplName string, data any) error
- func (c *Context) RespJSON(code int, val any) error
- func (c *Context) RespJSONOK(val any) error
- func (c *Context) SetCookie(cookie *http.Cookie)
- type FileDownloader
- type FileUploader
- type GoTemplateEngine
- func (g *GoTemplateEngine) LoadFromFS(fs fs.FS, patterns ...string) error
- func (g *GoTemplateEngine) LoadFromFiles(filenames ...string) error
- func (g *GoTemplateEngine) LoadFromGlob(pattern string) error
- func (g *GoTemplateEngine) Render(ctx context.Context, tplName string, data any) ([]byte, error)
- type HTTPServer
- func (s *HTTPServer) Delete(path string, handleFunc HandleFunc)
- func (s *HTTPServer) Get(path string, handleFunc HandleFunc)
- func (s *HTTPServer) Options(path string, handleFunc HandleFunc)
- func (s *HTTPServer) Patch(path string, handleFunc HandleFunc)
- func (s *HTTPServer) Post(path string, handleFunc HandleFunc)
- func (s *HTTPServer) Put(path string, handleFunc HandleFunc)
- func (s *HTTPServer) ServeHTTP(writer http.ResponseWriter, request *http.Request)
- func (s *HTTPServer) Start(addr string) error
- func (s *HTTPServer) Use(mdls ...Middleware)
- type HTTPServerOption
- type HandleFunc
- type Middleware
- type Server
- type StaticResourceHandler
- type StaticResourceHandlerOption
- type StringValue
- type TemplateEngine
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { // 将 http.Request 和 http.ResponseWriter 封装到 Context 里面 // 这样就可以在业务逻辑里面使用了 Req *http.Request // Resp 原生的 ResponseWriter。当你直接使用 Resp 的时候, // 那么相当于你绕开了 RespStatusCode 和 RespData。 // 响应数据直接被发送到前端,其它中间件将无法修改响应 // 其实我们也可以考虑将这个做成私有的 Resp http.ResponseWriter PathParams map[string]string // 缓存的响应部分 // 这部分数据会在最后刷新 RespStatusCode int RespData []byte // 命中的路由 MatchedRoute string // 用户可以自由决定在这里存储什么, // 主要用于解决在不同 Middleware 之间数据传递的问题 // 但是要注意 // 1. UserValues 在初始状态的时候总是 nil,你需要自己手动初始化 // 懒汉模式 => 在第一次使用的时候初始化 UserValues map[string]any // contains filtered or unexported fields }
Context is the interface that wraps the basic ServeHTTP method.
func (*Context) FormValue ¶
func (c *Context) FormValue(key string) StringValue
FormValue returns the first value for the named component of the query.
func (*Context) PathValue ¶
func (c *Context) PathValue(key string) StringValue
func (*Context) QueryValue ¶
func (c *Context) QueryValue(key string) StringValue
QueryValue Query 和表单比起来,它没有缓存,所以需要自己缓存起来,不然每次都要解析
func (*Context) RespJSONOK ¶
type FileDownloader ¶
type FileDownloader struct {
Dir string
}
FileDownloader 直接操作了 http.ResponseWriter 所以在 Middleware 里面将不能使用 RespData 因为没有赋值
func (*FileDownloader) Handle ¶
func (f *FileDownloader) Handle() HandleFunc
type FileUploader ¶
type FileUploader struct { FileField string // FileField 对应于文件在表单中的字段名字 DstPathFunc func(fh *multipart.FileHeader) string // DstPathFunc 用于计算目标路径 }
FileUploader @Description:
func (*FileUploader) Handle ¶
func (f *FileUploader) Handle() HandleFunc
Handle 返回值是路由函数,这样封装了一层,可以通过传参,继续添加功能。 上一种可以在返回 HandleFunc 之前可以继续检测一下传入的字段 这种形态和 Option 模式配合就很好
type GoTemplateEngine ¶
func (*GoTemplateEngine) LoadFromFS ¶
func (g *GoTemplateEngine) LoadFromFS(fs fs.FS, patterns ...string) error
func (*GoTemplateEngine) LoadFromFiles ¶
func (g *GoTemplateEngine) LoadFromFiles(filenames ...string) error
func (*GoTemplateEngine) LoadFromGlob ¶
func (g *GoTemplateEngine) LoadFromGlob(pattern string) error
type HTTPServer ¶
type HTTPServer struct {
// contains filtered or unexported fields
}
HTTPServer is the implementation of Server.
func NewHTTPServer ¶
func NewHTTPServer(opts ...HTTPServerOption) *HTTPServer
func (*HTTPServer) Delete ¶
func (s *HTTPServer) Delete(path string, handleFunc HandleFunc)
func (*HTTPServer) Get ¶
func (s *HTTPServer) Get(path string, handleFunc HandleFunc)
func (*HTTPServer) Options ¶
func (s *HTTPServer) Options(path string, handleFunc HandleFunc)
func (*HTTPServer) Patch ¶
func (s *HTTPServer) Patch(path string, handleFunc HandleFunc)
func (*HTTPServer) Post ¶
func (s *HTTPServer) Post(path string, handleFunc HandleFunc)
func (*HTTPServer) Put ¶
func (s *HTTPServer) Put(path string, handleFunc HandleFunc)
func (*HTTPServer) ServeHTTP ¶
func (s *HTTPServer) ServeHTTP(writer http.ResponseWriter, request *http.Request)
ServeHTTP is the entry point for a request handler. ServeHTTP 处理请求的入口
func (*HTTPServer) Start ¶
func (s *HTTPServer) Start(addr string) error
Start starts the HTTP server.
func (*HTTPServer) Use ¶
func (s *HTTPServer) Use(mdls ...Middleware)
Use 可以通过调用方法注册 Middleware 也可以改成 Opts 函数选项模式
type HTTPServerOption ¶
type HTTPServerOption func(server *HTTPServer)
func ServerWithTemplateEngine ¶
func ServerWithTemplateEngine(tplEngine TemplateEngine) HTTPServerOption
type HandleFunc ¶
type HandleFunc func(ctx *Context)
type Middleware ¶
type Middleware func(next HandleFunc) HandleFunc
type Server ¶
type Server interface { // Handler ServeHTTP should write reply headers and data to the ResponseWriter // 继承 http.Handler 接口 http.Handler // Start starts the HTTP server. // addr 是监听地址。如果只指定端口,可以使用 ":8081" // 或者 "localhost:8082" Start(addr string) error // contains filtered or unexported methods }
Server is the interface that wraps the basic ServeHTTP method. here is the interface of core API
type StaticResourceHandler ¶
type StaticResourceHandler struct {
// contains filtered or unexported fields
}
StaticResourceHandler 静态资源处理 两个层面上 1. 大文件不缓存 2. 控制住了缓存的文件的数量 所以,最多消耗多少内存? size(cache) * maxSize
func NewStaticResourceHandler ¶
func NewStaticResourceHandler(dir, pathPrefix string, options ...StaticResourceHandlerOption) *StaticResourceHandler
func (*StaticResourceHandler) Handle ¶
func (h *StaticResourceHandler) Handle(ctx *Context)
Handle 静态资源的处理逻辑
type StaticResourceHandlerOption ¶
type StaticResourceHandlerOption func(*StaticResourceHandler)
func StaticWithMaxFileSize ¶
func StaticWithMaxFileSize(maxSize int) StaticResourceHandlerOption
func WithFileCache ¶
func WithFileCache(maxFileSizeThreshold int, maxCacheFileCnt int) StaticResourceHandlerOption
WithFileCache 静态文件将会被缓存 maxFileSizeThreshold 超过这个大小的文件,就被认为是大文件,我们将不会缓存 maxCacheFileCnt 最多缓存多少个文件 所以我们最多缓存 maxFileSizeThreshold * maxCacheFileCnt
func WithMoreExtension ¶
func WithMoreExtension(extMap map[string]string) StaticResourceHandlerOption
type StringValue ¶
type StringValue struct {
// contains filtered or unexported fields
}
func (StringValue) String ¶
func (s StringValue) String() (string, error)
func (StringValue) StringMultiVal ¶
func (s StringValue) StringMultiVal() ([]string, error)
func (StringValue) ToInt64 ¶
func (s StringValue) ToInt64() (int64, error)