fexgo

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2023 License: MIT Imports: 9 Imported by: 0

README

Web 框架 - FexGo

参考 7 days golang programs from scratch

特性

  • 网络编程:基于 net/http 库的 HTTP 接口
  • 请求上下文:使用 Context 封装 Request 和 Response,生命周期与请求一致,承载请求相关信息,对外提供简化接口
  • 路由:支持动态路由,前缀树实现
  • 路由分组:支持路由分组控制,支持分组嵌套,支持应用分组中间件
  • 中间件
  • 静态文件服务:基于 http.FileServer
  • 服务端模板渲染:基于 http/template 库,支持普通变量/列表/对象渲染
  • 错误恢复:提供中间件处理 panic

使用多模块工作区进行开发

  1. 在工作区目录下执行下面的命令,会创建 go.work文件
go work init ./example
  1. 在工作区中添加其他模块
go work use ./fex
  1. 通过此方法调用同一工作区下的其他模块。执行 example 模块:
go run felixorb/example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Writer     http.ResponseWriter
	Req        *http.Request
	Path       string
	Method     string
	Params     map[string]string
	StatusCode int
	// contains filtered or unexported fields
}

func (*Context) Data

func (c *Context) Data(code int, data []byte)

func (*Context) Fail

func (c *Context) Fail(code int, err string)

func (*Context) HTML

func (c *Context) HTML(code int, name string, data interface{})

func (*Context) JSON

func (c *Context) JSON(code int, obj interface{})

func (*Context) Next

func (c *Context) Next()

func (*Context) Param

func (c *Context) Param(key string) string

func (*Context) PostForm

func (c *Context) PostForm(key string) string

func (*Context) Query

func (c *Context) Query(key string) string

func (*Context) SetHeader

func (c *Context) SetHeader(key string, value string)

func (*Context) Status

func (c *Context) Status(code int)

func (*Context) String

func (c *Context) String(code int, format string, values ...interface{})

type Engine

type Engine struct {
	*RouterGroup // 支持 RouterGroup 的所有接口
	// contains filtered or unexported fields
}

Engine 协调整个框架的资源。 router 作为核心,不直接对外暴露接口,而是对外提供 Engine 模型

func Default

func Default() *Engine

func NewEngine

func NewEngine() *Engine

func (*Engine) LoadHTMLGlob

func (e *Engine) LoadHTMLGlob(path string)

LoadHTMLGlob 将所有模板加载到内存 注意需要先设置 FuncMap

func (*Engine) Run

func (e *Engine) Run(addr string) error

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 处理请求时,将 Writer 和 Req 封装为 Context 实例 1. 根据 URL 解析分组中间件 2. 由 Router 实现具体路由

func (*Engine) SetFuncMap

func (e *Engine) SetFuncMap(funcMap template.FuncMap)

type H

type H map[string]interface{}

type HandlerFunc

type HandlerFunc func(*Context)

func Logger

func Logger() HandlerFunc

func Recovery

func Recovery() HandlerFunc

type RouterGroup

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

保存 Path 前缀,以前缀划分分组,支持嵌套

func (*RouterGroup) GET

func (rg *RouterGroup) GET(pattern string, handler HandlerFunc)

func (*RouterGroup) Group

func (rg *RouterGroup) Group(name string) *RouterGroup

Group 创建分组,支持嵌套创建

func (*RouterGroup) POST

func (rg *RouterGroup) POST(pattern string, handler HandlerFunc)

func (*RouterGroup) Static

func (rg *RouterGroup) Static(relativeDir, rootDir string)

Static 绑定静态资源目录。使用 http.FileServer 完成静态文件服务

func (*RouterGroup) Use

func (rg *RouterGroup) Use(middlewares ...HandlerFunc)

Jump to

Keyboard shortcuts

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