1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-26 21:35:56 +00:00
Part 3.


Former-commit-id: 229b86baca4043c69517968318d9a962d2e026d0
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-03-24 02:25:00 +02:00
parent 7c5d7cae05
commit 7a6cf4c5aa
18 changed files with 558 additions and 24 deletions

View File

@@ -0,0 +1,30 @@
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
#-IRIS-For development machine, you have to configure your dns also for online, search google how to do it if you don't know
127.0.0.1 mydomain.com
127.0.0.1 username1.mydomain.com
127.0.0.1 username2.mydomain.com
127.0.0.1 username3.mydomain.com
127.0.0.1 username4.mydomain.com
127.0.0.1 username5.mydomain.com
#-END IRIS-

View File

@@ -0,0 +1,71 @@
// Package main an example on how to catch dynamic subdomains - wildcard.
// On the first example (subdomains_1) we saw how to create routes for static subdomains, subdomains you know that you will have.
// Here we will see an example how to catch unknown subdomains, dynamic subdomains, like username.mydomain.com:8080.
package main
import (
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
)
// register a dynamic-wildcard subdomain to your server machine(dns/...) first, check ./hosts if you use windows.
// run this file and try to redirect: http://username1.mydomain.com:8080/ , http://username2.mydomain.com:8080/ , http://username1.mydomain.com/something, http://username1.mydomain.com/something/sadsadsa
func main() {
app := iris.New()
app.Adapt(iris.DevLogger())
// subdomains works with all available routers, like other features too.
app.Adapt(httprouter.New())
/* Keep note that you can use both type of subdomains (named and wildcard(*.) )
admin.mydomain.com, and for other the Party(*.) but this is not this example's purpose
admin := app.Party("admin.")
{
// admin.mydomain.com
admin.Get("/", func(ctx *iris.Context) {
ctx.Writef("INDEX FROM admin.mydomain.com")
})
// admin.mydomain.com/hey
admin.Get("/hey", func(ctx *iris.Context) {
ctx.Writef("HEY FROM admin.mydomain.com/hey")
})
// admin.mydomain.com/hey2
admin.Get("/hey2", func(ctx *iris.Context) {
ctx.Writef("HEY SECOND FROM admin.mydomain.com/hey")
})
}*/
// no order, you can register subdomains at the end also.
dynamicSubdomains := app.Party("*.")
{
dynamicSubdomains.Get("/", dynamicSubdomainHandler)
dynamicSubdomains.Get("/something", dynamicSubdomainHandler)
dynamicSubdomains.Get("/something/:param1", dynamicSubdomainHandlerWithParam)
}
app.Get("/", func(ctx *iris.Context) {
ctx.Writef("Hello from mydomain.com path: %s", ctx.Path())
})
app.Get("/hello", func(ctx *iris.Context) {
ctx.Writef("Hello from mydomain.com path: %s", ctx.Path())
})
app.Listen("mydomain.com:8080") // for beginners: look ../hosts file
}
func dynamicSubdomainHandler(ctx *iris.Context) {
username := ctx.Subdomain()
ctx.Writef("Hello from dynamic subdomain path: %s, here you can handle the route for dynamic subdomains, handle the user: %s", ctx.Path(), username)
// if http://username4.mydomain.com:8080/ prints:
// Hello from dynamic subdomain path: /, here you can handle the route for dynamic subdomains, handle the user: username4
}
func dynamicSubdomainHandlerWithParam(ctx *iris.Context) {
username := ctx.Subdomain()
ctx.Writef("Hello from dynamic subdomain path: %s, here you can handle the route for dynamic subdomains, handle the user: %s", ctx.Path(), username)
ctx.Writef("THE PARAM1 is: %s", ctx.Param("param1"))
}