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

Add a DSL Redirect #2830

Merged
merged 7 commits into from May 7, 2021
Merged

Add a DSL Redirect #2830

merged 7 commits into from May 7, 2021

Conversation

tchssk
Copy link
Member

@tchssk tchssk commented May 5, 2021

This pull request adds a new DSL to define a HTTP redirect.

The DSL can be used like below:

var _ = Service("service", func() {
	Method("method", func() {
		HTTP(func() {
			GET("/resources")
			Redirect("/redirect/dest", StatusMovedPermanently)
		})
	})
})

var _ = Service("service", func() {
	Files("/file.json", "/path/to/file.json", func() {
		Redirect("/redirect/dest", StatusMovedPermanently)
	})
})

The generated server handler initalizer for the HTTP endpoint redirect is:

func NewMethodHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "Method")
		ctx = context.WithValue(ctx, goa.ServiceKey, "Service")
		http.Redirect(w, r, "/redirect/dest", http.StatusMovedPermanently)
	})
}

and the generated server mounter for the file server redirect is:

func Mount(mux goahttp.Muxer) {
	MountPathToFileJSON(mux, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/redirect/dest", http.StatusMovedPermanently)
	}))
}

@tchssk tchssk added the v3 label May 5, 2021
@tchssk tchssk marked this pull request as ready for review May 5, 2021 04:14
Copy link
Member

@raphael raphael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you for the PR! Just one question: it seems the DSL func argument to Redirect is only there to allow for using Meta. And it doesn't like any metadata attached to redirects would be used anywhere at this point. Would it make sense to simplify and remove the DSL argument and the Meta field or do you have a use for it?

dsl/http_redirect.go Outdated Show resolved Hide resolved
@raphael raphael merged commit 3efab9f into goadesign:v3 May 7, 2021
@tchssk tchssk deleted the v3-dsl-redirect branch May 7, 2021 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants