mirror of
https://github.com/kataras/iris.git
synced 2025-12-28 07:17:06 +00:00
(#1554) Add support for all common compressions (write and read)
- Remove the context.Context interface and export the *context, the iris.Context now points to the pointer\nSupport compression and rate limiting in the FileServer\nBit of code organisation Former-commit-id: ad1c61bf968059510c6be9e7f2cceec7da70ba17
This commit is contained in:
@@ -11,9 +11,9 @@ import (
|
||||
)
|
||||
|
||||
// ResultHandler describes the function type which should serve the "v" struct value.
|
||||
type ResultHandler func(ctx context.Context, v interface{}) error
|
||||
type ResultHandler func(ctx *context.Context, v interface{}) error
|
||||
|
||||
func defaultResultHandler(ctx context.Context, v interface{}) error {
|
||||
func defaultResultHandler(ctx *context.Context, v interface{}) error {
|
||||
if p, ok := v.(PreflightResult); ok {
|
||||
if err := p.Preflight(ctx); err != nil {
|
||||
return err
|
||||
@@ -57,7 +57,7 @@ func defaultResultHandler(ctx context.Context, v interface{}) error {
|
||||
// Example at: https://github.com/kataras/iris/tree/master/_examples/dependency-injection/overview.
|
||||
type Result interface {
|
||||
// Dispatch should send a response to the client.
|
||||
Dispatch(context.Context)
|
||||
Dispatch(*context.Context)
|
||||
}
|
||||
|
||||
// PreflightResult is an interface which implementers
|
||||
@@ -73,7 +73,7 @@ type Result interface {
|
||||
// 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
|
||||
Preflight(*context.Context) error
|
||||
}
|
||||
|
||||
var defaultFailureResponse = Response{Code: DefaultErrStatusCode}
|
||||
@@ -115,7 +115,7 @@ type compatibleErr interface {
|
||||
}
|
||||
|
||||
// dispatchErr writes the error to the response.
|
||||
func dispatchErr(ctx context.Context, status int, err error) bool {
|
||||
func dispatchErr(ctx *context.Context, status int, err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
@@ -154,7 +154,7 @@ func dispatchErr(ctx context.Context, status int, err error) bool {
|
||||
// Result or (Result, error) and so on...
|
||||
//
|
||||
// where Get is an HTTP METHOD.
|
||||
func dispatchFuncResult(ctx context.Context, values []reflect.Value, handler ResultHandler) error {
|
||||
func dispatchFuncResult(ctx *context.Context, values []reflect.Value, handler ResultHandler) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -324,7 +324,7 @@ func dispatchFuncResult(ctx context.Context, values []reflect.Value, handler Res
|
||||
|
||||
// dispatchCommon is being used internally to send
|
||||
// commonly used data to the response writer with a smart way.
|
||||
func dispatchCommon(ctx context.Context,
|
||||
func dispatchCommon(ctx *context.Context,
|
||||
statusCode int, contentType string, content []byte, v interface{}, handler ResultHandler, found bool) error {
|
||||
// if we have a false boolean as a return value
|
||||
// then skip everything and fire a not found,
|
||||
@@ -416,7 +416,7 @@ type Response struct {
|
||||
var _ Result = Response{}
|
||||
|
||||
// Dispatch writes the response result to the context's response writer.
|
||||
func (r Response) Dispatch(ctx context.Context) {
|
||||
func (r Response) Dispatch(ctx *context.Context) {
|
||||
if dispatchErr(ctx, r.Code, r.Err) {
|
||||
return
|
||||
}
|
||||
@@ -492,7 +492,7 @@ func ensureExt(s string) string {
|
||||
|
||||
// Dispatch writes the template filename, template layout and (any) data to the client.
|
||||
// Completes the `Result` interface.
|
||||
func (r View) Dispatch(ctx context.Context) { // r as Response view.
|
||||
func (r View) Dispatch(ctx *context.Context) { // r as Response view.
|
||||
if dispatchErr(ctx, r.Code, r.Err) {
|
||||
return
|
||||
}
|
||||
@@ -520,9 +520,7 @@ func (r View) Dispatch(ctx context.Context) { // r as Response view.
|
||||
// else check if r.Data is map or struct, if struct convert it to map,
|
||||
// do a range loop and modify the data one by one.
|
||||
// context.Map is actually a map[string]interface{} but we have to make that check:
|
||||
if m, ok := r.Data.(map[string]interface{}); ok {
|
||||
setViewData(ctx, m)
|
||||
} else if m, ok := r.Data.(context.Map); ok {
|
||||
if m, ok := r.Data.(context.Map); ok {
|
||||
setViewData(ctx, m)
|
||||
} else if reflect.Indirect(reflect.ValueOf(r.Data)).Kind() == reflect.Struct {
|
||||
setViewData(ctx, structs.Map(r))
|
||||
@@ -534,7 +532,7 @@ func (r View) Dispatch(ctx context.Context) { // r as Response view.
|
||||
}
|
||||
}
|
||||
|
||||
func setViewData(ctx context.Context, data map[string]interface{}) {
|
||||
func setViewData(ctx *context.Context, data map[string]interface{}) {
|
||||
for k, v := range data {
|
||||
ctx.ViewData(k, v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user