mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
1bb76853a9
Former-commit-id: 169671a8b5b706dc8f136e68c1a060f27a2c421b
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/kataras/iris/v12"
|
|
)
|
|
|
|
type (
|
|
testInput struct {
|
|
Email string `json:"email"`
|
|
}
|
|
|
|
testOutput struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
)
|
|
|
|
func handler(id int, in testInput) testOutput {
|
|
return testOutput{
|
|
ID: id,
|
|
Name: in.Email,
|
|
}
|
|
}
|
|
|
|
var errCustom = errors.New("my_error")
|
|
|
|
func middleware(in testInput) (int, error) {
|
|
if in.Email == "invalid" {
|
|
// stop the execution and don't continue to "handler"
|
|
// without firing an error.
|
|
return iris.StatusAccepted, iris.ErrStopExecution
|
|
} else if in.Email == "error" {
|
|
// stop the execution and fire a custom error.
|
|
return iris.StatusConflict, errCustom
|
|
}
|
|
|
|
return iris.StatusOK, nil
|
|
}
|
|
|
|
func newApp() *iris.Application {
|
|
app := iris.New()
|
|
|
|
// handle the route, respond with
|
|
// a JSON and 200 status code
|
|
// or 202 status code and empty body
|
|
// or a 409 status code and "my_error" body.
|
|
app.ConfigureContainer(func(api *iris.APIContainer) {
|
|
api.Use(middleware)
|
|
api.Post("/{id:int}", handler)
|
|
})
|
|
|
|
app.Configure(
|
|
iris.WithOptimizations, /* optional */
|
|
iris.WithoutBodyConsumptionOnUnmarshal /* required when more than one handler is consuming request payload(testInput) */)
|
|
|
|
return app
|
|
}
|
|
|
|
func main() {
|
|
app := newApp()
|
|
app.Listen(":8080")
|
|
}
|