github.com/gogf/gf@v1.16.9/.example/net/ghttp/server/middleware/cors.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/gogf/gf/frame/g"
     5  	"github.com/gogf/gf/net/ghttp"
     6  )
     7  
     8  func MiddlewareCORS(r *ghttp.Request) {
     9  	r.Response.CORSDefault()
    10  	r.Middleware.Next()
    11  }
    12  
    13  func main() {
    14  	s := g.Server()
    15  	s.Group("/api.v2", func(group *ghttp.RouterGroup) {
    16  		group.Middleware(MiddlewareCORS)
    17  		group.ALL("/user/list", func(r *ghttp.Request) {
    18  			r.Response.Write("list")
    19  		})
    20  	})
    21  	s.SetPort(8199)
    22  	s.Run()
    23  }