Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Ability to set up pre-app handlers #609

Closed
CWharton opened this issue Aug 27, 2017 · 3 comments
Closed

Ability to set up pre-app handlers #609

CWharton opened this issue Aug 27, 2017 · 3 comments

Comments

@CWharton
Copy link

Trying to add CORS using 'github.com/rs/cors' as middleware but I am unable to get access to handlers through the middleware.

@markbates
Copy link
Member

See #610 for the PR for this. Once this PR is merged you'll be able to write the following in the development branch:

func App() *buffalo.App {
	if app == nil {
		app = buffalo.Automatic(buffalo.Options{
			Env:         ENV,
			SessionName: "_coke_session",
			PreWares:    []buffalo.PreWare{cors.Default().Handler},
		})
		app.Use(middleware.SessionSaver)
		if ENV == "development" {
			app.Use(middleware.ParameterLogger)
		}
		if ENV != "test" {
			app.Use(csrf.Middleware)
		}
		app.Use(middleware.PopTransaction(models.DB))

		app.GET("/", HomeHandler).Alias("/foo")
		app.POST("/", HomeHandler)

		app.ServeFiles("/assets", packr.NewBox("../public/assets"))
	}

	return app
}

Using http://www.test-cors.org/#?client_method=GET&client_credentials=false&server_url=http%3A%2F%2Flvh.me%3A3000&server_enable=true&server_status=200&server_credentials=false&server_tabs=remote I'm able to test that the CORS library works.

This feature will be released in 0.9.4, but can be used today by switching to the development branch.

@wiemann
Copy link

wiemann commented Jan 10, 2018

FYI: This does enable CORS, but setting a header for the response a la c.Response().Header().Add("key", ''val") in the action produces a "http: multiple response.WriteHeader calls" warning.

@danikarik
Copy link

danikarik commented Jan 16, 2018

@wiemann Same issue. Results with "http: multiple response.WriteHeader calls" when calling non GET methods.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants