After learning the client implementation of the HTTP protocol interfaces net/http and fasthttp, we will start the development of Server. If we don't learn, we will be surprised. Unexpectedly, these two libraries also support the development of Server. It's too convenient.
Compared with Java, HTTP Server development basically uses Spring or Springboot framework, and always configures various configuration classes and handle objects. The Server development of Golang is very simple because it is very simple, or there is no special unified specification or framework. I have found many implementation methods. The HTTP protocol is still based on net/http and fasthttp, but the handle syntax is diverse.
First review: Practice of HTTP client in Golang language,Golang fasthttp practice.
In terms of Golang language, there may be many libraries to realize a certain function. If you have the opportunity, you should communicate with your peers. You may find a better library to use. Next, I share the implementation demos of six kinds of Server development I learned.
First kind
Based on net/http implementation, this is a relatively basic method. It is not elegant to deal with the mapping relationship between interface and handle, so it is not recommended.
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, "This is net/http Created server The first way") return } fmt.Fprintf(w, task.FunTester) return }), } server.ListenAndServe() log.Println("Start creating HTTP service") }
Second
The second is also based on net/http. This writing syntax can well solve the problem of the first. handle and path have similar configured syntax, and their readability is greatly improved.
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: "This is net/http The second is to create service syntax"}) http.Handle("/", &indexHandler{content: task.FunTester}) http.ListenAndServe(":8001", nil) }
Third
The third is based on net/http and github.com/labstack/echo. The latter mainly provides Echo objects to handle various configurations, including interface and handle mapping. It has rich functions and the best readability.
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) }
Fourth
The fourth is still based on net/http, and introduces the route of github.com/gin-gonic/gin. It seems that the mapping relationship between interface and handle is relatively clear.
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": "This is creating HTTPServer The fourth way", }) context.Status(200) })) } s := &http.Server{ Addr: ":8001", Handler: router, ReadTimeout: 1000 * time.Second, WriteTimeout: 1000 * time.Second, MaxHeaderBytes: 1 << 20, } s.ListenAndServe() }
Fifth
The fifth is developed based on fasthttp, which uses the API s provided by fasthttp. It is readable, and the handle configuration is more like Java.
func TestFastSer(t *testing.T) { address := ":8001" handler := func(ctx *fasthttp.RequestCtx) { path := string(ctx.Path()) switch path { case "/test": ctx.SetBody([]byte("This is fasthttp The first syntax for creating a service")) 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()) } }
Sixth
The sixth one is still based on fasthttp and uses github.com/buaazp/fasthttprouter. It's a little strange that the two are not in a GitHub warehouse. The use of grammar is a little similar to the third way. It is more organized and conducive to reading.
func TestFastSer2(t *testing.T) { address := ":8001" router := fasthttprouter.New() router.GET("/test", func(ctx *fasthttp.RequestCtx) { ctx.Response.SetBody([]byte("This is fasthttp establish server The second grammar of")) }) router.GET("/", func(ctx *fasthttp.RequestCtx) { ctx.Response.SetBody([]byte(task.FunTester)) }) fasthttp.ListenAndServe(address, router.Handler) }
- END -