Skip to content

Commit 6c823e4

Browse files
committed
Add example for kataras#1706 (we already cover that functionality through the rest of the examples, but make it clear for newcomers that, dependencies can be used for that kind of reasons as well)
1 parent e990b23 commit 6c823e4

File tree

3 files changed

+103
-33
lines changed

3 files changed

+103
-33
lines changed

_examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
* [Hello world](mvc/hello-world/main.go)
254254
* [Basic](mvc/basic/main.go)
255255
* [Wildcard](mvc/basic/wildcard/main.go)
256+
* [Default request values](mvc/request-default-values/main.go)
256257
* [Singleton](mvc/singleton)
257258
* [Regexp](mvc/regexp/main.go)
258259
* [Session Controller](mvc/session-controller/main.go)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/kataras/iris/v12"
7+
"github.com/kataras/iris/v12/mvc"
8+
)
9+
10+
// https://github.com/kataras/iris/issues/1706
11+
// When you need that type of logic behind a request input,
12+
// e.g. set default values, the right way to do that is
13+
// to register a request-scoped dependency for that type.
14+
// We have the `Context.ReadQuery(pointer)`
15+
// which you can use to bind a struct value, that value can have default values.
16+
// Here is how you could do that:
17+
func main() {
18+
app := iris.New()
19+
20+
mvcApp := mvc.New(app)
21+
{
22+
mvcApp.Register(paramsDependency)
23+
24+
mvcApp.Handle(new(controller))
25+
}
26+
27+
// http://localhost:8080/records?phone=random&order_by=DESC
28+
// http://localhost:8080/records?phone=random
29+
app.Listen(":8080")
30+
}
31+
32+
type params struct {
33+
CallID string `url:"phone"`
34+
ComDir int `url:"dir"`
35+
CaseUserID string `url:"on"`
36+
StartYear int `url:"sy"`
37+
EndYear int `url:"ey"`
38+
OrderBy string `url:"order_by"`
39+
Offset int `url:"offset"`
40+
}
41+
42+
// As we've read in the previous examples, the paramsDependency
43+
// describes a request-scoped dependency.
44+
// It should accept the iris context (or any previously registered or builtin dependency)
45+
// and it should return the value which will be binded to the
46+
// controller's methods (or fields) - see `GetRecords`.
47+
var paramsDependency = func(ctx iris.Context) params {
48+
p := params{
49+
OrderBy: "ASC", // default value.
50+
}
51+
// Bind the URL values to the "p":
52+
ctx.ReadQuery(&p)
53+
// Or bind a specific URL value by giving a default value:
54+
// p.OrderBy = ctx.URLParamDefault("order_by", "ASC")
55+
//
56+
// OR make checks for default values after ReadXXX,
57+
// e.g. if p.OrderBy == "" {...}
58+
59+
/* More options to read a request:
60+
// Bind the JSON request body to the "p":
61+
ctx.ReadJSON(&p)
62+
// Bind the Form to the "p":
63+
ctx.ReadForm(&p)
64+
// Bind any, based on the client's content-type header:
65+
ctx.ReadBody(&p)
66+
// Bind the http requests to a struct value:
67+
ctx.ReadHeader(&h)
68+
*/
69+
70+
return p
71+
}
72+
73+
type controller struct{}
74+
75+
func (c *controller) GetRecords(stru params) string {
76+
return fmt.Sprintf("%#+v\n", stru)
77+
}

core/host/proxy.go

+25-33
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,14 @@ import (
1717
// the target request will be for /base/dir.
1818
//
1919
// Relative to httputil.NewSingleHostReverseProxy with some additions.
20+
//
21+
// Look `ProxyHandlerRemote` too.
2022
func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
21-
targetQuery := target.RawQuery
2223
director := func(req *http.Request) {
23-
req.URL.Scheme = target.Scheme
24-
req.URL.Host = target.Host
25-
req.Host = target.Host
26-
24+
modifyProxiedRequest(req, target)
2725
req.URL.Path = path.Join(target.Path, req.URL.Path)
28-
29-
if targetQuery == "" || req.URL.RawQuery == "" {
30-
req.URL.RawQuery = targetQuery + req.URL.RawQuery
31-
} else {
32-
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
33-
}
34-
35-
if _, ok := req.Header["User-Agent"]; !ok {
36-
// explicitly disable User-Agent so it's not set to default value
37-
req.Header.Set("User-Agent", "")
38-
}
3926
}
27+
4028
p := &httputil.ReverseProxy{Director: director}
4129

4230
if netutil.IsLoopbackHost(target.Host) {
@@ -49,37 +37,41 @@ func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
4937
return p
5038
}
5139

40+
func modifyProxiedRequest(req *http.Request, target *url.URL) {
41+
req.URL.Scheme = target.Scheme
42+
req.URL.Host = target.Host
43+
req.Host = target.Host
44+
45+
if target.RawQuery == "" || req.URL.RawQuery == "" {
46+
req.URL.RawQuery = target.RawQuery + req.URL.RawQuery
47+
} else {
48+
req.URL.RawQuery = target.RawQuery + "&" + req.URL.RawQuery
49+
}
50+
51+
if _, ok := req.Header["User-Agent"]; !ok {
52+
// explicitly disable User-Agent so it's not set to default value
53+
req.Header.Set("User-Agent", "")
54+
}
55+
}
56+
5257
// ProxyHandlerRemote returns a new ReverseProxy that rewrites
5358
// URLs to the scheme, host, and path provided in target.
5459
// Case 1: req.Host == target.Host
5560
// behavior same as ProxyHandler
5661
// Case 2: req.Host != target.Host
5762
// the target request will be forwarded to the target's url
58-
// insecureSkipVerify indicates enable ssl certificate verification or not
63+
// insecureSkipVerify indicates enable ssl certificate verification or not.
64+
//
65+
// Look `ProxyHandler` too.
5966
func ProxyHandlerRemote(target *url.URL, insecureSkipVerify bool) *httputil.ReverseProxy {
60-
targetQuery := target.RawQuery
6167
director := func(req *http.Request) {
62-
req.URL.Scheme = target.Scheme
68+
modifyProxiedRequest(req, target)
6369

6470
if req.Host != target.Host {
6571
req.URL.Path = target.Path
6672
} else {
6773
req.URL.Path = path.Join(target.Path, req.URL.Path)
6874
}
69-
70-
req.URL.Host = target.Host
71-
req.Host = target.Host
72-
73-
if targetQuery == "" || req.URL.RawQuery == "" {
74-
req.URL.RawQuery = targetQuery + req.URL.RawQuery
75-
} else {
76-
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
77-
}
78-
79-
if _, ok := req.Header["User-Agent"]; !ok {
80-
// explicitly disable User-Agent so it's not set to default value
81-
req.Header.Set("User-Agent", "")
82-
}
8375
}
8476
p := &httputil.ReverseProxy{Director: director}
8577

0 commit comments

Comments
 (0)