Go语言HTTPServer开发的六种方式小结

作者:FunTester 时间:2023-06-22 21:48:21 

学完了net/http和fasthttp两个HTTP协议接口的客户端实现,接下来就要开始Server的开发,不学不知道一学吓一跳,居然这两个库还支持Server的开发,太方便了。相比于Java的HTTPServer开发基本上都是使用Spring或者Springboot框架,总是要配置各种配置类,各种handle对象。Golang的Server开发显得非常简单,就是因为特别简单,或者说没有形成特别统一的规范或者框架,我发现了很多实现方式,HTTP协议基于还是net/http和fasthttp,但是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/http和github.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)
}

来源:https://blog.51cto.com/FunTester/4515606

标签:Go,HTTPServer
0
投稿

猜你喜欢

  • python GUI库图形界面开发之PyQt5中QWebEngineView内嵌网页与Python的数据交互传参详细方法实例

    2021-07-28 18:28:45
  • SQL Server 2008中的新日期数据类型

    2009-03-16 15:05:00
  • 有时应该告诉我,但有时不应该告诉我

    2009-03-19 13:40:00
  • JS的get和set使用示例

    2024-05-13 09:35:45
  • python3.6 实现AES加密的示例(pyCryptodome)

    2022-07-05 08:17:08
  • ASP基础教程之学习ASP中子程序的应用

    2008-10-16 10:53:00
  • 关于Python常用模块时间模块time

    2022-06-19 14:12:08
  • Python 3.x踩坑实战汇总

    2021-04-21 12:25:47
  • 多个函数验证同一表单方法

    2007-10-06 22:55:00
  • Pandas实现一列数据分隔为两列

    2021-01-06 04:31:36
  • 几个你不知道的技巧助你写出更优雅的vue.js代码

    2024-05-13 09:14:39
  • Python使用Excel将数据写入多个sheet

    2022-01-20 11:52:08
  • python采用getopt解析命令行输入参数实例

    2022-05-10 11:08:00
  • php模板引擎技术简单实现

    2023-11-14 13:28:37
  • 在JScript中使用RecordSet对象的GetRows方法

    2008-01-16 13:12:00
  • 利用pandas将非数值数据转换成数值的方式

    2023-05-18 15:17:17
  • C语言实现访问及查询MySQL数据库的方法

    2024-01-12 18:01:31
  • taro小程序添加骨架屏的实现代码

    2024-04-19 11:04:04
  • Python模拟FTP文件服务器的操作方法

    2022-11-11 19:53:04
  • golang中数组与切片的区别详析

    2024-04-25 15:08:46
  • asp之家 网络编程 m.aspxhome.com