2017-06-15 19:02:08 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-06-15 19:02:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func newApp() *iris.Application {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
app.Get("/", info)
|
|
|
|
app.Get("/about", info)
|
|
|
|
app.Get("/contact", info)
|
|
|
|
|
2018-01-20 04:17:31 +01:00
|
|
|
app.PartyFunc("/api/users", func(r iris.Party) {
|
|
|
|
r.Get("/", info)
|
2018-10-21 18:20:05 +02:00
|
|
|
r.Get("/{id:uint64}", info)
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2018-01-20 04:17:31 +01:00
|
|
|
r.Post("/", info)
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2018-10-21 18:20:05 +02:00
|
|
|
r.Put("/{id:uint64}", info)
|
2018-01-20 04:17:31 +01:00
|
|
|
}) /* <- same as:
|
|
|
|
usersAPI := app.Party("/api/users")
|
|
|
|
{ // those brackets are just syntactic-sugar things.
|
|
|
|
// This method is rarely used but you can make use of it when you want
|
|
|
|
// scoped variables to that code block only.
|
|
|
|
usersAPI.Get/Post...
|
|
|
|
}
|
|
|
|
usersAPI.Get/Post...
|
|
|
|
*/
|
2017-06-15 19:02:08 +02:00
|
|
|
|
|
|
|
www := app.Party("www.")
|
|
|
|
{
|
2018-01-20 04:17:31 +01:00
|
|
|
// Just to show how you can get all routes and copy them to another
|
|
|
|
// party or subdomain:
|
2018-01-25 02:16:49 +01:00
|
|
|
// Get all routes that are registered so far, including all "Parties" and subdomains:
|
2017-06-15 19:02:08 +02:00
|
|
|
currentRoutes := app.GetRoutes()
|
2018-01-20 04:17:31 +01:00
|
|
|
// Register them to the www subdomain/vhost as well:
|
2017-06-15 19:02:08 +02:00
|
|
|
for _, r := range currentRoutes {
|
2018-01-25 02:16:49 +01:00
|
|
|
www.Handle(r.Method, r.Tmpl().Src, r.Handlers...)
|
2017-06-15 19:02:08 +02:00
|
|
|
}
|
2018-01-25 02:16:49 +01:00
|
|
|
|
|
|
|
// http://www.mydomain.com/hi
|
|
|
|
www.Get("/hi", func(ctx iris.Context) {
|
|
|
|
ctx.Writef("hi from www.mydomain.com")
|
|
|
|
})
|
2017-06-15 19:02:08 +02:00
|
|
|
}
|
2018-01-20 04:17:31 +01:00
|
|
|
// See also the "subdomains/redirect" to register redirect router wrappers between subdomains,
|
|
|
|
// i.e mydomain.com to www.mydomain.com (like facebook does for SEO reasons(;)).
|
2017-06-15 19:02:08 +02:00
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := newApp()
|
2017-07-10 17:32:42 +02:00
|
|
|
// 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
|
2018-01-20 04:17:31 +01:00
|
|
|
// http://www.mydomain.com/hi
|
2017-07-10 17:32:42 +02:00
|
|
|
// 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 {
|
2017-06-15 19:02:08 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
func info(ctx iris.Context) {
|
2017-06-15 19:02:08 +02:00
|
|
|
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)
|
|
|
|
}
|