github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_z_example_init_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	"github.com/gogf/gf/frame/g"
    11  	"github.com/gogf/gf/net/ghttp"
    12  	"time"
    13  )
    14  
    15  func init() {
    16  	p := 8999
    17  	s := g.Server(p)
    18  	// HTTP method handlers.
    19  	s.Group("/", func(group *ghttp.RouterGroup) {
    20  		group.GET("/", func(r *ghttp.Request) {
    21  			r.Response.Writef(
    22  				"GET: query: %d, %s",
    23  				r.GetQueryInt("id"),
    24  				r.GetQueryString("name"),
    25  			)
    26  		})
    27  		group.PUT("/", func(r *ghttp.Request) {
    28  			r.Response.Writef(
    29  				"PUT: form: %d, %s",
    30  				r.GetFormInt("id"),
    31  				r.GetFormString("name"),
    32  			)
    33  		})
    34  		group.POST("/", func(r *ghttp.Request) {
    35  			r.Response.Writef(
    36  				"POST: form: %d, %s",
    37  				r.GetFormInt("id"),
    38  				r.GetFormString("name"),
    39  			)
    40  		})
    41  		group.DELETE("/", func(r *ghttp.Request) {
    42  			r.Response.Writef(
    43  				"DELETE: form: %d, %s",
    44  				r.GetFormInt("id"),
    45  				r.GetFormString("name"),
    46  			)
    47  		})
    48  		group.HEAD("/", func(r *ghttp.Request) {
    49  			r.Response.Write("head")
    50  		})
    51  		group.OPTIONS("/", func(r *ghttp.Request) {
    52  			r.Response.Write("options")
    53  		})
    54  	})
    55  	// Client chaining operations handlers.
    56  	s.Group("/", func(group *ghttp.RouterGroup) {
    57  		group.ALL("/header", func(r *ghttp.Request) {
    58  			r.Response.Writef(
    59  				"Span-Id: %s, Trace-Id: %s",
    60  				r.Header.Get("Span-Id"),
    61  				r.Header.Get("Trace-Id"),
    62  			)
    63  		})
    64  		group.ALL("/cookie", func(r *ghttp.Request) {
    65  			r.Response.Writef(
    66  				"SessionId: %s",
    67  				r.Cookie.Get("SessionId"),
    68  			)
    69  		})
    70  		group.ALL("/json", func(r *ghttp.Request) {
    71  			r.Response.Writef(
    72  				"Content-Type: %s, id: %d",
    73  				r.Header.Get("Content-Type"),
    74  				r.GetInt("id"),
    75  			)
    76  		})
    77  	})
    78  	// Other testing handlers.
    79  	s.Group("/var", func(group *ghttp.RouterGroup) {
    80  		group.ALL("/json", func(r *ghttp.Request) {
    81  			r.Response.Write(`{"id":1,"name":"john"}`)
    82  		})
    83  		group.ALL("/jsons", func(r *ghttp.Request) {
    84  			r.Response.Write(`[{"id":1,"name":"john"}, {"id":2,"name":"smith"}]`)
    85  		})
    86  	})
    87  	s.SetAccessLogEnabled(false)
    88  	s.SetDumpRouterMap(false)
    89  	s.SetPort(p)
    90  	err := s.Start()
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	time.Sleep(time.Millisecond * 500)
    95  }