1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +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:
kataras
2017-08-27 20:35:23 +03:00
parent a2de506f80
commit 42b123975c
100 changed files with 405 additions and 981 deletions

View File

@@ -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>.")
}