forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (60 loc) · 2.52 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Package main shows how to register a simple 'www' subdomain,
// using the `app.WWW` method, which will register a router wrapper which will
// redirect all 'mydomain.com' requests to 'www.mydomain.com'.
// Check the 'hosts' file to see how to test the 'mydomain.com' on your local machine.
package main
import "github.com/kataras/iris"
const addr = "mydomain.com:80"
func main() {
app := newApp()
// http(s)://mydomain.com, will be redirect to http(s)://www.mydomain.com.
// The `www` variable is the `app.Subdomain("www")`.
//
// app.WWW() wraps the router so it can redirect all incoming requests
// that comes from 'http(s)://mydomain.com/%path%' (www is missing)
// to `http(s)://www.mydomain.com/%path%`.
//
// Try:
// http://mydomain.com -> http://www.mydomain.com
// http://mydomain.com/users -> http://www.mydomain.com/users
// http://mydomain.com/users/login -> http://www.mydomain.com/users/login
app.Run(iris.Addr(addr))
}
func newApp() *iris.Application {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.Writef("This will never be executed.")
})
www := app.Subdomain("www") // <- same as app.Party("www.")
www.Get("/", index)
// www is an `iris.Party`, use it like you already know, like grouping routes.
www.PartyFunc("/users", func(p iris.Party) { // <- same as www.Party("/users").Get(...)
p.Get("/", usersIndex)
p.Get("/login", getLogin)
})
// redirects mydomain.com/%anypath% to www.mydomain.com/%anypath%.
// First argument is the 'from' and second is the 'to/target'.
app.SubdomainRedirect(app, www)
// SubdomainRedirect works for multi-level subdomains as well:
// subsub := www.Subdomain("subsub") // subsub.www.mydomain.com
// subsub.Get("/", func(ctx iris.Context) { ctx.Writef("subdomain is: " + ctx.Subdomain()) })
// app.SubdomainRedirect(subsub, www)
//
// If you need to redirect any subdomain to 'www' then:
// app.SubdomainRedirect(app.WildcardSubdomain(), www)
// If you need to redirect from a subdomain to the root domain then:
// app.SubdomainRedirect(app.Subdomain("mysubdomain"), app)
//
// Note that app.Party("mysubdomain.") and app.Subdomain("mysubdomain")
// is the same exactly thing, the difference is that the second can omit the last dot('.').
return app
}
func index(ctx iris.Context) {
ctx.Writef("This is the www.mydomain.com endpoint.")
}
func usersIndex(ctx iris.Context) {
ctx.Writef("This is the www.mydomain.com/users endpoint.")
}
func getLogin(ctx iris.Context) {
ctx.Writef("This is the www.mydomain.com/users/login endpoint.")
}