/
http_redirect.go
49 lines (47 loc) Β· 1.27 KB
/
http_redirect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package dsl
import (
"goa.design/goa/v3/eval"
"goa.design/goa/v3/expr"
)
// Redirect indicates that HTTP requests reply to the request with a redirect.
// The logic is the same as the standard http package Redirect function.
//
// Redirect must appear in a HTTP endpoint expression or a HTTP file server
// expression.
//
// Redirect accepts 2 arguments. The first argument is the URL that is being
// redirected to. The second argument is the HTTP status code.
//
// Example:
//
// 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)
// })
// })
//
func Redirect(url string, code int) {
redirect := &expr.HTTPRedirectExpr{
URL: url,
StatusCode: code,
}
switch actual := eval.Current().(type) {
case *expr.HTTPEndpointExpr:
redirect.Parent = actual
actual.Redirect = redirect
case *expr.HTTPFileServerExpr:
redirect.Parent = actual
actual.Redirect = redirect
default:
eval.IncompatibleDSL()
}
}