Skip to content

Commit

Permalink
refactor(main): 更新README
Browse files Browse the repository at this point in the history
  • Loading branch information
BadKid90s committed Apr 25, 2024
1 parent 00e5d35 commit d1bc502
Showing 1 changed file with 93 additions and 8 deletions.
101 changes: 93 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ Add and initialize your program and execute it, that's it!
package main

import (
"fmt"
"knife"
"knife/middleware"
"knife/middleware/cache"
"knife/middleware/gzip"
"knife/middleware/logger"
"knife/middleware/proxy"
"knife/middleware/recover"
"log"
"net/http"
"time"
Expand All @@ -43,26 +48,38 @@ 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
_ = http.ListenAndServe(":8080", chain)
err := http.ListenAndServe(":8080", chain)
if err != nil {
panic(err)
}
}

// plan one
func planOne() *knife.Chain {
//create middleware chain
chain := knife.NewChain()
//add logger middleware
chain.Use(middleware.Logger())
chain.Use(logger.Logger())
//add recover middleware
chain.Use(middleware.Recover())
chain.Use(recover.Recover())

//add custom implemented middleware
chain.Use(func(context *knife.Context) {
Expand All @@ -80,7 +97,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(middleware.Logger(), middleware.Recover())
chain := knife.NewChain(logger.Logger(), recover.Recover())

//add custom implemented middleware
chain.Use(func(context *knife.Context) {
Expand All @@ -99,8 +116,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(middleware.Logger()).
Use(middleware.Recover()).
Use(logger.Logger()).
Use(recover.Recover()).
Use(func(context *knife.Context) {
start := time.Now()
defer func() {
Expand All @@ -113,6 +130,74 @@ 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

0 comments on commit d1bc502

Please sign in to comment.