mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 11:57:02 +00:00
Update to 7.0.5 | Dynamic and static paths are not in conflict anymore.
Read more at: https://github.com/kataras/iris/blob/master/HISTORY.md Former-commit-id: b636d25c141ebdd5ad095ae9271433876a96e7ff
This commit is contained in:
@@ -20,21 +20,15 @@ func main() {
|
||||
// Until go 1.9 you will have to import that package too, after go 1.9 this will be not be necessary.
|
||||
//
|
||||
// Iris has the easiest and the most powerful routing process you have ever meet.
|
||||
// If you're used to use the "httprouter"
|
||||
// then you don't have to change a thing of a route's path.
|
||||
//
|
||||
// At the same time,
|
||||
// Iris has its own interpeter(yes like a programming language)
|
||||
// for route's path syntax and their dynamic path parameters parsing and evaluation,
|
||||
// I am calling them "macros" for shortcut.
|
||||
// In the following examples we will see only the second option, which has exactly the same speed
|
||||
// compared to "httprouter".
|
||||
// How? It calculates its needs and if not any special regexp needed then it just
|
||||
// registers the route with the underline httprouter's path syntax,
|
||||
// registers the route with the low-level underline path syntax,
|
||||
// otherwise it pre-compiles the regexp and adds the necessary middleware(s).
|
||||
//
|
||||
// Note: the Iris' router follows the "httprouter"'s rules for routes confliction.
|
||||
//
|
||||
// Standard macro types for parameters:
|
||||
// +------------------------+
|
||||
// | {param:string} |
|
||||
@@ -142,6 +136,10 @@ func main() {
|
||||
ctx.Writef("name: %s | level: %s", ctx.Params().Get("name"), ctx.Params().Get("level"))
|
||||
})
|
||||
|
||||
app.Get("/lowercase/static", func(ctx context.Context) {
|
||||
ctx.Writef("static and dynamic paths are not conflicted anymore!")
|
||||
})
|
||||
|
||||
// let's use a trivial custom regexp that validates a single path parameter
|
||||
// which its value is only lowercase letters.
|
||||
|
||||
@@ -160,12 +158,14 @@ func main() {
|
||||
app.Get("/myfiles/{directory:path}", func(ctx context.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/*directory
|
||||
// /myfiles/*
|
||||
|
||||
// "{param}"'s performance is exactly the same of ":param"'s.
|
||||
|
||||
// alternatives -> ":param" for single path parameter and "*paramPath" for wildcard path parameter
|
||||
// acquire them by ctx.Params().Get as always.
|
||||
// alternatives -> ":param" for single path parameter and "*" for wildcard path parameter.
|
||||
// Note these:
|
||||
// if "/mypath/*" then the parameter name is "*".
|
||||
// if "/mypath/{myparam:path}" then the parameter has two names, one is the "*" and the other is the user-defined "myparam".
|
||||
|
||||
// WARNING:
|
||||
// A path parameter name should contain only alphabetical letters, symbols, containing '_' and numbers are NOT allowed.
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
/*
|
||||
Read:
|
||||
"overview"
|
||||
"basic"
|
||||
"dynamic-path"
|
||||
and "reverse" examples if you want to release Iris' real power.
|
||||
|
||||
134
_examples/beginner/routing/overview/main.go
Normal file
134
_examples/beginner/routing/overview/main.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// GET: http://localhost:8080
|
||||
app.Get("/", info)
|
||||
// GET: http://localhost:8080/profile/kataras
|
||||
app.Get("/profile/{username:string}", info)
|
||||
// GET: http://localhost:8080/profile/backups/any/number/of/paths/here
|
||||
app.Get("/profile/backups/{filepath:path}", info)
|
||||
|
||||
// Favicon
|
||||
|
||||
// GET: http://localhost:8080/favicon.ico
|
||||
app.Favicon("./public/images/favicon.ico")
|
||||
|
||||
// Static assets
|
||||
|
||||
// GET: http://localhost:8080/assets/css/bootstrap.min.css
|
||||
// maps to ./public/assets/css/bootstrap.min.css file at system location.
|
||||
// GET: http://localhost:8080/assets/js/react.min.js
|
||||
// maps to ./public/assets/js/react.min.js file at system location.
|
||||
app.StaticWeb("/assets", "./public/assets")
|
||||
|
||||
/* OR
|
||||
|
||||
// GET: http://localhost:8080/js/react.min.js
|
||||
// maps to ./public/assets/js/react.min.js file at system location.
|
||||
app.StaticWeb("/js", "./public/assets/js")
|
||||
|
||||
// GET: http://localhost:8080/css/bootstrap.min.css
|
||||
// maps to ./public/assets/css/bootstrap.min.css file at system location.
|
||||
app.StaticWeb("/css", "./public/assets/css")
|
||||
|
||||
*/
|
||||
|
||||
// Grouping
|
||||
|
||||
usersRoutes := app.Party("/users")
|
||||
// GET: http://localhost:8080/users/help
|
||||
usersRoutes.Get("/help", func(ctx context.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")
|
||||
ctx.Writef("PUT /$ID -- update an existing user\n")
|
||||
ctx.Writef("DELETE /$ID -- delete an existing user\n")
|
||||
})
|
||||
|
||||
// GET: http://localhost:8080/users
|
||||
usersRoutes.Get("/", func(ctx context.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) {
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
ctx.Writef("get user by id: %d", id)
|
||||
})
|
||||
|
||||
// POST: http://localhost:8080/users
|
||||
usersRoutes.Post("/", func(ctx context.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) {
|
||||
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) {
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
ctx.Writef("delete user by id: %d", id)
|
||||
})
|
||||
|
||||
// Subdomains, depends on the host, you have to edit the hosts or nginx/caddy's configuration if you use them.
|
||||
//
|
||||
// See more subdomains examples at _examples/intermediate/subdomains folder.
|
||||
adminRoutes := app.Party("admin.")
|
||||
|
||||
// GET: http://admin.localhost:8080
|
||||
adminRoutes.Get("/", info)
|
||||
// GET: http://admin.localhost:8080/settings
|
||||
adminRoutes.Get("/settings", info)
|
||||
|
||||
// Wildcard/dynamic subdomain
|
||||
dynamicSubdomainRoutes := app.Party("*.")
|
||||
|
||||
// GET: http://any_thing_here.localhost:8080
|
||||
dynamicSubdomainRoutes.Get("/", info)
|
||||
|
||||
// GET: http://localhost:8080/
|
||||
// GET: http://localhost:8080/profile/kataras
|
||||
// GET: http://localhost:8080/profile/backups/any/number/of/paths/here
|
||||
|
||||
// GET: http://localhost:8080/users/help
|
||||
// GET: http://localhost:8080/users
|
||||
// GET: http://localhost:8080/users/42
|
||||
// POST: http://localhost:8080/users
|
||||
// PUT: http://localhost:8080/users
|
||||
// DELETE: http://localhost:8080/users/42
|
||||
|
||||
// GET: http://admin.localhost:8080
|
||||
// GET: http://admin.localhost:8080/settings
|
||||
// GET: http://any_thing_here.localhost:8080
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func info(ctx context.Context) {
|
||||
method := ctx.Method() // the http method requested a server's resource.
|
||||
subdomain := ctx.Subdomain() // the subdomain, if any.
|
||||
|
||||
// the request path (without scheme and host).
|
||||
path := ctx.Path()
|
||||
// how to get all parameters, if we don't know
|
||||
// the names:
|
||||
paramsLen := ctx.Params().Len()
|
||||
|
||||
ctx.Params().Visit(func(name string, value string) {
|
||||
ctx.Writef("%s = %s\n", name, value)
|
||||
})
|
||||
ctx.Writef("\nInfo\n\n")
|
||||
ctx.Writef("Method: %s\nSubdomain: %s\nPath: %s\nParameters length: %d", method, subdomain, path, paramsLen)
|
||||
}
|
||||
Reference in New Issue
Block a user