mirror of
https://github.com/kataras/iris.git
synced 2026-01-20 10:26:01 +00:00
Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded
Former-commit-id: da4f38eb9034daa49446df3ee529423b98f9b331
This commit is contained in:
28
_examples/subdomains/multi/hosts
Normal file
28
_examples/subdomains/multi/hosts
Normal file
@@ -0,0 +1,28 @@
|
||||
# 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 domain.local
|
||||
127.0.0.1 system.domain.local
|
||||
127.0.0.1 dashboard.domain.local
|
||||
|
||||
#-END iris-
|
||||
41
_examples/subdomains/multi/main.go
Normal file
41
_examples/subdomains/multi/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
/*
|
||||
* Setup static files
|
||||
*/
|
||||
|
||||
app.StaticWeb("/assets", "./public/assets")
|
||||
app.StaticWeb("/upload_resources", "./public/upload_resources")
|
||||
|
||||
dashboard := app.Party("dashboard.")
|
||||
{
|
||||
dashboard.Get("/", func(ctx context.Context) {
|
||||
ctx.Writef("HEY FROM dashboard")
|
||||
})
|
||||
}
|
||||
system := app.Party("system.")
|
||||
{
|
||||
system.Get("/", func(ctx context.Context) {
|
||||
ctx.Writef("HEY FROM system")
|
||||
})
|
||||
}
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.Writef("HEY FROM frontend /")
|
||||
})
|
||||
// http://domain.local:80
|
||||
// http://dashboard.local
|
||||
// http://system.local
|
||||
// Make sure you prepend the "http" in your browser
|
||||
// because .local is a virtual domain we think to show case you
|
||||
// that you can declare any syntactical correct name as a subdomain in iris.
|
||||
app.Run(iris.Addr("domain.local:80")) // for beginners: look ../hosts file
|
||||
}
|
||||
BIN
_examples/subdomains/multi/public/assets/images/test.ico
Normal file
BIN
_examples/subdomains/multi/public/assets/images/test.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
BIN
_examples/subdomains/multi/public/upload_resources/favicon.ico
Normal file
BIN
_examples/subdomains/multi/public/upload_resources/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
27
_examples/subdomains/single/hosts
Normal file
27
_examples/subdomains/single/hosts
Normal file
@@ -0,0 +1,27 @@
|
||||
# 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 admin.mydomain.com
|
||||
|
||||
#-END iris-
|
||||
47
_examples/subdomains/single/main.go
Normal file
47
_examples/subdomains/single/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Package main register static subdomains, simple as parties, check ./hosts if you use windows
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// subdomains works with all available routers, like other features too.
|
||||
|
||||
// no order, you can register subdomains at the end also.
|
||||
admin := app.Party("admin.")
|
||||
{
|
||||
// admin.mydomain.com
|
||||
admin.Get("/", func(c context.Context) {
|
||||
c.Writef("INDEX FROM admin.mydomain.com")
|
||||
})
|
||||
// admin.mydomain.com/hey
|
||||
admin.Get("/hey", func(c context.Context) {
|
||||
c.Writef("HEY FROM admin.mydomain.com/hey")
|
||||
})
|
||||
// admin.mydomain.com/hey2
|
||||
admin.Get("/hey2", func(c context.Context) {
|
||||
c.Writef("HEY SECOND FROM admin.mydomain.com/hey")
|
||||
})
|
||||
}
|
||||
|
||||
// mydomain.com/
|
||||
app.Get("/", func(c context.Context) {
|
||||
c.Writef("INDEX FROM no-subdomain hey")
|
||||
})
|
||||
|
||||
// mydomain.com/hey
|
||||
app.Get("/hey", func(c context.Context) {
|
||||
c.Writef("HEY FROM no-subdomain hey")
|
||||
})
|
||||
|
||||
// http://admin.mydomain.com
|
||||
// http://admin.mydomain.com/hey
|
||||
// http://admin.mydomain.com/hey2
|
||||
// http://mydomain.com
|
||||
// http://mydomain.com/hey
|
||||
app.Run(iris.Addr("mydomain.com:80")) // for beginners: look ../hosts file
|
||||
}
|
||||
30
_examples/subdomains/wildcard/hosts
Normal file
30
_examples/subdomains/wildcard/hosts
Normal 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-
|
||||
72
_examples/subdomains/wildcard/main.go
Normal file
72
_examples/subdomains/wildcard/main.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// 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 (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// 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()
|
||||
|
||||
/* 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 context.Context) {
|
||||
ctx.Writef("INDEX FROM admin.mydomain.com")
|
||||
})
|
||||
// admin.mydomain.com/hey
|
||||
admin.Get("/hey", func(ctx context.Context) {
|
||||
ctx.Writef("HEY FROM admin.mydomain.com/hey")
|
||||
})
|
||||
// admin.mydomain.com/hey2
|
||||
admin.Get("/hey2", func(ctx context.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/{paramfirst}", dynamicSubdomainHandlerWithParam)
|
||||
}
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.Writef("Hello from mydomain.com path: %s", ctx.Path())
|
||||
})
|
||||
|
||||
app.Get("/hello", func(ctx context.Context) {
|
||||
ctx.Writef("Hello from mydomain.com path: %s", ctx.Path())
|
||||
})
|
||||
|
||||
// http://mydomain.com:8080
|
||||
// http://username1.mydomain.com:8080
|
||||
// http://username2.mydomain.com:8080/something
|
||||
// http://username3.mydomain.com:8080/something/yourname
|
||||
app.Run(iris.Addr("mydomain.com:8080")) // for beginners: look ../hosts file
|
||||
}
|
||||
|
||||
func dynamicSubdomainHandler(ctx context.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 context.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 paramfirst is: %s", ctx.Params().Get("paramfirst"))
|
||||
}
|
||||
25
_examples/subdomains/www/hosts
Normal file
25
_examples/subdomains/www/hosts
Normal file
@@ -0,0 +1,25 @@
|
||||
# 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 www.mydomain.com
|
||||
#-END iris-
|
||||
63
_examples/subdomains/www/main.go
Normal file
63
_examples/subdomains/www/main.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", info)
|
||||
app.Get("/about", info)
|
||||
app.Get("/contact", info)
|
||||
|
||||
usersAPI := app.Party("/api/users")
|
||||
{
|
||||
usersAPI.Get("/", info)
|
||||
usersAPI.Get("/{id:int}", info)
|
||||
|
||||
usersAPI.Post("/", info)
|
||||
|
||||
usersAPI.Put("/{id:int}", info)
|
||||
}
|
||||
|
||||
www := app.Party("www.")
|
||||
{
|
||||
// get all routes that are registered so far, including all "Parties" but subdomains:
|
||||
currentRoutes := app.GetRoutes()
|
||||
// register them to the www subdomain/vhost as well:
|
||||
for _, r := range currentRoutes {
|
||||
www.Handle(r.Method, r.Path, r.Handlers...)
|
||||
}
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
// http://mydomain.com
|
||||
// http://mydomain.com/about
|
||||
// http://imydomain.com/contact
|
||||
// http://mydomain.com/api/users
|
||||
// http://mydomain.com/api/users/42
|
||||
|
||||
// http://www.mydomain.com
|
||||
// http://www.mydomain.com/about
|
||||
// http://www.mydomain.com/contact
|
||||
// http://www.mydomain.com/api/users
|
||||
// http://www.mydomain.com/api/users/42
|
||||
if err := app.Run(iris.Addr("mydomain.com:80")); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func info(ctx context.Context) {
|
||||
method := ctx.Method()
|
||||
subdomain := ctx.Subdomain()
|
||||
path := ctx.Path()
|
||||
|
||||
ctx.Writef("\nInfo\n\n")
|
||||
ctx.Writef("Method: %s\nSubdomain: %s\nPath: %s", method, subdomain, path)
|
||||
}
|
||||
58
_examples/subdomains/www/main_test.go
Normal file
58
_examples/subdomains/www/main_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
type testRoute struct {
|
||||
path string
|
||||
method string
|
||||
subdomain string
|
||||
}
|
||||
|
||||
func (r testRoute) response() string {
|
||||
msg := fmt.Sprintf("\nInfo\n\nMethod: %s\nSubdomain: %s\nPath: %s", r.method, r.subdomain, r.path)
|
||||
return msg
|
||||
}
|
||||
|
||||
func TestSubdomainWWW(t *testing.T) {
|
||||
app := newApp()
|
||||
|
||||
tests := []testRoute{
|
||||
// host
|
||||
{"/", "GET", ""},
|
||||
{"/about", "GET", ""},
|
||||
{"/contact", "GET", ""},
|
||||
{"/api/users", "GET", ""},
|
||||
{"/api/users/42", "GET", ""},
|
||||
{"/api/users", "POST", ""},
|
||||
{"/api/users/42", "PUT", ""},
|
||||
// www sub domain
|
||||
{"/", "GET", "www"},
|
||||
{"/about", "GET", "www"},
|
||||
{"/contact", "GET", "www"},
|
||||
{"/api/users", "GET", "www"},
|
||||
{"/api/users/42", "GET", "www"},
|
||||
{"/api/users", "POST", "www"},
|
||||
{"/api/users/42", "PUT", "www"},
|
||||
}
|
||||
|
||||
host := "localhost:1111"
|
||||
e := httptest.New(t, app, httptest.URL("http://"+host))
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
req := e.Request(test.method, test.path)
|
||||
if subdomain := test.subdomain; subdomain != "" {
|
||||
req.WithURL("http://" + subdomain + "." + host)
|
||||
}
|
||||
|
||||
req.Expect().
|
||||
Status(httptest.StatusOK).
|
||||
Body().Equal(test.response())
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user