目录
  • 第一种
  • 第二种
  • 第三种
  • 第四种
  • 第五种
  • 第六种

学完了​​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)
}

到此这篇关于go语言httpserver开发的六种实现小结的文章就介绍到这了,更多相关go httpserver 内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!