diff --git a/README.md b/README.md index c6ac80e..5044e8d 100644 --- a/README.md +++ b/README.md @@ -32,13 +32,8 @@ Add and initialize your program and execute it, that's it! package main import ( - "fmt" "knife" - "knife/middleware/cache" - "knife/middleware/gzip" - "knife/middleware/logger" - "knife/middleware/proxy" - "knife/middleware/recover" + "knife/middleware" "log" "net/http" "time" @@ -48,7 +43,7 @@ func main() { //Create a middleware chain //first plan - //chain := planOne() + chain := planOne() //second plan //chain := planTwo() @@ -56,20 +51,8 @@ func main() { //the third plan //chain := planThree() - //gzip middleware - //chain := gzipFun() - - //cache middleware - //chain := cacheFun() - - //cache middleware - chain := loadBalance() - //start serve - err := http.ListenAndServe(":8080", chain) - if err != nil { - panic(err) - } + _ = http.ListenAndServe(":8080", chain) } // plan one @@ -77,9 +60,9 @@ func planOne() *knife.Chain { //create middleware chain chain := knife.NewChain() //add logger middleware - chain.Use(logger.Logger()) + chain.Use(middleware.Logger()) //add recover middleware - chain.Use(recover.Recover()) + chain.Use(middleware.Recover()) //add custom implemented middleware chain.Use(func(context *knife.Context) { @@ -97,7 +80,7 @@ func planOne() *knife.Chain { // add middleware to the constructor func planTwo() *knife.Chain { //create a middleware chain and add logging and error handling middleware - chain := knife.NewChain(logger.Logger(), recover.Recover()) + chain := knife.NewChain(middleware.Logger(), middleware.Recover()) //add custom implemented middleware chain.Use(func(context *knife.Context) { @@ -116,8 +99,8 @@ func planTwo() *knife.Chain { func planThree() *knife.Chain { //create a middleware chain and use the chain to add logging, error handling, and custom middleware chain := knife.NewChain(). - Use(logger.Logger()). - Use(recover.Recover()). + Use(middleware.Logger()). + Use(middleware.Recover()). Use(func(context *knife.Context) { start := time.Now() defer func() { @@ -130,74 +113,6 @@ func planThree() *knife.Chain { return chain } -func gzipFun() *knife.Chain { - chain := knife.NewChain(). - Use(logger.Logger()). - Use(recover.Recover()). - //Use(middleware.GzipDefault()). - Use(gzip.Gzip(1024)). - Use(func(context *knife.Context) { - data := "Gzip是一种压缩文件格式并且也是一个在类 Unix 上的一种文件解压缩的软件,通常指GNU计划的实现,此处的gzip代表GNU zip。" + - "也经常用来表示gzip这种文件格式。软件的作者是Jean-loup Gailly和Mark Adler。在1992年10月31日第一次公开发布,版本号0.1,1993年2月,发布了1.0版本。" + - "Gzip是一种压缩文件格式并且也是一个在类 Unix 上的一种文件解压缩的软件,通常指GNU计划的实现,此处的gzip代表GNU zip。" + - "也经常用来表示gzip这种文件格式。软件的作者是Jean-loup Gailly和Mark Adler。在1992年10月31日第一次公开发布,版本号0.1,1993年2月,发布了1.0版本。" + - "Gzip是一种压缩文件格式并且也是一个在类 Unix 上的一种文件解压缩的软件,通常指GNU计划的实现,此处的gzip代表GNU zip。" + - "也经常用来表示gzip这种文件格式。软件的作者是Jean-loup Gailly和Mark Adler。在1992年10月31日第一次公开发布,版本号0.1,1993年2月,发布了1.0版本。" - _, err := context.Writer.Write([]byte(data)) - if err != nil { - panic(fmt.Sprintf("writer data error %s", err)) - } - context.Writer.WriteHeader(http.StatusOK) - }) - return chain -} - -func cacheFun() *knife.Chain { - chain := knife.NewChain(). - Use(logger.Logger()). - Use(recover.Recover()). - Use(cache.Cache(30, 60)). - Use(func(context *knife.Context) { - data := "Hello World" - _, err := context.Writer.Write([]byte(data)) - if err != nil { - panic(fmt.Sprintf("writer data error %s", err)) - } - context.Writer.WriteHeader(http.StatusOK) - }) - return chain -} - -func loadBalance() *knife.Chain { - nodes := []*proxy.ServiceNode{ - { - Address: "127.0.0.1:8080", - Weight: 1, - }, - { - Address: "127.0.0.2:8080", - Weight: 1, - }, - { - Address: "127.0.0.3:8080", - Weight: 1, - }, - } - chain := knife.NewChain(). - Use(logger.Logger()). - Use(recover.Recover()). - Use(proxy.LoadBalanceProxy(proxy.LoadBalanceRandom, nodes)). - Use(func(context *knife.Context) { - data := "Hello World" - _, err := context.Writer.Write([]byte(data)) - if err != nil { - panic(fmt.Sprintf("writer data error %s", err)) - } - context.Abort(http.StatusOK) - }) - return chain -} - ``` diff --git a/example/base/main.go b/example/base/main.go index 9668f3b..942f8f1 100644 --- a/example/base/main.go +++ b/example/base/main.go @@ -3,11 +3,7 @@ package main import ( "fmt" "knife" - "knife/middleware/cache" - "knife/middleware/gzip" - "knife/middleware/logger" - "knife/middleware/proxy" - "knife/middleware/recover" + "knife/middleware" "log" "net/http" "time" @@ -26,10 +22,10 @@ func main() { //chain := planThree() //gzip middleware - //chain := gzipFun() + //chain := gzip() //cache middleware - //chain := cacheFun() + //chain := cache() //cache middleware chain := loadBalance() @@ -46,9 +42,9 @@ func planOne() *knife.Chain { //create middleware chain chain := knife.NewChain() //add logger middleware - chain.Use(logger.Logger()) + chain.Use(middleware.Logger()) //add recover middleware - chain.Use(recover.Recover()) + chain.Use(middleware.Recover()) //add custom implemented middleware chain.Use(func(context *knife.Context) { @@ -66,7 +62,7 @@ func planOne() *knife.Chain { // add middleware to the constructor func planTwo() *knife.Chain { //create a middleware chain and add logging and error handling middleware - chain := knife.NewChain(logger.Logger(), recover.Recover()) + chain := knife.NewChain(middleware.Logger(), middleware.Recover()) //add custom implemented middleware chain.Use(func(context *knife.Context) { @@ -85,8 +81,8 @@ func planTwo() *knife.Chain { func planThree() *knife.Chain { //create a middleware chain and use the chain to add logging, error handling, and custom middleware chain := knife.NewChain(). - Use(logger.Logger()). - Use(recover.Recover()). + Use(middleware.Logger()). + Use(middleware.Recover()). Use(func(context *knife.Context) { start := time.Now() defer func() { @@ -99,12 +95,12 @@ func planThree() *knife.Chain { return chain } -func gzipFun() *knife.Chain { +func gzip() *knife.Chain { chain := knife.NewChain(). - Use(logger.Logger()). - Use(recover.Recover()). - //Use(gzip.Default()). - Use(gzip.Gzip(1024)). + Use(middleware.Logger()). + Use(middleware.Recover()). + //Use(middleware.GzipDefault()). + Use(middleware.Gzip(1024)). Use(func(context *knife.Context) { data := "Gzip是一种压缩文件格式并且也是一个在类 Unix 上的一种文件解压缩的软件,通常指GNU计划的实现,此处的gzip代表GNU zip。" + "也经常用来表示gzip这种文件格式。软件的作者是Jean-loup Gailly和Mark Adler。在1992年10月31日第一次公开发布,版本号0.1,1993年2月,发布了1.0版本。" + @@ -121,11 +117,11 @@ func gzipFun() *knife.Chain { return chain } -func cacheFun() *knife.Chain { +func cache() *knife.Chain { chain := knife.NewChain(). - Use(logger.Logger()). - Use(recover.Recover()). - Use(cache.Cache(30, 60)). + Use(middleware.Logger()). + Use(middleware.Recover()). + Use(middleware.Cache(30, 60)). Use(func(context *knife.Context) { data := "Hello World" _, err := context.Writer.Write([]byte(data)) @@ -138,7 +134,7 @@ func cacheFun() *knife.Chain { } func loadBalance() *knife.Chain { - nodes := []*proxy.ServiceNode{ + nodes := []*middleware.ServiceNode{ { Address: "127.0.0.1:8080", Weight: 1, @@ -153,9 +149,9 @@ func loadBalance() *knife.Chain { }, } chain := knife.NewChain(). - Use(logger.Logger()). - Use(recover.Recover()). - Use(proxy.LoadBalanceProxy(proxy.LoadBalanceRandom, nodes)). + Use(middleware.Logger()). + Use(middleware.Recover()). + Use(middleware.LoadBalanceProxy(middleware.LoadBalanceRandom, nodes)). Use(func(context *knife.Context) { data := "Hello World" _, err := context.Writer.Write([]byte(data)) diff --git a/example/matcher/main.go b/example/matcher/main.go index 6019662..f6dbe3e 100644 --- a/example/matcher/main.go +++ b/example/matcher/main.go @@ -2,11 +2,8 @@ package main import ( "knife" - "knife/matcher/combination" - "knife/matcher/header" - "knife/matcher/path" - "knife/middleware/logger" - "knife/middleware/recover" + "knife/matcher" + "knife/middleware" "log" "net/http" ) @@ -16,24 +13,24 @@ func main() { //创建中间件链 chain := knife.NewChain() //添加日志记录中间件 - chain.Use(logger.Logger()) + chain.Use(middleware.Logger()) //添加错误处理中间件 - chain.Use(recover.Recover()) + chain.Use(middleware.Recover()) //添加响应头是否存在匹配器的自定义中间件 - chain.UseMatcher(header.ResponseExists("token"), func(context *knife.Context) { + chain.UseMatcher(matcher.HeaderResponseExists("token"), func(context *knife.Context) { log.Printf("token middleware,token:%s ", context.Writer.Header().Get("token")) context.Next() }) //添加带有组合匹配器的自定义中间件 - chain.UseMatcher(combination.Any(header.ResponseExists("token"), header.ResponseExists("auth")), func(context *knife.Context) { + chain.UseMatcher(matcher.Any(matcher.HeaderResponseExists("token"), matcher.HeaderResponseExists("auth")), func(context *knife.Context) { log.Printf("token middleware,token:%s ", context.Writer.Header().Get("token")) context.Next() }) //添加带路径匹配器的自定义中间件 - chain.UseMatcher(path.Prefix("/hello"), func(context *knife.Context) { + chain.UseMatcher(matcher.PathPrefix("/hello"), func(context *knife.Context) { log.Println("pathPrefix matcher") context.Next() }) diff --git a/knife/matcher/combination/combination.go b/knife/matcher/combination.go similarity index 97% rename from knife/matcher/combination/combination.go rename to knife/matcher/combination.go index b756f15..c9e3ffb 100644 --- a/knife/matcher/combination/combination.go +++ b/knife/matcher/combination.go @@ -1,6 +1,6 @@ // 将匹配器进行组合的匹配器 -package combination +package matcher import "knife" diff --git a/knife/matcher/header/header.go b/knife/matcher/header.go similarity index 77% rename from knife/matcher/header/header.go rename to knife/matcher/header.go index 88f0981..fe52d04 100644 --- a/knife/matcher/header/header.go +++ b/knife/matcher/header.go @@ -1,43 +1,43 @@ // 关于请求头的匹配器 -package header +package matcher import ( "knife" "net/http" ) -func ResponseExists(key string) knife.MiddlewareMatcher { +func HeaderResponseExists(key string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return !equalHeaderValue(response.Header(), key, "") } } -func ResponseNotExists(key string) knife.MiddlewareMatcher { +func HeaderResponseNotExists(key string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return equalHeaderValue(response.Header(), key, "") } } -func ResponseNe(key, value string) knife.MiddlewareMatcher { +func HeaderResponseNe(key, value string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return !equalHeaderValue(response.Header(), key, value) } } -func ResponseEq(key, value string) knife.MiddlewareMatcher { +func HeaderResponseEq(key, value string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return equalHeaderValue(response.Header(), key, value) } } -func RequestExists(key string) knife.MiddlewareMatcher { +func HeaderRequestExists(key string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return !equalHeaderValue(request.Header, key, "") } } -func RequestNotExists(key string) knife.MiddlewareMatcher { +func HeaderRequestNotExists(key string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return equalHeaderValue(request.Header, key, "") } diff --git a/knife/matcher/method/method.go b/knife/matcher/method.go similarity index 76% rename from knife/matcher/method/method.go rename to knife/matcher/method.go index 402680a..fbe3bdd 100644 --- a/knife/matcher/method/method.go +++ b/knife/matcher/method.go @@ -1,26 +1,26 @@ // 关于请求方法的匹配器 -package method +package matcher import ( "knife" "strings" ) -func Eq(method string) knife.MiddlewareMatcher { +func MethodEq(method string) knife.MiddlewareMatcher { method = strings.ToUpper(method) return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return request.Method == method } } -func Ne(method string) knife.MiddlewareMatcher { +func MethodNe(method string) knife.MiddlewareMatcher { method = strings.ToUpper(method) return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return request.Method != method } } -func Any(methods ...string) knife.MiddlewareMatcher { +func MethodAny(methods ...string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { for _, method := range methods { method = strings.ToUpper(method) diff --git a/knife/matcher/path/path.go b/knife/matcher/path.go similarity index 79% rename from knife/matcher/path/path.go rename to knife/matcher/path.go index c00d0a6..9f911d9 100644 --- a/knife/matcher/path/path.go +++ b/knife/matcher/path.go @@ -1,6 +1,6 @@ // 关于路径的匹配器 -package path +package matcher import ( "fmt" @@ -10,15 +10,15 @@ import ( "strings" ) -// Prefix 路径前缀匹配 -func Prefix(prefix string) knife.MiddlewareMatcher { +// PathPrefix 路径前缀匹配 +func PathPrefix(prefix string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { return strings.HasPrefix(request.URL.Path, prefix) } } -// Match 路径正则匹配 -func Match(pattern string) knife.MiddlewareMatcher { +// PathMatch 路径正则匹配 +func PathMatch(pattern string) knife.MiddlewareMatcher { return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { matched, err := isPathMatched(request.URL.Path, pattern) if err != nil { diff --git a/knife/middleware/cache/cache.go b/knife/middleware/cache.go similarity index 99% rename from knife/middleware/cache/cache.go rename to knife/middleware/cache.go index 232a742..e9a9ebc 100644 --- a/knife/middleware/cache/cache.go +++ b/knife/middleware/cache.go @@ -1,4 +1,4 @@ -package cache +package middleware import ( "fmt" diff --git a/knife/middleware/gzip/gzip.go b/knife/middleware/gzip.go similarity index 92% rename from knife/middleware/gzip/gzip.go rename to knife/middleware/gzip.go index 5a572c1..5bd47b8 100644 --- a/knife/middleware/gzip/gzip.go +++ b/knife/middleware/gzip.go @@ -1,4 +1,4 @@ -package gzip +package middleware import ( "bytes" @@ -6,8 +6,8 @@ import ( "knife" ) -// Default 当响应体的大小超过100kb后,进行gzip压缩 -func Default() knife.MiddlewareFunc { +// GzipDefault 当响应体的大小超过100kb后,进行gzip压缩 +func GzipDefault() knife.MiddlewareFunc { return Gzip(100 * 1024) } diff --git a/knife/middleware/proxy/load_balance_proxy.go b/knife/middleware/load_balance_proxy.go similarity index 99% rename from knife/middleware/proxy/load_balance_proxy.go rename to knife/middleware/load_balance_proxy.go index 74a0fb2..66b629d 100644 --- a/knife/middleware/proxy/load_balance_proxy.go +++ b/knife/middleware/load_balance_proxy.go @@ -1,4 +1,4 @@ -package proxy +package middleware import ( "fmt" diff --git a/knife/middleware/proxy/load_balance_proxy_test.go b/knife/middleware/load_balance_proxy_test.go similarity index 99% rename from knife/middleware/proxy/load_balance_proxy_test.go rename to knife/middleware/load_balance_proxy_test.go index 1119de3..bb8d7d2 100644 --- a/knife/middleware/proxy/load_balance_proxy_test.go +++ b/knife/middleware/load_balance_proxy_test.go @@ -1,4 +1,4 @@ -package proxy +package middleware import ( "github.com/stretchr/testify/assert" diff --git a/knife/middleware/logger/logger.go b/knife/middleware/logger.go similarity index 96% rename from knife/middleware/logger/logger.go rename to knife/middleware/logger.go index 75f9d17..2a037e4 100644 --- a/knife/middleware/logger/logger.go +++ b/knife/middleware/logger.go @@ -1,4 +1,4 @@ -package logger +package middleware import ( "knife" diff --git a/knife/middleware/proxy/proxy.go b/knife/middleware/proxy.go similarity index 99% rename from knife/middleware/proxy/proxy.go rename to knife/middleware/proxy.go index 54743fc..be52f32 100644 --- a/knife/middleware/proxy/proxy.go +++ b/knife/middleware/proxy.go @@ -1,4 +1,4 @@ -package proxy +package middleware import ( "knife" diff --git a/knife/middleware/recover/recover.go b/knife/middleware/recover.go similarity index 99% rename from knife/middleware/recover/recover.go rename to knife/middleware/recover.go index 8da664f..0a5d18f 100644 --- a/knife/middleware/recover/recover.go +++ b/knife/middleware/recover.go @@ -1,4 +1,4 @@ -package recover +package middleware import ( "bytes" diff --git a/knife/middleware/stripprefix/strip_prefix.go b/knife/middleware/strip_prefix.go similarity index 90% rename from knife/middleware/stripprefix/strip_prefix.go rename to knife/middleware/strip_prefix.go index 0e503bd..ee6529e 100644 --- a/knife/middleware/stripprefix/strip_prefix.go +++ b/knife/middleware/strip_prefix.go @@ -1,4 +1,4 @@ -package stripprefix +package middleware import ( "knife" @@ -6,7 +6,6 @@ import ( "strings" ) -// StripPrefix 剥离前缀 func StripPrefix(prefix string) knife.MiddlewareFunc { return func(context *knife.Context) { r := context.Req