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

CORS doesn't work #59

Closed
guerinoni opened this issue Mar 1, 2024 · 9 comments · Fixed by #68
Closed

CORS doesn't work #59

guerinoni opened this issue Mar 1, 2024 · 9 comments · Fixed by #68
Labels
bug Something isn't working cannot reproduce One of the maintainer tried to reproduce the issue, without success
Milestone

Comments

@guerinoni
Copy link
Contributor

To Reproduce
Steps to reproduce the behavior:

Take the basic example and just add

	fuego.Use(s, cors.New(cors.Options{
		AllowedOrigins: []string{"*"},
		AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowedHeaders: []string{"*"},
	}).Handler)

Expected behavior
I have a front end running on localhost:3000 and hitting backend in localhost:8080, I got the error about missing CORS.

If I switch to another example i tried without fuego it is working

server := http.Server{
                Addr: "127.0.0.1:8080",
                Handler: cors.New(cors.Options{
                        AllowedOrigins: []string{"http://localhost:3000"},
                        AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
                }).Handler(mux),
        }

Screenshots
Screenshot 2024-03-01 at 12 59 54 PM

Framework version (please check if it happens with the last Fuego version before posting):
Latest 0.12

Go version (please check if it happens with the last Go version before posting):
Latest: go version go1.22.0 darwin/arm64

@guerinoni guerinoni added the bug Something isn't working label Mar 1, 2024
@EwenQuim
Copy link
Member

EwenQuim commented Mar 1, 2024

Hi @guerinoni !

Are your routes declared before or after the middleware registration ?

@guerinoni
Copy link
Contributor Author

Hi @guerinoni !

Are your routes declared before or after the middleware registration ?

Does not matter, i see the same problem

@guerinoni
Copy link
Contributor Author

func (s *Server) Run() error {
	go s.generateOpenAPI()
	elapsed := time.Since(s.startTime)
	slog.Debug("Server started in "+elapsed.String(), "info", "time between since server creation (fuego.NewServer) and server startup (fuego.Run). Depending on your implementation, there might be things that do not depend on fuego slowing start time")
	slog.Info("Server running ✅ on http://localhost"+s.Server.Addr, "started in", elapsed.String())

	s.Server.Handler = cors.New(cors.Options{
		AllowedOrigins: []string{"*"},
		AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
		AllowedHeaders: []string{"*"},
	}).Handler(s.Mux)

	return s.Server.ListenAndServe()
}

This fixes the problem

@EwenQuim
Copy link
Member

EwenQuim commented Mar 4, 2024

Sorry I cannot reproduce.

I noticed having CORS issues when having a 405 response, is it your case ?

@EwenQuim EwenQuim closed this as completed Mar 4, 2024
@EwenQuim
Copy link
Member

EwenQuim commented Mar 4, 2024

I can help you solve your CORS issue if you want, but I don't think it is related to the framework, it works in my case!


Result on a React app:
Notice that the Access-Control-Allow-Origin is correctly set.
image
image

Code:
image

@EwenQuim EwenQuim added the cannot reproduce One of the maintainer tried to reproduce the issue, without success label Mar 4, 2024
@EwenQuim EwenQuim moved this to Todo in Roadmap Fuego 🔥 Mar 4, 2024
@EwenQuim EwenQuim moved this from Todo to Done in Roadmap Fuego 🔥 Mar 4, 2024
@EwenQuim EwenQuim added this to the v0.13 milestone Mar 4, 2024
@guerinoni
Copy link
Contributor Author

I'm not able to make it work, can you attach your front end application? I can try with your code

@EwenQuim
Copy link
Member

EwenQuim commented Mar 6, 2024

Can you show me the network error ? I want to see the preflight request

@guerinoni
Copy link
Contributor Author

Screenshot 2024-03-06 at 2 18 28 PM

@EwenQuim
Copy link
Member

EwenQuim commented Mar 6, 2024

This is a 405 return code, are you sure that you are calling the correct METHOD for the /signup route?

@EwenQuim EwenQuim mentioned this issue Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cannot reproduce One of the maintainer tried to reproduce the issue, without success
Projects
Development

Successfully merging a pull request may close this issue.

2 participants