diff --git a/_examples/README.md b/_examples/README.md index 8e0475f7..9d42c9d7 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -23,6 +23,10 @@ Developers should read the official [documentation](https://godoc.org/gopkg.in/k * [Favicon](beginner/favicon/main.go) * [File Server](beginner/file-server/main.go) * [Send Files](beginner/send-files/main.go) + * [Stream Writer](beginner/stream-writer/main.go) + * [Listen UNIX Socket](beginner/listen-unix/main.go) + * [Listen TLS](beginner/listen-tls/main.go) + * [Listen Letsencrypt (Automatic Certifications)](beginner/listen-letsencrypt/main.go) * [Level: Intermediate](intermediate) * [Send An E-mail](intermediate/e-mail/main.go) * [Upload/Read Files](intermediate/upload-files/main.go) @@ -34,6 +38,8 @@ Developers should read the official [documentation](https://godoc.org/gopkg.in/k * [Localization and Internationalization](intermediate/i18n/main.go) * [Recovery](intermediate/recover/main.go) * [Graceful Shutdown](intermediate/graceful-shutdown/main.go) + * [Custom TCP Listener](intermediate/custom-listener/main.go) + * [Custom HTTP Server](intermediate/custom-httpserver/main.go) * [View Engine](intermediate/view) * [Overview](intermediate/view/overview/main.go) * [Embedding Templates Into Executable](intermediate/embedding-templates-into-app) @@ -58,10 +64,15 @@ Developers should read the official [documentation](https://godoc.org/gopkg.in/k * [Secure](intermediate/websockets/secure/main.go) * [Custom Go Client](intermediate/websockets/custom-go-client/main.go) * [Level: Advanced](advanced) + * [Transactions](advanced/transactions/main.go) * [HTTP Testing](advanced/httptest/main_test.go) * [Watch & Compile Typescript source files](advanced/typescript/main.go) * [Cloud Editor](advanced/cloud-editor/main.go) * [Online Visitors](advanced/online-visitors/main.go) * [URL Shortener using BoltDB](advanced/url-shortener/main.go) + * [Subdomains](advanced/subdomains) + * [Single](advanced/subdomains/single/main.go) + * [Multi](advanced/subdomains/multi/main.go) + * [Wildcard](advanced/subdomains/wildcard/main.go) > Take look at the [community examples](https://github.com/iris-contrib/examples) too! diff --git a/_examples/advanced/subdomains/multi/hosts b/_examples/advanced/subdomains/multi/hosts new file mode 100644 index 00000000..6b40c422 --- /dev/null +++ b/_examples/advanced/subdomains/multi/hosts @@ -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- diff --git a/_examples/advanced/subdomains/multi/main.go b/_examples/advanced/subdomains/multi/main.go new file mode 100644 index 00000000..8a7bcbd8 --- /dev/null +++ b/_examples/advanced/subdomains/multi/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "gopkg.in/kataras/iris.v6" + "gopkg.in/kataras/iris.v6/adaptors/httprouter" +) + +func main() { + app := iris.New() + app.Adapt(iris.DevLogger()) + // subdomains works with all available routers, like other features too. + app.Adapt(httprouter.New()) + + /* + * Setup static files + */ + + app.StaticWeb("/assets", "./public/assets") + app.StaticWeb("/upload_resources", "./public/upload_resources") + + dashboard := app.Party("dashboard.") + { + dashboard.Get("/", func(ctx *iris.Context) { + ctx.Writef("HEY FROM dashboard") + }) + } + system := app.Party("system.") + { + system.Get("/", func(ctx *iris.Context) { + ctx.Writef("HEY FROM system") + }) + } + + app.Get("/", func(ctx *iris.Context) { + ctx.Writef("HEY FROM frontend /") + }) + /* test this on firefox, because the domain is not real (because of .local), on firefox this will fail, but you can test it with other domain */ + app.Listen("domain.local:80") // for beginners: look ../hosts file +} diff --git a/_examples/advanced/subdomains/multi/public/assets/images/test.ico b/_examples/advanced/subdomains/multi/public/assets/images/test.ico new file mode 100644 index 00000000..5987ec74 Binary files /dev/null and b/_examples/advanced/subdomains/multi/public/assets/images/test.ico differ diff --git a/_examples/advanced/subdomains/multi/public/upload_resources/iris_favicon_32_32.ico b/_examples/advanced/subdomains/multi/public/upload_resources/iris_favicon_32_32.ico new file mode 100644 index 00000000..5987ec74 Binary files /dev/null and b/_examples/advanced/subdomains/multi/public/upload_resources/iris_favicon_32_32.ico differ diff --git a/_examples/advanced/subdomains/single/hosts b/_examples/advanced/subdomains/single/hosts new file mode 100644 index 00000000..8e5c20d7 --- /dev/null +++ b/_examples/advanced/subdomains/single/hosts @@ -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- diff --git a/_examples/advanced/subdomains/single/main.go b/_examples/advanced/subdomains/single/main.go new file mode 100644 index 00000000..eec51168 --- /dev/null +++ b/_examples/advanced/subdomains/single/main.go @@ -0,0 +1,43 @@ +// Package main register static subdomains, simple as parties, check ./hosts if you use windows +package main + +import ( + "gopkg.in/kataras/iris.v6" + "gopkg.in/kataras/iris.v6/adaptors/httprouter" +) + +func main() { + app := iris.New() + app.Adapt(iris.DevLogger()) + // subdomains works with all available routers, like other features too. + app.Adapt(httprouter.New()) + + // no order, you can register subdomains at the end also. + admin := app.Party("admin.") + { + // admin.mydomain.com + admin.Get("/", func(c *iris.Context) { + c.Writef("INDEX FROM admin.mydomain.com") + }) + // admin.mydomain.com/hey + admin.Get("/hey", func(c *iris.Context) { + c.Writef("HEY FROM admin.mydomain.com/hey") + }) + // admin.mydomain.com/hey2 + admin.Get("/hey2", func(c *iris.Context) { + c.Writef("HEY SECOND FROM admin.mydomain.com/hey") + }) + } + + // mydomain.com/ + app.Get("/", func(c *iris.Context) { + c.Writef("INDEX FROM no-subdomain hey") + }) + + // mydomain.com/hey + app.Get("/hey", func(c *iris.Context) { + c.Writef("HEY FROM no-subdomain hey") + }) + + app.Listen("mydomain.com:80") // for beginners: look ../hosts file +} diff --git a/_examples/advanced/subdomains/wildcard/hosts b/_examples/advanced/subdomains/wildcard/hosts new file mode 100644 index 00000000..743ed62f --- /dev/null +++ b/_examples/advanced/subdomains/wildcard/hosts @@ -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- diff --git a/_examples/advanced/subdomains/wildcard/main.go b/_examples/advanced/subdomains/wildcard/main.go new file mode 100644 index 00000000..4cc48d46 --- /dev/null +++ b/_examples/advanced/subdomains/wildcard/main.go @@ -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")) +} diff --git a/_examples/advanced/transactions/main.go b/_examples/advanced/transactions/main.go new file mode 100644 index 00000000..4f6995f5 --- /dev/null +++ b/_examples/advanced/transactions/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "gopkg.in/kataras/iris.v6" + "gopkg.in/kataras/iris.v6/adaptors/httprouter" +) + +func main() { + app := iris.New() + app.Adapt(iris.DevLogger()) + // subdomains works with all available routers, like other features too. + app.Adapt(httprouter.New()) + + app.Get("/", func(ctx *iris.Context) { + ctx.BeginTransaction(func(t *iris.Transaction) { + // OPTIONAl STEP: , if true then the next transictions will not be executed if this transiction fails + // t.SetScope(iris.RequestTransactionScope) + + // OPTIONAL STEP: + // create a new custom type of error here to keep track of the status code and reason message + err := iris.NewTransactionErrResult() + + // we should use t.Context if we want to rollback on any errors lives inside this function clojure. + t.Context.Text(iris.StatusOK, "Blablabla this should not be sent to the client because we will fill the err with a message and status") + + // virtualize a fake error here, for the shake of the example + fail := true + if fail { + err.StatusCode = iris.StatusInternalServerError + // NOTE: if empty reason then the default or the custom http error will be fired (like ctx.EmitError) + err.Reason = "Error: Virtual failure!!" + } + + // OPTIONAl STEP: + // but useful if we want to post back an error message to the client if the transaction failed. + // if the reason is empty then the transaction completed succesfuly, + // otherwise we rollback the whole response writer's body, + // headers and cookies, status code and everything lives inside this transaction + t.Complete(err) + }) + + ctx.BeginTransaction(func(t *iris.Transaction) { + t.Context.HTML(iris.StatusOK, + "