mirror of
https://github.com/kataras/iris.git
synced 2025-12-22 04:17:03 +00:00
8.4.0 | New MVC Features | Refactor examples and godoc for go 1.9 use. Read HISTORY.md.
Former-commit-id: 90c05e743052bc3722e7fefaa0cbb0ed5153a1fb
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -14,23 +13,23 @@ func main() {
|
||||
|
||||
// GET -> HTTP Method
|
||||
// / -> Path
|
||||
// func(ctx context.Context) -> The route's handler.
|
||||
// func(ctx iris.Context) -> The route's handler.
|
||||
//
|
||||
// Third receiver should contains the route's handler(s), they are executed by order.
|
||||
app.Handle("GET", "/", func(ctx context.Context) {
|
||||
app.Handle("GET", "/", func(ctx iris.Context) {
|
||||
// navigate to the middle of $GOPATH/src/github.com/kataras/iris/context/context.go
|
||||
// to overview all context's method (there a lot of them, read that and you will learn how iris works too)
|
||||
ctx.HTML("Hello from " + ctx.Path()) // Hello from /
|
||||
})
|
||||
|
||||
app.Get("/home", func(ctx context.Context) {
|
||||
app.Get("/home", func(ctx iris.Context) {
|
||||
ctx.Writef(`Same as app.Handle("GET", "/", [...])`)
|
||||
})
|
||||
|
||||
app.Get("/donate", donateHandler, donateFinishHandler)
|
||||
|
||||
// Pssst, don't forget dynamic-path example for more "magic"!
|
||||
app.Get("/api/users/{userid:int min(1)}", func(ctx context.Context) {
|
||||
app.Get("/api/users/{userid:int min(1)}", func(ctx iris.Context) {
|
||||
userID, err := ctx.Params().GetInt("userid")
|
||||
|
||||
if err != nil {
|
||||
@@ -45,15 +44,15 @@ func main() {
|
||||
"user_id": userID,
|
||||
})
|
||||
})
|
||||
// app.Post("/", func(ctx context.Context){}) -> for POST http method.
|
||||
// app.Put("/", func(ctx context.Context){})-> for "PUT" http method.
|
||||
// app.Delete("/", func(ctx context.Context){})-> for "DELETE" http method.
|
||||
// app.Options("/", func(ctx context.Context){})-> for "OPTIONS" http method.
|
||||
// app.Trace("/", func(ctx context.Context){})-> for "TRACE" http method.
|
||||
// app.Head("/", func(ctx context.Context){})-> for "HEAD" http method.
|
||||
// app.Connect("/", func(ctx context.Context){})-> for "CONNECT" http method.
|
||||
// app.Patch("/", func(ctx context.Context){})-> for "PATCH" http method.
|
||||
// app.Any("/", func(ctx context.Context){}) for all http methods.
|
||||
// app.Post("/", func(ctx iris.Context){}) -> for POST http method.
|
||||
// app.Put("/", func(ctx iris.Context){})-> for "PUT" http method.
|
||||
// app.Delete("/", func(ctx iris.Context){})-> for "DELETE" http method.
|
||||
// app.Options("/", func(ctx iris.Context){})-> for "OPTIONS" http method.
|
||||
// app.Trace("/", func(ctx iris.Context){})-> for "TRACE" http method.
|
||||
// app.Head("/", func(ctx iris.Context){})-> for "HEAD" http method.
|
||||
// app.Connect("/", func(ctx iris.Context){})-> for "CONNECT" http method.
|
||||
// app.Patch("/", func(ctx iris.Context){})-> for "PATCH" http method.
|
||||
// app.Any("/", func(ctx iris.Context){}) for all http methods.
|
||||
|
||||
// More than one route can contain the same path with a different http mapped method.
|
||||
// You can catch any route creation errors with:
|
||||
@@ -64,13 +63,13 @@ func main() {
|
||||
|
||||
adminRoutes := app.Party("/admin", adminMiddleware)
|
||||
|
||||
adminRoutes.Done(func(ctx context.Context) { // executes always last if ctx.Next()
|
||||
adminRoutes.Done(func(ctx iris.Context) { // executes always last if ctx.Next()
|
||||
ctx.Application().Logger().Infof("response sent to " + ctx.Path())
|
||||
})
|
||||
// adminRoutes.Layout("/views/layouts/admin.html") // set a view layout for these routes, see more at view examples.
|
||||
|
||||
// GET: http://localhost:8080/admin
|
||||
adminRoutes.Get("/", func(ctx context.Context) {
|
||||
adminRoutes.Get("/", func(ctx iris.Context) {
|
||||
// [...]
|
||||
ctx.StatusCode(iris.StatusOK) // default is 200 == iris.StatusOK
|
||||
ctx.HTML("<h1>Hello from admin/</h1>")
|
||||
@@ -79,11 +78,11 @@ func main() {
|
||||
})
|
||||
|
||||
// GET: http://localhost:8080/admin/login
|
||||
adminRoutes.Get("/login", func(ctx context.Context) {
|
||||
adminRoutes.Get("/login", func(ctx iris.Context) {
|
||||
// [...]
|
||||
})
|
||||
// POST: http://localhost:8080/admin/login
|
||||
adminRoutes.Post("/login", func(ctx context.Context) {
|
||||
adminRoutes.Post("/login", func(ctx iris.Context) {
|
||||
// [...]
|
||||
})
|
||||
|
||||
@@ -93,18 +92,18 @@ func main() {
|
||||
{ // braces are optional, it's just type of style, to group the routes visually.
|
||||
|
||||
// http://v1.localhost:8080
|
||||
v1.Get("/", func(ctx context.Context) {
|
||||
v1.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("Version 1 API. go to <a href='" + ctx.Path() + "/api" + "'>/api/users</a>")
|
||||
})
|
||||
|
||||
usersAPI := v1.Party("/api/users")
|
||||
{
|
||||
// http://v1.localhost:8080/api/users
|
||||
usersAPI.Get("/", func(ctx context.Context) {
|
||||
usersAPI.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("All users")
|
||||
})
|
||||
// http://v1.localhost:8080/api/users/42
|
||||
usersAPI.Get("/{userid:int}", func(ctx context.Context) {
|
||||
usersAPI.Get("/{userid:int}", func(ctx iris.Context) {
|
||||
ctx.Writef("user with id: %s", ctx.Params().Get("userid"))
|
||||
})
|
||||
}
|
||||
@@ -113,7 +112,7 @@ func main() {
|
||||
// wildcard subdomains.
|
||||
wildcardSubdomain := app.Party("*.")
|
||||
{
|
||||
wildcardSubdomain.Get("/", func(ctx context.Context) {
|
||||
wildcardSubdomain.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Subdomain can be anything, now you're here from: %s", ctx.Subdomain())
|
||||
})
|
||||
}
|
||||
@@ -137,22 +136,22 @@ func main() {
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func adminMiddleware(ctx context.Context) {
|
||||
func adminMiddleware(ctx iris.Context) {
|
||||
// [...]
|
||||
ctx.Next() // to move to the next handler, or don't that if you have any auth logic.
|
||||
}
|
||||
|
||||
func donateHandler(ctx context.Context) {
|
||||
func donateHandler(ctx iris.Context) {
|
||||
ctx.Writef("Just like an inline handler, but it can be " +
|
||||
"used by other package, anywhere in your project.")
|
||||
|
||||
// let's pass a value to the next handler
|
||||
// Values is the way handlers(or middleware) are communicating between each other.
|
||||
ctx.Values().Set("donate_url", "https://github.com/kataras/iris#buy-me-a-cup-of-coffee")
|
||||
ctx.Values().Set("donate_url", "https://github.com/kataras/iris#-people")
|
||||
ctx.Next() // in order to execute the next handler in the chain, look donate route.
|
||||
}
|
||||
|
||||
func donateFinishHandler(ctx context.Context) {
|
||||
func donateFinishHandler(ctx iris.Context) {
|
||||
// values can be any type of object so we could cast the value to a string
|
||||
// but iris provides an easy to do that, if donate_url is not defined, then it returns an empty string instead.
|
||||
donateURL := ctx.Values().GetString("donate_url")
|
||||
@@ -160,7 +159,7 @@ func donateFinishHandler(ctx context.Context) {
|
||||
ctx.Writef("\n\nDonate sent(?).")
|
||||
}
|
||||
|
||||
func notFoundHandler(ctx context.Context) {
|
||||
func notFoundHandler(ctx iris.Context) {
|
||||
ctx.HTML("Custom route for 404 not found http code, here you can render a view, html, json <b>any valid response</b>.")
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
@@ -25,7 +24,7 @@ var owner = &Owner{
|
||||
// Let's implement a context which will give us access
|
||||
// to the client's Session with a trivial `ctx.Session()` call.
|
||||
type Context struct {
|
||||
context.Context
|
||||
iris.Context
|
||||
session *sessions.Session
|
||||
}
|
||||
|
||||
@@ -49,7 +48,7 @@ var contextPool = sync.Pool{New: func() interface{} {
|
||||
return &Context{}
|
||||
}}
|
||||
|
||||
func acquire(original context.Context) *Context {
|
||||
func acquire(original iris.Context) *Context {
|
||||
ctx := contextPool.Get().(*Context)
|
||||
ctx.Context = original // set the context to the original one in order to have access to iris's implementation.
|
||||
ctx.session = nil // reset the session
|
||||
@@ -62,8 +61,8 @@ func release(ctx *Context) {
|
||||
|
||||
// Handler will convert our handler of func(*Context) to an iris Handler,
|
||||
// in order to be compatible with the HTTP API.
|
||||
func Handler(h func(*Context)) context.Handler {
|
||||
return func(original context.Context) {
|
||||
func Handler(h func(*Context)) iris.Handler {
|
||||
return func(original iris.Context) {
|
||||
ctx := acquire(original)
|
||||
h(ctx)
|
||||
release(ctx)
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// In this example you'll just see one use case of .WrapRouter.
|
||||
@@ -20,15 +19,15 @@ func newApp() *iris.Application {
|
||||
|
||||
app := iris.New()
|
||||
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Resource Not found</b>")
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ServeFile("./public/index.html", false)
|
||||
})
|
||||
|
||||
app.Get("/profile/{username}", func(ctx context.Context) {
|
||||
app.Get("/profile/{username}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s", ctx.Params().Get("username"))
|
||||
})
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -15,7 +14,7 @@ func main() {
|
||||
// with a single known paramete and custom http errors, now it's time to see wildcard parameters and macros.
|
||||
|
||||
// iris, like net/http std package registers route's handlers
|
||||
// by a Handler, the iris' type of handler is just a func(ctx context.Context)
|
||||
// by a Handler, the iris' type of handler is just a func(ctx iris.Context)
|
||||
// where context comes from github.com/kataras/iris/context.
|
||||
// Until go 1.9 you will have to import that package too, after go 1.9 this will be not be necessary.
|
||||
//
|
||||
@@ -92,7 +91,7 @@ func main() {
|
||||
// })
|
||||
|
||||
// you can use the "string" type which is valid for a single path parameter that can be anything.
|
||||
app.Get("/username/{name}", func(ctx context.Context) {
|
||||
app.Get("/username/{name}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s", ctx.Params().Get("name"))
|
||||
}) // type is missing = {name:string}
|
||||
|
||||
@@ -116,7 +115,7 @@ func main() {
|
||||
// http://localhost:8080/profile/id>=1
|
||||
// this will throw 404 even if it's found as route on : /profile/0, /profile/blabla, /profile/-1
|
||||
// macro parameter functions are optional of course.
|
||||
app.Get("/profile/{id:int min(1)}", func(ctx context.Context) {
|
||||
app.Get("/profile/{id:int min(1)}", func(ctx iris.Context) {
|
||||
// second parameter is the error but it will always nil because we use macros,
|
||||
// the validaton already happened.
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
@@ -124,7 +123,7 @@ func main() {
|
||||
})
|
||||
|
||||
// to change the error code per route's macro evaluator:
|
||||
app.Get("/profile/{id:int min(1)}/friends/{friendid:int min(1) else 504}", func(ctx context.Context) {
|
||||
app.Get("/profile/{id:int min(1)}/friends/{friendid:int min(1) else 504}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
friendid, _ := ctx.Params().GetInt("friendid")
|
||||
ctx.Writef("Hello id: %d looking for friend id: ", id, friendid)
|
||||
@@ -132,11 +131,11 @@ func main() {
|
||||
|
||||
// http://localhost:8080/game/a-zA-Z/level/0-9
|
||||
// remember, alphabetical is lowercase or uppercase letters only.
|
||||
app.Get("/game/{name:alphabetical}/level/{level:int}", func(ctx context.Context) {
|
||||
app.Get("/game/{name:alphabetical}/level/{level:int}", func(ctx iris.Context) {
|
||||
ctx.Writef("name: %s | level: %s", ctx.Params().Get("name"), ctx.Params().Get("level"))
|
||||
})
|
||||
|
||||
app.Get("/lowercase/static", func(ctx context.Context) {
|
||||
app.Get("/lowercase/static", func(ctx iris.Context) {
|
||||
ctx.Writef("static and dynamic paths are not conflicted anymore!")
|
||||
})
|
||||
|
||||
@@ -144,18 +143,18 @@ func main() {
|
||||
// which its value is only lowercase letters.
|
||||
|
||||
// http://localhost:8080/lowercase/anylowercase
|
||||
app.Get("/lowercase/{name:string regexp(^[a-z]+)}", func(ctx context.Context) {
|
||||
app.Get("/lowercase/{name:string regexp(^[a-z]+)}", func(ctx iris.Context) {
|
||||
ctx.Writef("name should be only lowercase, otherwise this handler will never executed: %s", ctx.Params().Get("name"))
|
||||
})
|
||||
|
||||
// http://localhost:8080/single_file/app.js
|
||||
app.Get("/single_file/{myfile:file}", func(ctx context.Context) {
|
||||
app.Get("/single_file/{myfile:file}", func(ctx iris.Context) {
|
||||
ctx.Writef("file type validates if the parameter value has a form of a file name, got: %s", ctx.Params().Get("myfile"))
|
||||
})
|
||||
|
||||
// http://localhost:8080/myfiles/any/directory/here/
|
||||
// this is the only macro type that accepts any number of path segments.
|
||||
app.Get("/myfiles/{directory:path}", func(ctx context.Context) {
|
||||
app.Get("/myfiles/{directory:path}", func(ctx iris.Context) {
|
||||
ctx.Writef("path type accepts any number of path segments, path after /myfiles/ is: %s", ctx.Params().Get("directory"))
|
||||
}) // for wildcard path (any number of path segments) without validation you can use:
|
||||
// /myfiles/*
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -42,30 +41,30 @@ func main() {
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
}
|
||||
|
||||
func h(ctx context.Context) {
|
||||
func h(ctx iris.Context) {
|
||||
param := ctx.Params().Get("p")
|
||||
ctx.WriteString(param)
|
||||
}
|
||||
|
||||
func staticWildcardH(ctx context.Context) {
|
||||
func staticWildcardH(ctx iris.Context) {
|
||||
param := ctx.Params().Get("p")
|
||||
ctx.WriteString("from staticWildcardH: param=" + param)
|
||||
}
|
||||
|
||||
func other(ctx context.Context) {
|
||||
func other(ctx iris.Context) {
|
||||
param := ctx.Params().Get("paramother")
|
||||
ctx.Writef("from other: %s", param)
|
||||
}
|
||||
|
||||
func other2(ctx context.Context) {
|
||||
func other2(ctx iris.Context) {
|
||||
param := ctx.Params().Get("paramothersecond")
|
||||
ctx.Writef("from other2: %s", param)
|
||||
}
|
||||
|
||||
func staticPath(ctx context.Context) {
|
||||
func staticPath(ctx iris.Context) {
|
||||
ctx.Writef("from the static path(/): %s", ctx.Path())
|
||||
}
|
||||
|
||||
func staticPathOther2(ctx context.Context) {
|
||||
func staticPathOther2(ctx iris.Context) {
|
||||
ctx.Writef("from the static path(/other2/static2): %s", ctx.Path())
|
||||
}
|
||||
|
||||
@@ -2,26 +2,25 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.OnErrorCode(iris.StatusInternalServerError, func(ctx context.Context) {
|
||||
app.OnErrorCode(iris.StatusInternalServerError, func(ctx iris.Context) {
|
||||
ctx.HTML("Message: <b>" + ctx.Values().GetString("message") + "</b>")
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML(`Click <a href="/my500">here</a> to fire the 500 status code`)
|
||||
})
|
||||
|
||||
app.Get("/my500", func(ctx context.Context) {
|
||||
app.Get("/my500", func(ctx iris.Context) {
|
||||
ctx.Values().Set("message", "this is the error message")
|
||||
ctx.StatusCode(500)
|
||||
})
|
||||
|
||||
app.Get("/u/{firstname:alphabetical}", func(ctx context.Context) {
|
||||
app.Get("/u/{firstname:alphabetical}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s", ctx.Params().Get("firstname"))
|
||||
})
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -20,13 +19,13 @@ const notFoundHTML = "<h1> custom http error page </h1>"
|
||||
|
||||
func registerErrors(app *iris.Application) {
|
||||
// set a custom 404 handler
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||
ctx.HTML(notFoundHTML)
|
||||
})
|
||||
}
|
||||
|
||||
func registerGamesRoutes(app *iris.Application) {
|
||||
gamesMiddleware := func(ctx context.Context) {
|
||||
gamesMiddleware := func(ctx iris.Context) {
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
@@ -107,7 +106,7 @@ func newApp() *iris.Application {
|
||||
// and sends back the same body
|
||||
// remember, we have limit to that body in order
|
||||
// to protect ourselves from "over heating".
|
||||
app.Post("/", context.LimitRequestBodySize(maxBodySize), func(ctx context.Context) {
|
||||
app.Post("/", iris.LimitRequestBodySize(maxBodySize), func(ctx iris.Context) {
|
||||
// get request body
|
||||
b, err := ioutil.ReadAll(ctx.Request().Body)
|
||||
// if is larger then send a bad request status
|
||||
@@ -123,7 +122,7 @@ func newApp() *iris.Application {
|
||||
return app
|
||||
}
|
||||
|
||||
func h(ctx context.Context) {
|
||||
func h(ctx iris.Context) {
|
||||
method := ctx.Method() // the http method requested a server's resource.
|
||||
subdomain := ctx.Subdomain() // the subdomain, if any.
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -44,7 +43,7 @@ func main() {
|
||||
|
||||
usersRoutes := app.Party("/users")
|
||||
// GET: http://localhost:8080/users/help
|
||||
usersRoutes.Get("/help", func(ctx context.Context) {
|
||||
usersRoutes.Get("/help", func(ctx iris.Context) {
|
||||
ctx.Writef("GET / -- fetch all users\n")
|
||||
ctx.Writef("GET /$ID -- fetch a user by id\n")
|
||||
ctx.Writef("POST / -- create new user\n")
|
||||
@@ -53,32 +52,32 @@ func main() {
|
||||
})
|
||||
|
||||
// GET: http://localhost:8080/users
|
||||
usersRoutes.Get("/", func(ctx context.Context) {
|
||||
usersRoutes.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("get all users")
|
||||
})
|
||||
|
||||
// GET: http://localhost:8080/users/42
|
||||
// **/users/42 and /users/help works after iris version 7.0.5**
|
||||
usersRoutes.Get("/{id:int}", func(ctx context.Context) {
|
||||
usersRoutes.Get("/{id:int}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
ctx.Writef("get user by id: %d", id)
|
||||
})
|
||||
|
||||
// POST: http://localhost:8080/users
|
||||
usersRoutes.Post("/", func(ctx context.Context) {
|
||||
usersRoutes.Post("/", func(ctx iris.Context) {
|
||||
username, password := ctx.PostValue("username"), ctx.PostValue("password")
|
||||
ctx.Writef("create user for username= %s and password= %s", username, password)
|
||||
})
|
||||
|
||||
// PUT: http://localhost:8080/users
|
||||
usersRoutes.Put("/{id:int}", func(ctx context.Context) {
|
||||
usersRoutes.Put("/{id:int}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetInt("id") // or .Get to get its string represatantion.
|
||||
username := ctx.PostValue("username")
|
||||
ctx.Writef("update user for id= %d and new username= %s", id, username)
|
||||
})
|
||||
|
||||
// DELETE: http://localhost:8080/users/42
|
||||
usersRoutes.Delete("/{id:int}", func(ctx context.Context) {
|
||||
usersRoutes.Delete("/{id:int}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
ctx.Writef("delete user by id: %d", id)
|
||||
})
|
||||
@@ -118,7 +117,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func info(ctx context.Context) {
|
||||
func info(ctx iris.Context) {
|
||||
method := ctx.Method() // the http method requested a server's resource.
|
||||
subdomain := ctx.Subdomain() // the subdomain, if any.
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
@@ -12,7 +11,7 @@ func main() {
|
||||
// you normally don't need it because of the {{ urlpath "routename" "path" "values" "here"}}
|
||||
rv := router.NewRoutePathReverser(app)
|
||||
|
||||
myroute := app.Get("/anything/{anythingparameter:path}", func(ctx context.Context) {
|
||||
myroute := app.Get("/anything/{anythingparameter:path}", func(ctx iris.Context) {
|
||||
paramValue := ctx.Params().Get("anythingparameter")
|
||||
ctx.Writef("The path after /anything is: %s", paramValue)
|
||||
})
|
||||
@@ -20,13 +19,13 @@ func main() {
|
||||
myroute.Name = "myroute"
|
||||
|
||||
// useful for links, although iris' view engine has the {{ urlpath "routename" "path values"}} already.
|
||||
app.Get("/reverse_myroute", func(ctx context.Context) {
|
||||
app.Get("/reverse_myroute", func(ctx iris.Context) {
|
||||
myrouteRequestPath := rv.Path(myroute.Name, "any/path")
|
||||
ctx.HTML("Should be <b>/anything/any/path</b>: " + myrouteRequestPath)
|
||||
})
|
||||
|
||||
// execute a route, similar to redirect but without redirect :)
|
||||
app.Get("/execute_myroute", func(ctx context.Context) {
|
||||
app.Get("/execute_myroute", func(ctx iris.Context) {
|
||||
ctx.Exec("GET", "/anything/any/path") // like it was called by the client.
|
||||
})
|
||||
|
||||
|
||||
@@ -2,13 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
none := app.None("/invisible/{username}", func(ctx context.Context) {
|
||||
none := app.None("/invisible/{username}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s with method: %s", ctx.Values().GetString("username"), ctx.Method())
|
||||
|
||||
if from := ctx.Values().GetString("from"); from != "" {
|
||||
@@ -16,7 +15,7 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/change", func(ctx context.Context) {
|
||||
app.Get("/change", func(ctx iris.Context) {
|
||||
|
||||
if none.IsOnline() {
|
||||
none.Method = iris.MethodNone
|
||||
@@ -28,7 +27,7 @@ func main() {
|
||||
app.RefreshRouter()
|
||||
})
|
||||
|
||||
app.Get("/execute", func(ctx context.Context) {
|
||||
app.Get("/execute", func(ctx iris.Context) {
|
||||
// same as navigating to "http://localhost:8080/invisible/iris" when /change has being invoked and route state changed
|
||||
// from "offline" to "online"
|
||||
ctx.Values().Set("from", "/execute") // values and session can be shared when calling Exec from a "foreign" context.
|
||||
|
||||
Reference in New Issue
Block a user