mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
replace ioutil with io package and other minor improvements
This commit is contained in:
@@ -377,8 +377,9 @@ func (api *APIBuilder) RegisterDependency(dependencies ...interface{}) {
|
||||
// (see ConfigureContainer().RegisterDependency)
|
||||
// or leave the framework to parse the request and fill the values accordingly.
|
||||
// The output of the "handlersFn" can be any output result:
|
||||
// custom structs <T>, string, []byte, int, error,
|
||||
// a combination of the above, hero.Result(hero.View | hero.Response) and more.
|
||||
//
|
||||
// custom structs <T>, string, []byte, int, error,
|
||||
// a combination of the above, hero.Result(hero.View | hero.Response) and more.
|
||||
//
|
||||
// If more than one handler function is registered
|
||||
// then the execution happens without the nessecity of the `Context.Next` method,
|
||||
@@ -390,22 +391,22 @@ func (api *APIBuilder) RegisterDependency(dependencies ...interface{}) {
|
||||
// The client's request body and server's response body Go types.
|
||||
// Could be any data structure.
|
||||
//
|
||||
// type (
|
||||
// request struct {
|
||||
// Firstname string `json:"firstname"`
|
||||
// Lastname string `json:"lastname"`
|
||||
// }
|
||||
// type (
|
||||
// request struct {
|
||||
// Firstname string `json:"firstname"`
|
||||
// Lastname string `json:"lastname"`
|
||||
// }
|
||||
//
|
||||
// response struct {
|
||||
// ID uint64 `json:"id"`
|
||||
// Message string `json:"message"`
|
||||
// }
|
||||
// )
|
||||
// response struct {
|
||||
// ID uint64 `json:"id"`
|
||||
// Message string `json:"message"`
|
||||
// }
|
||||
// )
|
||||
//
|
||||
// Register the route hander.
|
||||
//
|
||||
// HTTP VERB ROUTE PATH ROUTE HANDLER
|
||||
// app.HandleFunc("PUT", "/users/{id:uint64}", updateUser)
|
||||
// HTTP VERB ROUTE PATH ROUTE HANDLER
|
||||
// app.HandleFunc("PUT", "/users/{id:uint64}", updateUser)
|
||||
//
|
||||
// Code the route handler function.
|
||||
// Path parameters and request body are binded
|
||||
@@ -413,26 +414,26 @@ func (api *APIBuilder) RegisterDependency(dependencies ...interface{}) {
|
||||
// The "id" uint64 binds to "{id:uint64}" route path parameter and
|
||||
// the "input" binds to client request data such as JSON.
|
||||
//
|
||||
// func updateUser(id uint64, input request) response {
|
||||
// // [custom logic...]
|
||||
// func updateUser(id uint64, input request) response {
|
||||
// // [custom logic...]
|
||||
//
|
||||
// return response{
|
||||
// ID:id,
|
||||
// Message: "User updated successfully",
|
||||
// }
|
||||
// }
|
||||
// return response{
|
||||
// ID:id,
|
||||
// Message: "User updated successfully",
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Simulate a client request which sends data
|
||||
// to the server and prints out the response.
|
||||
//
|
||||
// curl --request PUT -d '{"firstname":"John","lastname":"Doe"}' \
|
||||
// -H "Content-Type: application/json" \
|
||||
// http://localhost:8080/users/42
|
||||
// curl --request PUT -d '{"firstname":"John","lastname":"Doe"}' \
|
||||
// -H "Content-Type: application/json" \
|
||||
// http://localhost:8080/users/42
|
||||
//
|
||||
// {
|
||||
// "id": 42,
|
||||
// "message": "User updated successfully"
|
||||
// }
|
||||
// {
|
||||
// "id": 42,
|
||||
// "message": "User updated successfully"
|
||||
// }
|
||||
//
|
||||
// See the `ConfigureContainer` for more features regrading
|
||||
// the dependency injection, mvc and function handlers.
|
||||
@@ -474,11 +475,12 @@ func (api *APIBuilder) AllowMethods(methods ...string) Party {
|
||||
// For example, if for some reason the desired result is the (done or all) handlers to be executed no matter what
|
||||
// even if no `ctx.Next()` is called in the previous handlers, including the begin(`Use`),
|
||||
// the main(`Handle`) and the done(`Done`) handlers themselves, then:
|
||||
// Party#SetExecutionRules(iris.ExecutionRules {
|
||||
// Begin: iris.ExecutionOptions{Force: true},
|
||||
// Main: iris.ExecutionOptions{Force: true},
|
||||
// Done: iris.ExecutionOptions{Force: true},
|
||||
// })
|
||||
//
|
||||
// Party#SetExecutionRules(iris.ExecutionRules {
|
||||
// Begin: iris.ExecutionOptions{Force: true},
|
||||
// Main: iris.ExecutionOptions{Force: true},
|
||||
// Done: iris.ExecutionOptions{Force: true},
|
||||
// })
|
||||
//
|
||||
// Note that if : true then the only remained way to "break" the handler chain is by `ctx.StopExecution()` now that `ctx.Next()` does not matter.
|
||||
//
|
||||
@@ -567,11 +569,14 @@ func (api *APIBuilder) handle(errorCode int, method string, relativePath string,
|
||||
// otherwise use `Party` which can handle many paths with different handlers and middlewares.
|
||||
//
|
||||
// Usage:
|
||||
// app.HandleMany("GET", "/user /user/{id:uint64} /user/me", genericUserHandler)
|
||||
//
|
||||
// app.HandleMany("GET", "/user /user/{id:uint64} /user/me", genericUserHandler)
|
||||
//
|
||||
// At the other side, with `Handle` we've had to write:
|
||||
// app.Handle("GET", "/user", userHandler)
|
||||
// app.Handle("GET", "/user/{id:uint64}", userByIDHandler)
|
||||
// app.Handle("GET", "/user/me", userMeHandler)
|
||||
//
|
||||
// app.Handle("GET", "/user", userHandler)
|
||||
// app.Handle("GET", "/user/{id:uint64}", userByIDHandler)
|
||||
// app.Handle("GET", "/user/me", userMeHandler)
|
||||
//
|
||||
// app.HandleMany("GET POST", "/path", handler)
|
||||
func (api *APIBuilder) HandleMany(methodOrMulti string, relativePathorMulti string, handlers ...context.Handler) (routes []*Route) {
|
||||
@@ -606,7 +611,7 @@ func (api *APIBuilder) HandleMany(methodOrMulti string, relativePathorMulti stri
|
||||
//
|
||||
// Alternatively, to get just the handler for that look the FileServer function instead.
|
||||
//
|
||||
// api.HandleDir("/static", iris.Dir("./assets"), iris.DirOptions{IndexName: "/index.html", Compress: true})
|
||||
// api.HandleDir("/static", iris.Dir("./assets"), iris.DirOptions{IndexName: "/index.html", Compress: true})
|
||||
//
|
||||
// Returns all the registered routes, including GET index and path patterm and HEAD.
|
||||
//
|
||||
@@ -904,12 +909,13 @@ func (api *APIBuilder) Party(relativePath string, handlers ...context.Handler) P
|
||||
// Note: `iris#Party` and `core/router#Party` describes the exactly same interface.
|
||||
//
|
||||
// Usage:
|
||||
// app.PartyFunc("/users", func(u iris.Party){
|
||||
// u.Use(authMiddleware, logMiddleware)
|
||||
// u.Get("/", getAllUsers)
|
||||
// u.Post("/", createOrUpdateUser)
|
||||
// u.Delete("/", deleteUser)
|
||||
// })
|
||||
//
|
||||
// app.PartyFunc("/users", func(u iris.Party){
|
||||
// u.Use(authMiddleware, logMiddleware)
|
||||
// u.Get("/", getAllUsers)
|
||||
// u.Post("/", createOrUpdateUser)
|
||||
// u.Delete("/", deleteUser)
|
||||
// })
|
||||
//
|
||||
// Look `Party` for more.
|
||||
func (api *APIBuilder) PartyFunc(relativePath string, partyBuilderFunc func(p Party)) Party {
|
||||
@@ -949,16 +955,21 @@ type (
|
||||
// Useful when the api's dependencies amount are too much to pass on a function.
|
||||
//
|
||||
// Usage:
|
||||
// app.PartyConfigure("/users", &api.UsersAPI{UserRepository: ..., ...})
|
||||
//
|
||||
// app.PartyConfigure("/users", &api.UsersAPI{UserRepository: ..., ...})
|
||||
//
|
||||
// Where UsersAPI looks like:
|
||||
// type UsersAPI struct { [...] }
|
||||
// func(api *UsersAPI) Configure(router iris.Party) {
|
||||
// router.Get("/{id:uuid}", api.getUser)
|
||||
// [...]
|
||||
// }
|
||||
//
|
||||
// type UsersAPI struct { [...] }
|
||||
// func(api *UsersAPI) Configure(router iris.Party) {
|
||||
// router.Get("/{id:uuid}", api.getUser)
|
||||
// [...]
|
||||
// }
|
||||
//
|
||||
// Usage with (static) dependencies:
|
||||
// app.RegisterDependency(userRepo, ...)
|
||||
// app.PartyConfigure("/users", new(api.UsersAPI))
|
||||
//
|
||||
// app.RegisterDependency(userRepo, ...)
|
||||
// app.PartyConfigure("/users", new(api.UsersAPI))
|
||||
func (api *APIBuilder) PartyConfigure(relativePath string, partyReg ...PartyConfigurator) Party {
|
||||
var child Party
|
||||
|
||||
@@ -1626,15 +1637,16 @@ func (api *APIBuilder) RegisterView(viewEngine context.ViewEngine) {
|
||||
|
||||
// FallbackView registers one or more fallback views for a template or a template layout.
|
||||
// Usage:
|
||||
// FallbackView(iris.FallbackView("fallback.html"))
|
||||
// FallbackView(iris.FallbackViewLayout("layouts/fallback.html"))
|
||||
// OR
|
||||
// FallbackView(iris.FallbackViewFunc(ctx iris.Context, err iris.ErrViewNotExist) error {
|
||||
// err.Name is the previous template name.
|
||||
// err.IsLayout reports whether the failure came from the layout template.
|
||||
// err.Data is the template data provided to the previous View call.
|
||||
// [...custom logic e.g. ctx.View("fallback", err.Data)]
|
||||
// })
|
||||
//
|
||||
// FallbackView(iris.FallbackView("fallback.html"))
|
||||
// FallbackView(iris.FallbackViewLayout("layouts/fallback.html"))
|
||||
// OR
|
||||
// FallbackView(iris.FallbackViewFunc(ctx iris.Context, err iris.ErrViewNotExist) error {
|
||||
// err.Name is the previous template name.
|
||||
// err.IsLayout reports whether the failure came from the layout template.
|
||||
// err.Data is the template data provided to the previous View call.
|
||||
// [...custom logic e.g. ctx.View("fallback", err.Data)]
|
||||
// })
|
||||
func (api *APIBuilder) FallbackView(provider context.FallbackViewProvider) {
|
||||
handler := func(ctx *context.Context) {
|
||||
ctx.FallbackView(provider)
|
||||
@@ -1653,9 +1665,10 @@ func (api *APIBuilder) FallbackView(provider context.FallbackViewProvider) {
|
||||
// app := iris.New()
|
||||
// app.RegisterView(iris.$VIEW_ENGINE("./views", ".$extension"))
|
||||
// my := app.Party("/my").Layout("layouts/mylayout.html")
|
||||
// my.Get("/", func(ctx iris.Context) {
|
||||
// ctx.View("page1.html")
|
||||
// })
|
||||
//
|
||||
// my.Get("/", func(ctx iris.Context) {
|
||||
// ctx.View("page1.html")
|
||||
// })
|
||||
//
|
||||
// Examples: https://github.com/kataras/iris/tree/master/_examples/view
|
||||
func (api *APIBuilder) Layout(tmplLayoutFile string) Party {
|
||||
|
||||
Reference in New Issue
Block a user