1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 05:25:58 +00:00

🚌 next version preparation: new PreflightResult interface for hero handlers

Former-commit-id: ea2d7ab93889beaddfe269bd213d259d26df979f
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-03-02 10:07:44 +02:00
parent afd0f5caef
commit 5da8ff92f3
6 changed files with 90 additions and 7 deletions

View File

@@ -15,8 +15,24 @@ import (
//
// Example at: https://github.com/kataras/iris/tree/master/_examples/hero/overview.
type Result interface {
// Dispatch should sends the response to the context's response writer.
Dispatch(ctx context.Context)
// Dispatch should send a response to the client.
Dispatch(context.Context)
}
// PreflightResult is an interface which implementers
// should be responsible to perform preflight checks of a <T> resource (or Result) before sent to the client.
//
// If a non-nil error returned from the `Preflight` method then the JSON result
// will be not sent to the client and an ErrorHandler will be responsible to render the error.
//
// Usage: a custom struct value will be a JSON body response (by-default) but it contains
// "Code int" and `ID string` fields, the "Code" should be the status code of the response
// and the "ID" should be sent as a Header of "X-Request-ID: $ID".
//
// The caller can manage it at the handler itself. However,
// to reduce thoese type of duplications it's preferable to use such a standard interface instead.
type PreflightResult interface {
Preflight(context.Context) error
}
var defaultFailureResponse = Response{Code: DefaultErrStatusCode}
@@ -294,6 +310,12 @@ func dispatchCommon(ctx context.Context,
}
if v != nil {
if p, ok := v.(PreflightResult); ok {
if err := p.Preflight(ctx); err != nil {
return err
}
}
if d, ok := v.(Result); ok {
// write the content type now (internal check for empty value)
ctx.ContentType(contentType)