程序
package main
import (
"net/http"
"github.com/xgfone/ship/v5"
"github.com/xgfone/ship/v5/middleware"
)
func main() {
cors := middleware.CORS(&middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
})
r := ship.Default()
r.Use(cors)
r.Route("/ping").GET(func(c *ship.Context) error {
return c.Text(http.StatusOK, "pong")
})
srv := &http.Server{
Addr: ":8080",
Handler: r,
}
srv.ListenAndServe()
}
复现
$ curl -i -XOPTIONS http://127.0.0.1:8080/balabala -H "Origin: http://example.com" -H "Access-Control-Request-Method: GET"
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Date: Wed, 23 Jul 2025 10:36:51 GMT
Content-Length: 9
Not Found
程序
复现