mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
add two new examples and share the app's specific logger instance with sessions databases and APIBuilder
This commit is contained in:
@@ -17,9 +17,10 @@ func newApp() *iris.Application {
|
||||
|
||||
// Optionally, to minify the HTML5 error response.
|
||||
// Note that minification might be slower, caching is advised.
|
||||
test.UseError(iris.Minify)
|
||||
// test.UseError(iris.Minify)
|
||||
// or pass it to OnErrorCode:
|
||||
// Register error code 404 handler.
|
||||
test.OnErrorCode(iris.StatusNotFound, handleNotFoundTestSubdomain)
|
||||
test.OnErrorCode(iris.StatusNotFound, iris.Minify, handleNotFoundTestSubdomain)
|
||||
|
||||
test.Get("/", testIndex)
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
hosts := map[string]*iris.Application{
|
||||
"mydomain.com": createRoot("www.mydomain.com"), // redirects to www.
|
||||
"www.mydomain.com": createWWW(),
|
||||
"test.mydomain.com": createTest(),
|
||||
}
|
||||
for _, r := range hosts {
|
||||
r.Build()
|
||||
}
|
||||
|
||||
app.Downgrade(func(w http.ResponseWriter, r *http.Request) {
|
||||
host := r.Host
|
||||
if host == "" {
|
||||
host = r.URL.Host
|
||||
}
|
||||
|
||||
if router, ok := hosts[host]; ok {
|
||||
router.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
http.NotFound(w, r)
|
||||
})
|
||||
|
||||
app.Listen(":80")
|
||||
}
|
||||
|
||||
func createRoot(redirectTo string) *iris.Application {
|
||||
app := iris.New()
|
||||
app.Downgrade(func(w http.ResponseWriter, r *http.Request) {
|
||||
fullScheme := "http://"
|
||||
if r.TLS != nil {
|
||||
fullScheme = "https://"
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fullScheme+redirectTo, iris.StatusMovedPermanently)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func createWWW() *iris.Application {
|
||||
app := iris.New()
|
||||
app.Get("/", index)
|
||||
|
||||
users := app.Party("/users")
|
||||
users.Get("/", usersIndex)
|
||||
users.Get("/login", getLogin)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func createTest() *iris.Application {
|
||||
app := iris.New()
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.WriteString("Test Index")
|
||||
})
|
||||
|
||||
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.")
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
@@ -44,9 +42,9 @@ func newApp() *iris.Application {
|
||||
ctx.Writef("hi from www.mydomain.com")
|
||||
})
|
||||
}
|
||||
// See also the "subdomains/redirect" to register redirect router wrappers between subdomains,
|
||||
// See "subdomains/redirect" to register redirect router wrappers between subdomains,
|
||||
// i.e mydomain.com to www.mydomain.com (like facebook does for SEO reasons(;)).
|
||||
|
||||
// And ./www-method example.
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -64,9 +62,7 @@ func main() {
|
||||
// http://www.mydomain.com/contact
|
||||
// http://www.mydomain.com/api/users
|
||||
// http://www.mydomain.com/api/users/42
|
||||
if err := app.Listen("mydomain.com:80"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
app.Listen("mydomain.com:80")
|
||||
}
|
||||
|
||||
func info(ctx iris.Context) {
|
||||
|
||||
51
_examples/routing/subdomains/www/www-method/main.go
Normal file
51
_examples/routing/subdomains/www/www-method/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
// This will create a new "www" subdomain
|
||||
// and redirect root-level domain requests
|
||||
// to that one:
|
||||
www := app.WWW()
|
||||
www.Get("/", info)
|
||||
www.Get("/about", info)
|
||||
www.Get("/contact", info)
|
||||
|
||||
www.PartyFunc("/api/users", func(r iris.Party) {
|
||||
r.Get("/", info)
|
||||
r.Get("/{id:uint64}", info)
|
||||
|
||||
r.Post("/", info)
|
||||
|
||||
r.Put("/{id:uint64}", info)
|
||||
})
|
||||
|
||||
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/hi
|
||||
// http://www.mydomain.com/about
|
||||
// http://www.mydomain.com/contact
|
||||
// http://www.mydomain.com/api/users
|
||||
// http://www.mydomain.com/api/users/42
|
||||
app.Listen("mydomain.com:80")
|
||||
}
|
||||
|
||||
func info(ctx iris.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)
|
||||
}
|
||||
Reference in New Issue
Block a user