Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware #10

Open
mybigman opened this issue Jul 21, 2024 · 0 comments
Open

middleware #10

mybigman opened this issue Jul 21, 2024 · 0 comments

Comments

@mybigman
Copy link

There seems to be an issue with the way the middleware handles or I'm not seeing it :/

This fails.

// using github.com/rs/cors
func main() {
	server := grape.New()
	router := grape.NewRouter()
	h := handler{Server: server}

	c := cors.New(cors.Options{
		AllowedOrigins: []string{"http://localhost:8880"},
	})
	router.Use(c.Handler)

	router.Get("/", h.GetSession)
	router.Post("/session", h.CreateSession)

	fmt.Println("starting server port :3003")
	err := router.Serve(":3003")
	if err != nil {
		fmt.Println(err)
	}
}

However when I use the default mux and alice for middleware there is no issues.

// using github.com/justinas/alice
func main() {
	c := cors.New(cors.Options{
		AllowedOrigins: []string{"http://localhost:8880"},
	})

	mux := http.NewServeMux()

	mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte("{\"hello\": \"world\"}"))
	})

	mux.HandleFunc("POST /session", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte("{\"hello\": \"session\"}"))
	})

	fmt.Println("starting")
	chain := alice.New(c.Handler).Then(mux)
	http.ListenAndServe(":3003", chain)
}

Theres a hacky workaround using a catch all

router.Method("OPTIONS", "/*", handleOptions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant