seed

package module
v0.0.0-...-6878667 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

seed

Documentation

Index

Constants

View Source
const (
	MethodGet     = http.MethodGet
	MethodHead    = http.MethodHead
	MethodPost    = http.MethodPost
	MethodPut     = http.MethodPut
	MethodPatch   = http.MethodPatch // RFC 5789
	MethodDelete  = http.MethodDelete
	MethodConnect = http.MethodConnect
	MethodOptions = http.MethodOptions
	MethodTrace   = http.MethodTrace
	MethodAny     = "ANY" //for all methods support
)
View Source
const (
	// HeaderContentType  HTTP Header 中 Content-Type 的 Key
	HeaderContentType = "Content-Type"

	// HeaderContentLength HTTP Header 中 Content-Length 的 Key
	HeaderContentLength = "Content-Length"
)
View Source
const (
	Worker = "worker"
	Master = "master"
)

Variables

This section is empty.

Functions

func IsMaster

func IsMaster() bool

IsMaster 当前进程的环境是否是master进程

func IsWorker

func IsWorker() bool

IsWorkerMode 当前进程的环境是否是worker进程

Types

type ContextKey

type ContextKey struct {
	Name string
}

ContextKey is a value for use with context.WithValue. It's used as a pointer, so it fits in an interface{} without allocation. This technique for defining context keys was copied from Go 1.7's new use of context in net/http.

func (*ContextKey) String

func (k *ContextKey) String() string

type HandlerFunc

type HandlerFunc func(ctx context.Context, req Request) Response

HandlerFunc 标准的HandlerFunc

var NotFoundHandler HandlerFunc = func(ctx context.Context, req Request) Response {
	return NopResponse(http.StatusNotFound)
}

NotFoundHandler 404默认处理器

func (HandlerFunc) Handler

func (h HandlerFunc) Handler() http.Handler

Handler HandlerFunc自身转换为http.Handler

type MSeed

type MSeed interface {
	Router

	// HTTPServer 返回实例的 *http.Server
	HTTPServer() *http.Server

	// 设置默认的http server
	SetHTTPServer(srv *http.Server) MSeed

	// Run 启动HTTPServer
	//
	// 	addrs 监听的端口,非必选,但如果没有通过HTTPServer pointer修改且值不给可能导致服务启动失败
	Run(addrs ...string) error

	// Run 启动HTTPServer
	//
	// 	certFile https certFile
	// 	keyFile https keyFile
	// 	addrs 监听的端口,非必选,但如果没有通过HTTPServer pointer修改且值不给可能导致服务启动失败
	RunTLS(certFile, keyFile string, addrs ...string) error
}

func New

func New() MSeed

New return *mseed

type MiddleWareQueue

type MiddleWareQueue interface {
	Next(ctx context.Context, w http.ResponseWriter, req *http.Request) bool
}

MiddleWareQueue 中间件执行队列

func NewMiddleWareQueue

func NewMiddleWareQueue(funcs ...MiddlewareFunc) MiddleWareQueue

NewMiddleWareQueue 创建一个中间件执行队列

type MiddlewareFunc

type MiddlewareFunc func(ctx context.Context, w http.ResponseWriter, req *http.Request, next MiddleWareQueue) bool

MiddlewareFunc 中间件执行器

type MiddlewareFuncs

type MiddlewareFuncs []MiddlewareFunc

MiddlewareFuncs 中间件执行器队列示例

func (MiddlewareFuncs) Next

Next 触发下一个 MiddleWareFunc 或者是业务 handler

执行顺序: filter1 -> filter2 -> filter3 若 filter2 返回 false,调用将终止,即 filter3 不会被执行

type Request

type Request interface {
	// HTTPRequest 返回原始 *http.Request
	HTTPRequest() *http.Request

	// Query 获取GET方式传递的参数
	Query(name string) (value string, has bool)

	// QueryDefault 获取GET方式传递的参数如果没有那么返回默认值/空值
	QueryDefault(name string, defaultValue ...string) (value string)

	// PostForm 获取POST方式传递的参数
	PostForm(name string) (value string, has bool)

	// PostFormDefault 获取POST方式传递的参数如果没有那么返回默认值/空值
	PostFormDefault(name string, defaultValue ...string) (value string)

	// Param 获取GET/POST方式传递的参数
	Param(name string) (value string, has bool)

	// ParamDefault 获取GET/POST方式传递的参数如果没有那么返回默认值/空值
	ParamDefault(name string, defaultValue ...string) (value string)

	// Header 获取Header传递的参数
	Header(name string) (value string, has bool)

	// HeaderDefault 获取Header方式传递的参数如果没有那么返回默认值/空值
	HeaderDefault(name string, defaultValue ...string) (value string)

	// Cookie 获取Cookie方式传递的参数
	Cookie(name string) (value *http.Cookie, has bool)

	// RemoteAddr 获取客户端的请求地址
	RemoteAddr() string

	// JsonUnmarshal json序列化参数到目标数据
	JsonUnmarshal(dst interface{}) error
}

func NewRequest

func NewRequest(req *http.Request) Request

NewRequest 返回Request实例

type Response

type Response interface {
	WriteTo(w http.ResponseWriter) error
}

func HtmlResponse

func HtmlResponse(statusCode int, html string) Response

func JsonResponse

func JsonResponse(statusCode int, data interface{}) Response

JsonResponse 返回JsonResponse

func NopResponse

func NopResponse(statusCode int) Response

NopResponse 返回一个nopResponse

type Route

type Route interface {
	http.Handler

	Path() string
	Method() string
}

type RouteMapper

type RouteMapper interface {
	Add(route Route) error
	Find(req *http.Request) Route
}

RouteMapper 路由匹配器

type Router

type Router interface {
	http.Handler

	// Use 注册过滤器(中间件)
	//
	// 	可用于给 server 增加切片的功能
	// 	如可以创建注册一个用于校验是否登录的 中间件
	// 	注册的中间件会对此后的 handler 生效,在此之前注册的 handler 方法不会生效
	Use(ms ...MiddlewareFunc) Router

	// HandleStd 以http.Handler方式注册业务handler
	//
	// 	method  是http方法,如GET、POST,也可以使用逗号来连接同时传入多个,如 "GET,POST"
	// 	也可以用特殊的 ANY,会自动注册所有( ANY 的取值详见 MethodAny )
	// 	handler 是业务的逻辑
	// 	ms 是该接口特有的中间件函数
	HandleStd(methods string, path string, handler http.Handler, ms ...MiddlewareFunc)

	// HandleFunc 以HandlerFunc方式注册业务handler
	//
	// 	method  是http方法,如GET、POST,也可以使用逗号来连接同时传入多个,如 "GET,POST"
	// 	也可以用特殊的 ANY,会自动注册所有( ANY 的取值详见 MethodAny )
	// 	handler 是业务的逻辑
	// 	ms 是该接口特有的中间件函数
	HandleFunc(methods string, path string, handlerFunc HandlerFunc, ms ...MiddlewareFunc)

	// Group 路由分组
	//
	// 	如 可以将 /user/xxx 系列分成一个分组
	// 	prefix路由前缀,如 "/user"
	// 	ms 是该分组的中间件函数
	Group(prefix string, f func(r Router), ms ...MiddlewareFunc)
}

Router 路由器

func NewRouter

func NewRouter() Router

NewRouter 返回一个Router实例

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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