Skip to content

Commit

Permalink
Revert "refactor(main): 重构包管理"
Browse files Browse the repository at this point in the history
  • Loading branch information
BadKid90s authored Apr 25, 2024
1 parent 5d3ed65 commit 2498885
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 156 deletions.
101 changes: 8 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -48,38 +43,26 @@ func main() {

//Create a middleware chain
//first plan
//chain := planOne()
chain := planOne()

//second plan
//chain := planTwo()

//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
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) {
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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
}

```


Expand Down
46 changes: 21 additions & 25 deletions example/base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,10 +22,10 @@ func main() {
//chain := planThree()

//gzip middleware
//chain := gzipFun()
//chain := gzip()

//cache middleware
//chain := cacheFun()
//chain := cache()

//cache middleware
chain := loadBalance()
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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版本。" +
Expand All @@ -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))
Expand All @@ -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,
Expand All @@ -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))
Expand Down
17 changes: 7 additions & 10 deletions example/matcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 将匹配器进行组合的匹配器

package combination
package matcher

import "knife"

Expand Down
14 changes: 7 additions & 7 deletions knife/matcher/header/header.go → knife/matcher/header.go
Original file line number Diff line number Diff line change
@@ -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, "")
}
Expand Down
Loading

0 comments on commit 2498885

Please sign in to comment.