web

package module
v0.0.0-...-75e85f9 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 16 Imported by: 0

README

web 框架

实现流程如下: 工作流程

设计参照项目如下:

  • gin
  • beego
  • echo
  • iris

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Req *http.Request
	// Resp 原生的 ResponseWriter。当你直接使用 Resp 的时候,
	// 那么相当于你绕开了 RespStatusCode 和 RespData。
	// 响应数据直接被发送到前端,其它中间件将无法修改响应
	// 其实我们也可以考虑将这个做成私有的
	Resp http.ResponseWriter
	// 缓存的响应部分
	// 这部分数据会在最后刷新
	RespStatusCode int
	RespData       []byte

	PathParams map[string]string
	// 命中的路由
	MatchedRoute string

	// 主要用于 session 存储
	UserValues map[string]any
	// contains filtered or unexported fields
}

func (*Context) BindJSON

func (c *Context) BindJSON(val any) error

func (*Context) FormValue

func (c *Context) FormValue(key string) StringValue

func (*Context) PathValue

func (c *Context) PathValue(key string) StringValue

func (*Context) QueryValue

func (c *Context) QueryValue(key string) StringValue

func (*Context) Render

func (c *Context) Render(tplName string, data any) error

func (*Context) RespJSON

func (c *Context) RespJSON(code int, val any) error

func (*Context) RespOK

func (c *Context) RespOK(val any) error

type FileDownloader

type FileDownloader struct {
	Dir string
}

FileDownloader 直接操作了 http.ResponseWriter 所以在 Middleware 里面将不能使用 RespData 因为没有赋值

func (*FileDownloader) Handle

func (fd *FileDownloader) Handle() HandleFunc

type FileUploader

type FileUploader struct {
	// FileField 对应于文件在表单中的字段名字
	FileField string
	// DstPathFunc 用于计算目标路径
	DstPathFunc func(fh *multipart.FileHeader) string
}

func (*FileUploader) Handle

func (f *FileUploader) Handle() HandleFunc

type GoTemplateEngine

type GoTemplateEngine struct {
	T *template.Template
}

func (*GoTemplateEngine) LoadFromFs

func (g *GoTemplateEngine) LoadFromFs(files ...string) error

func (*GoTemplateEngine) LoadFromGlob

func (g *GoTemplateEngine) LoadFromGlob(pattern string) error

func (*GoTemplateEngine) LoadFs

func (g *GoTemplateEngine) LoadFs(fs fs.FS, pattern string) error

func (*GoTemplateEngine) Render

func (g *GoTemplateEngine) Render(ctx context.Context,
	tplName string, data any) ([]byte, error)

type HTTPServer

type HTTPServer struct {
	// contains filtered or unexported fields
}

func NewHTTPServer

func NewHTTPServer(opts ...HTTPServerOption) *HTTPServer

func (*HTTPServer) GET

func (h *HTTPServer) GET(path string, handler HandleFunc)

func (*HTTPServer) POST

func (h *HTTPServer) POST(path string, handler HandleFunc)

func (*HTTPServer) ServeHTTP

func (h *HTTPServer) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP HTTPServer 处理请求的入口

func (*HTTPServer) Start

func (h *HTTPServer) Start(addr string) error

Start 启动服务器

func (*HTTPServer) Use

func (h *HTTPServer) Use(mdls ...Middleware)

type HTTPServerOption

type HTTPServerOption func(server *HTTPServer)

func ServerWithMiddleware

func ServerWithMiddleware(mdls ...Middleware) HTTPServerOption

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 {
	http.Handler
	// Start 启动服务器
	// addr 是监听地址。如果只指定端口,可以使用 ":8081"
	// 或者 "localhost:8082"
	Start(addr string) error
	// contains filtered or unexported methods
}

type StaticResourceHandler

type StaticResourceHandler struct {
	// contains filtered or unexported fields
}

func NewStaticResourceHandler

func NewStaticResourceHandler(dir string, pathPrefix string,
	opts ...StaticResourceHandlerOption) *StaticResourceHandler

func (*StaticResourceHandler) Handle

func (h *StaticResourceHandler) Handle(ctx *Context)

type StaticResourceHandlerOption

type StaticResourceHandlerOption func(s *StaticResourceHandler)

type StringValue

type StringValue struct {
	// contains filtered or unexported fields
}

func (StringValue) String

func (s StringValue) String() (string, error)

type TemplateEngine

type TemplateEngine interface {
	// Render 渲染页面
	// data 是渲染页面所需要的数据
	Render(ctx context.Context, tplName string, data any) ([]byte, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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