Go語言HTTPServer開發的六種實現

學完了net/httpfasthttp兩個HTTP協議接口的客戶端實現,接下來就要開始Server的開發,不學不知道一學嚇一跳,居然這兩個庫還支持Server的開發,太方便了。

相比于Java的HTTPServer開發基本上都是使用Spring或者Springboot框架,總是要配置各種配置類,各種handle對象。Golang的Server開發顯得非常簡單,就是因為特別簡單,或者說沒有形成特別統一的規范或者框架,我發現了很多實現方式,HTTP協議基于還是net/httpfasthttp,但是handle語法就多種多樣了。

先復習一下:Golang語言HTTP客戶端實踐Golang fasthttp實踐

在Golang語言方面,實現某個功能的庫可能會比較多,有機會還是要多跟同行交流,指不定就發現了更好用的庫。下面我分享我學到的六種Server開發的實現Demo。

第一種

基于net/http實現,這是一種比較基礎的,對于接口和handle映射關系處理并不優雅,不推薦使用。

func TestHttpSer(t *testing.T) {
    server := http.Server{
        Addr: ":8001",
        Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            if strings.Index(r.URL.String(), "test") > 0 {
                fmt.Fprintf(w, "這是net/http創建的server第一種方式")
                return
            }
            fmt.Fprintf(w, task.FunTester)
            return
        }),
    }
    server.ListenAndServe()
    log.Println("開始創建HTTP服務")

}

第二種

第二種也是基于net/http,這種編寫語法可以很好地解決第一種的問題,handle和path有了類似配置的語法,可讀性提高了很多。


type indexHandler struct {
    content string
}

func (ih *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, ih.content)
}

func TestHttpSer2(t *testing.T) {
    http.Handle("/test", &indexHandler{content: "這是net/http第二種創建服務語法"})
    http.Handle("/", &indexHandler{content: task.FunTester})
    http.ListenAndServe(":8001", nil)
}

第三種

第三個基于net/httpgithub.com/labstack/echo,后者主要提供了Echo對象用來處理各類配置包括接口和handle映射,功能很豐富,可讀性最佳。

func TestHttpSer3(t *testing.T) {
    app := echo.New()
    app.Use(middleware.CORSWithConfig(middleware.CORSConfig{
        AllowOrigins: []string{"*"},
        AllowMethods: []string{echo.GET, echo.DELETE, echo.POST, echo.OPTIONS, echo.PUT, echo.HEAD},
        AllowHeaders: []string{echo.HeaderContentType, echo.HeaderAuthorization},
    }))
    app.Group("/test")
    {
        projectGroup := app.Group("/test")
        projectGroup.GET("/", PropertyAddHandler)
    }
    app.Server.Addr = ":8001"
    gracehttp.Serve(app.Server)

}

第四種

第四種依然基于net/http實現,引入了github.com/gin-gonic/gin的路由,看起來接口和handle映射關系比較明晰了。

func TestHttpServer4(t *testing.T) {
    router := gin.New()

    api := router.Group("/okreplay/api")
    {
        api.POST("/submit", gin.HandlerFunc(func(context *gin.Context) {
            context.ShouldBindJSON(map[string]interface{}{
                "code": 0,
                "msg":  "這是創建HTTPServer第四種方式",
            })
            context.Status(200)
        }))

    }
    s := &http.Server{
        Addr:           ":8001",
        Handler:        router,
        ReadTimeout:    1000 * time.Second,
        WriteTimeout:   1000 * time.Second,
        MaxHeaderBytes: 1 << 20,
    }
    s.ListenAndServe()
}

第五種

第五種基于fasthttp開發,使用都是fasthttp提供的API,可讀性尚可,handle配置倒是更像Java了。

func TestFastSer(t *testing.T) {
    address := ":8001"
    handler := func(ctx *fasthttp.RequestCtx) {
        path := string(ctx.Path())
        switch path {
        case "/test":
            ctx.SetBody([]byte("這是fasthttp創建服務的第一種語法"))
        default:
            ctx.SetBody([]byte(task.FunTester))
        }
    }
    s := &fasthttp.Server{
        Handler: handler,
        Name:    "FunTester server",
    }

    if err := s.ListenAndServe(address); err != nil {
        log.Fatal("error in ListenAndServe", err.Error())
    }

}

第六種

第六種依然基于fasthttp,用到了github.com/buaazp/fasthttprouter,有點奇怪兩個居然不在一個GitHub倉庫里。使用語法跟第三種方式有點類似,比較有條理,有利于閱讀。

func TestFastSer2(t *testing.T) {
    address := ":8001"

    router := fasthttprouter.New()
    router.GET("/test", func(ctx *fasthttp.RequestCtx) {
        ctx.Response.SetBody([]byte("這是fasthttp創建server的第二種語法"))
    })
    router.GET("/", func(ctx *fasthttp.RequestCtx) {
        ctx.Response.SetBody([]byte(task.FunTester))
    })
    fasthttp.ListenAndServe(address, router.Handler)
}

Have Fun ~ Tester !

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容