mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded
Former-commit-id: da4f38eb9034daa49446df3ee529423b98f9b331
This commit is contained in:
145
_examples/routing/main.go
Normal file
145
_examples/routing/main.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
/*
|
||||
Read:
|
||||
"overview"
|
||||
"basic"
|
||||
"dynamic-path"
|
||||
and "reverse" examples if you want to release iris' real power.
|
||||
*/
|
||||
|
||||
const maxBodySize = 1 << 20
|
||||
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) {
|
||||
ctx.HTML(notFoundHTML)
|
||||
})
|
||||
}
|
||||
|
||||
func registerGamesRoutes(app *iris.Application) {
|
||||
gamesMiddleware := func(ctx context.Context) {
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
// party is just a group of routes with the same prefix
|
||||
// and middleware, i.e: "/games" and gamesMiddleware.
|
||||
games := app.Party("/games", gamesMiddleware)
|
||||
{ // braces are optional of course, it's just a style of code
|
||||
|
||||
// "GET" method
|
||||
games.Get("/{gameID:int}/clans", h)
|
||||
games.Get("/{gameID:int}/clans/clan/{clanPublicID:int}", h)
|
||||
games.Get("/{gameID:int}/clans/search", h)
|
||||
|
||||
// "PUT" method
|
||||
games.Put("/{gameID:int}/players/{clanPublicID:int}", h)
|
||||
games.Put("/{gameID:int}/clans/clan/{clanPublicID:int}", h)
|
||||
// remember: "clanPublicID" should not be changed to other routes with the same prefix.
|
||||
// "POST" method
|
||||
games.Post("/{gameID:int}/clans", h)
|
||||
games.Post("/{gameID:int}/players", h)
|
||||
games.Post("/{gameID:int}/clans/{clanPublicID:int}/leave", h)
|
||||
games.Post("/{gameID:int}/clans/{clanPublicID:int}/memberships/application", h)
|
||||
games.Post("/{gameID:int}/clans/{clanPublicID:int}/memberships/application/{action}", h) // {action} == {action:string}
|
||||
games.Post("/{gameID:int}/clans/{clanPublicID:int}/memberships/invitation", h)
|
||||
games.Post("/{gameID:int}/clans/{clanPublicID:int}/memberships/invitation/{action}", h)
|
||||
games.Post("/{gameID:int}/clans/{clanPublicID:int}/memberships/delete", h)
|
||||
games.Post("/{gameID:int}/clans/{clanPublicID:int}/memberships/promote", h)
|
||||
games.Post("/{gameID:int}/clans/{clanPublicID:int}/memberships/demote", h)
|
||||
}
|
||||
}
|
||||
|
||||
func registerSubdomains(app *iris.Application) {
|
||||
mysubdomain := app.Party("mysubdomain.")
|
||||
// http://mysubdomain.myhost.com
|
||||
mysubdomain.Get("/", h)
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
registerErrors(app)
|
||||
registerGamesRoutes(app)
|
||||
registerSubdomains(app)
|
||||
|
||||
app.Handle("GET", "/healthcheck", h)
|
||||
|
||||
// "POST" method
|
||||
// this handler reads raw body from the client/request
|
||||
// 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) {
|
||||
// get request body
|
||||
b, err := ioutil.ReadAll(ctx.Request().Body)
|
||||
// if is larger then send a bad request status
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
ctx.Writef(err.Error())
|
||||
return
|
||||
}
|
||||
// send back the post body
|
||||
ctx.Write(b)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func h(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("Info\n\n")
|
||||
ctx.Writef("Method: %s\nSubdomain: %s\nPath: %s\nParameters length: %d", method, subdomain, path, paramsLen)
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
/*
|
||||
// GET
|
||||
http://localhost:8080/healthcheck
|
||||
http://localhost:8080/games/42/clans
|
||||
http://localhost:8080/games/42/clans/clan/93
|
||||
http://localhost:8080/games/42/clans/search
|
||||
http://mysubdomain.localhost:8080/
|
||||
|
||||
// PUT
|
||||
http://localhost:8080/games/42/players/93
|
||||
http://localhost:8080/games/42/clans/clan/93
|
||||
|
||||
// POST
|
||||
http://localhost:8080/
|
||||
http://localhost:8080/games/42/clans
|
||||
http://localhost:8080/games/42/players
|
||||
http://localhost:8080/games/42/clans/93/leave
|
||||
http://localhost:8080/games/42/clans/93/memberships/application
|
||||
http://localhost:8080/games/42/clans/93/memberships/application/anystring
|
||||
http://localhost:8080/games/42/clans/93/memberships/invitation
|
||||
http://localhost:8080/games/42/clans/93/memberships/invitation/anystring
|
||||
http://localhost:8080/games/42/clans/93/memberships/delete
|
||||
http://localhost:8080/games/42/clans/93/memberships/promote
|
||||
http://localhost:8080/games/42/clans/93/memberships/demote
|
||||
|
||||
// FIRE NOT FOUND
|
||||
http://localhost:8080/coudlntfound
|
||||
*/
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
Reference in New Issue
Block a user