Skip to content

Commit 4cbd9de

Browse files
committed
apps.Switch/Hosts minor doc improvement
1 parent f29d690 commit 4cbd9de

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

Diff for: apps/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ Example Code:
121121

122122
```go
123123
switcher := Switch(Hosts{
124-
"mydomain.com": app,
125-
"test.mydomain.com": testSubdomainApp,
126-
"otherdomain.com": "appName",
124+
{Pattern: "mydomain.com", Target: app},
125+
{Pattern: "test.mydomain.com", Target: testSubdomainApp},
126+
{Pattern: "otherdomain.com", Target: "appName"},
127127
})
128128
switcher.Listen(":80")
129129
```
@@ -211,7 +211,7 @@ Switch(Join{
211211
App: myapp,
212212
},
213213
Hosts{
214-
{"^test.*$", myapp},
214+
{Pattern: "^test.*$", Target: myapp},
215215
},
216216
})
217217
```

Diff for: apps/switch.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
// Example Code:
1616
//
1717
// switcher := Switch(Hosts{
18-
// "mydomain.com": app,
19-
// "test.mydomain.com": testSubdomainApp,
20-
// "otherdomain.com": "appName",
18+
// { Pattern: "mydomain.com", Target: app },
19+
// { Pattern: "test.mydomain.com", Target: testSubdomainApp },
20+
// { Pattern: "otherdomain.com", "Target: appName" },
2121
// })
2222
// switcher.Listen(":80")
2323
//
@@ -35,6 +35,28 @@ import (
3535
//
3636
// Wrap with the `Join` slice to pass
3737
// more than one provider at the same time.
38+
//
39+
// An alternative way for manually embedding an Iris Application to another one is:
40+
//
41+
// app := iris.New() // root app.
42+
// myOtherApp := api.NewServer(otherServerConfiguration) // embedded app.
43+
// // myOtherApp.Logger().SetLevel("debug")
44+
//
45+
// if err := myOtherApp.Build(); err != nil {
46+
// panic(err)
47+
// }
48+
//
49+
// app.Any("/api/identity/{p:path}", func(ctx iris.Context) {
50+
// apiPath := "/" + ctx.Params().Get("p")
51+
// r := ctx.Request()
52+
// r.URL.Path = apiPath
53+
// r.URL.RawPath = apiPath
54+
// ctx.Params().Remove("p")
55+
//
56+
// myOtherApp.ServeHTTPC(ctx)
57+
// })
58+
//
59+
// app.Listen(":80")
3860
func Switch(provider SwitchProvider, options ...SwitchOption) *iris.Application {
3961
cases := provider.GetSwitchCases()
4062
if len(cases) == 0 {

Diff for: apps/switch_hosts.go

+16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ type (
3636

3737
var _ SwitchProvider = Hosts{}
3838

39+
// AnyDomain is a regexp that matches any domain.
40+
// It can be used as the Pattern field of a Host.
41+
//
42+
// Example:
43+
//
44+
// apps.Switch(apps.Hosts{
45+
// {
46+
// Pattern: "^id.*$", Target: identityApp,
47+
// },
48+
// {
49+
// Pattern: apps.AnyDomain, Target: app,
50+
// },
51+
// }).Listen(":80")
52+
const AnyDomain = `^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z
53+
]{2,3})$`
54+
3955
// GetSwitchCases completes the SwitchProvider.
4056
// It returns a slice of SwitchCase which
4157
// if passed on `Switch` function, they act

0 commit comments

Comments
 (0)