You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/examples/http/cors/main.go

37 lines
672 B

package main
import (
"log"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/gorilla/handlers"
)
func hello(ctx http.Context) error {
name := ctx.Vars().Get("name")
return ctx.String(200, "hellowolrd "+name)
}
func main() {
httpSrv := http.NewServer(
http.Address(":8000"),
http.Filter(handlers.CORS(
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedHeaders([]string{"GET", "POST"}),
)),
)
route := httpSrv.Route("/")
route.GET("/helloworld/{name}", hello)
app := kratos.New(
kratos.Name("cors"),
kratos.Server(
httpSrv,
),
)
if err := app.Run(); err != nil {
log.Fatal(err)
}
}