mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +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:
@@ -15,8 +15,8 @@ import (
|
||||
// completed by the end controller then the BeginRequest and EndRequest
|
||||
// are called between the controller's method responsible for the incoming request.
|
||||
type BaseController interface {
|
||||
BeginRequest(context.Context)
|
||||
EndRequest(context.Context)
|
||||
BeginRequest(*context.Context)
|
||||
EndRequest(*context.Context)
|
||||
}
|
||||
|
||||
type shared interface {
|
||||
@@ -400,7 +400,7 @@ func (c *ControllerActivator) handlerOf(relPath, methodName string) context.Hand
|
||||
handler := c.injector.MethodHandler(methodName, paramsCount)
|
||||
|
||||
if isBaseController(c.Type) {
|
||||
return func(ctx context.Context) {
|
||||
return func(ctx *context.Context) {
|
||||
ctrl, err := c.injector.Acquire(ctx)
|
||||
if err != nil {
|
||||
// if err != hero.ErrStopExecution {
|
||||
|
||||
@@ -28,7 +28,7 @@ func (s *testServiceImpl) Say(message string) string {
|
||||
}
|
||||
|
||||
type testControllerHandle struct {
|
||||
Ctx context.Context
|
||||
Ctx *context.Context
|
||||
Service testService
|
||||
|
||||
reqField string
|
||||
@@ -110,7 +110,7 @@ func (c *testControllerHandle) CustomWithParameters(param1, param2 string) strin
|
||||
type testSmallController struct{}
|
||||
|
||||
// test ctx + id in the same time.
|
||||
func (c *testSmallController) GetHiParamEmptyInputWithCtxBy(ctx context.Context, id string) string {
|
||||
func (c *testSmallController) GetHiParamEmptyInputWithCtxBy(ctx *context.Context, id string) string {
|
||||
return "empty in but served with ctx.Params.Get('param2')= " + ctx.Params().Get("param2") + " == id == " + id
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
type testControllerMethodResult struct {
|
||||
Ctx context.Context
|
||||
Ctx *context.Context
|
||||
}
|
||||
|
||||
func (c *testControllerMethodResult) Get() Result {
|
||||
@@ -103,7 +103,7 @@ func TestControllerMethodResult(t *testing.T) {
|
||||
}
|
||||
|
||||
type testControllerMethodResultTypes struct {
|
||||
Ctx context.Context
|
||||
Ctx *context.Context
|
||||
}
|
||||
|
||||
func (c *testControllerMethodResultTypes) GetText() string {
|
||||
@@ -132,7 +132,7 @@ type testControllerMethodCustomResult struct {
|
||||
}
|
||||
|
||||
// The only one required function to make that a custom Response dispatcher.
|
||||
func (r testControllerMethodCustomResult) Dispatch(ctx context.Context) {
|
||||
func (r testControllerMethodCustomResult) Dispatch(ctx *context.Context) {
|
||||
ctx.HTML(r.HTML)
|
||||
}
|
||||
|
||||
@@ -227,11 +227,11 @@ type testControllerViewResultRespectCtxViewData struct {
|
||||
T *testing.T
|
||||
}
|
||||
|
||||
func (t *testControllerViewResultRespectCtxViewData) BeginRequest(ctx context.Context) {
|
||||
func (t *testControllerViewResultRespectCtxViewData) BeginRequest(ctx *context.Context) {
|
||||
ctx.ViewData("name_begin", "iris_begin")
|
||||
}
|
||||
|
||||
func (t *testControllerViewResultRespectCtxViewData) EndRequest(ctx context.Context) {
|
||||
func (t *testControllerViewResultRespectCtxViewData) EndRequest(ctx *context.Context) {
|
||||
// check if data is not overridden by return View {Data: context.Map...}
|
||||
|
||||
dataWritten := ctx.GetViewData()
|
||||
|
||||
@@ -14,10 +14,10 @@ import (
|
||||
)
|
||||
|
||||
type testController struct {
|
||||
Ctx context.Context
|
||||
Ctx *context.Context
|
||||
}
|
||||
|
||||
var writeMethod = func(ctx context.Context) {
|
||||
var writeMethod = func(ctx *context.Context) {
|
||||
ctx.Writef(ctx.Method())
|
||||
}
|
||||
|
||||
@@ -58,8 +58,8 @@ func (c *testController) Trace() {
|
||||
}
|
||||
|
||||
type (
|
||||
testControllerAll struct{ Ctx context.Context }
|
||||
testControllerAny struct{ Ctx context.Context } // exactly the same as All.
|
||||
testControllerAll struct{ Ctx *context.Context }
|
||||
testControllerAny struct{ Ctx *context.Context } // exactly the same as All.
|
||||
)
|
||||
|
||||
func (c *testControllerAll) All() {
|
||||
@@ -92,7 +92,7 @@ func TestControllerMethodFuncs(t *testing.T) {
|
||||
}
|
||||
|
||||
type testControllerBeginAndEndRequestFunc struct {
|
||||
Ctx context.Context
|
||||
Ctx *context.Context
|
||||
|
||||
Username string
|
||||
}
|
||||
@@ -101,12 +101,12 @@ type testControllerBeginAndEndRequestFunc struct {
|
||||
//
|
||||
// useful when more than one methods using the
|
||||
// same request values or context's function calls.
|
||||
func (c *testControllerBeginAndEndRequestFunc) BeginRequest(ctx context.Context) {
|
||||
func (c *testControllerBeginAndEndRequestFunc) BeginRequest(ctx *context.Context) {
|
||||
c.Username = ctx.Params().Get("username")
|
||||
}
|
||||
|
||||
// called after every method (Get() or Post()).
|
||||
func (c *testControllerBeginAndEndRequestFunc) EndRequest(ctx context.Context) {
|
||||
func (c *testControllerBeginAndEndRequestFunc) EndRequest(ctx *context.Context) {
|
||||
ctx.Writef("done") // append "done" to the response
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ func TestControllerBeginAndEndRequestFuncBindMiddleware(t *testing.T) {
|
||||
"bill": true,
|
||||
"whoisyourdaddy": false,
|
||||
}
|
||||
middlewareCheck := func(ctx context.Context) {
|
||||
middlewareCheck := func(ctx *context.Context) {
|
||||
for username, allow := range usernames {
|
||||
if ctx.Params().Get("username") == username && allow {
|
||||
ctx.Next()
|
||||
@@ -197,7 +197,7 @@ type Model struct {
|
||||
}
|
||||
|
||||
type testControllerEndRequestAwareness struct {
|
||||
Ctx context.Context
|
||||
Ctx *context.Context
|
||||
}
|
||||
|
||||
func (c *testControllerEndRequestAwareness) Get() {
|
||||
@@ -209,7 +209,7 @@ func (c *testControllerEndRequestAwareness) Get() {
|
||||
})
|
||||
}
|
||||
|
||||
func writeModels(ctx context.Context, names ...string) {
|
||||
func writeModels(ctx *context.Context, names ...string) {
|
||||
if expected, got := len(names), len(ctx.GetViewData()); expected != got {
|
||||
ctx.Writef("expected view data length: %d but got: %d for names: %s", expected, got, names)
|
||||
return
|
||||
@@ -233,8 +233,8 @@ func writeModels(ctx context.Context, names ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *testControllerEndRequestAwareness) BeginRequest(ctx context.Context) {}
|
||||
func (c *testControllerEndRequestAwareness) EndRequest(ctx context.Context) {
|
||||
func (c *testControllerEndRequestAwareness) BeginRequest(ctx *context.Context) {}
|
||||
func (c *testControllerEndRequestAwareness) EndRequest(ctx *context.Context) {
|
||||
writeModels(ctx, "TestModel", "myModel")
|
||||
}
|
||||
|
||||
@@ -259,7 +259,7 @@ type testBindType struct {
|
||||
}
|
||||
|
||||
type testControllerBindStruct struct {
|
||||
Ctx context.Context
|
||||
Ctx *context.Context
|
||||
|
||||
// should start with upper letter of course
|
||||
TitlePointer *testBindType // should have the value of the "myTitlePtr" on test
|
||||
@@ -335,7 +335,7 @@ func (c *testCtrl0) Get() string {
|
||||
return c.Ctx.Params().Get("username")
|
||||
}
|
||||
|
||||
func (c *testCtrl0) EndRequest(ctx context.Context) {
|
||||
func (c *testCtrl0) EndRequest(ctx *context.Context) {
|
||||
if c.TitlePointer == nil {
|
||||
ctx.Writef("\nTitlePointer is nil!\n")
|
||||
} else {
|
||||
@@ -347,7 +347,7 @@ func (c *testCtrl0) EndRequest(ctx context.Context) {
|
||||
}
|
||||
|
||||
type testCtrl00 struct {
|
||||
Ctx context.Context
|
||||
Ctx *context.Context
|
||||
|
||||
testCtrl000
|
||||
}
|
||||
@@ -361,8 +361,8 @@ type testCtrl000 struct {
|
||||
type testCtrl0000 struct {
|
||||
}
|
||||
|
||||
func (c *testCtrl0000) BeginRequest(ctx context.Context) {}
|
||||
func (c *testCtrl0000) EndRequest(ctx context.Context) {
|
||||
func (c *testCtrl0000) BeginRequest(ctx *context.Context) {}
|
||||
func (c *testCtrl0000) EndRequest(ctx *context.Context) {
|
||||
ctx.Writef("finish")
|
||||
}
|
||||
|
||||
@@ -385,8 +385,8 @@ func TestControllerInsideControllerRecursively(t *testing.T) {
|
||||
|
||||
type testControllerRelPathFromFunc struct{}
|
||||
|
||||
func (c *testControllerRelPathFromFunc) BeginRequest(ctx context.Context) {}
|
||||
func (c *testControllerRelPathFromFunc) EndRequest(ctx context.Context) {
|
||||
func (c *testControllerRelPathFromFunc) BeginRequest(ctx *context.Context) {}
|
||||
func (c *testControllerRelPathFromFunc) EndRequest(ctx *context.Context) {
|
||||
ctx.Writef("%s:%s", ctx.Method(), ctx.Path())
|
||||
}
|
||||
|
||||
@@ -565,7 +565,7 @@ func (c *testControllerRequestScopedDependencies) GetCustomContext() string {
|
||||
return c.MyContext.OtherField
|
||||
}
|
||||
|
||||
func newRequestDep1(ctx context.Context) *testCustomStruct {
|
||||
func newRequestDep1(ctx *context.Context) *testCustomStruct {
|
||||
return &testCustomStruct{
|
||||
Name: ctx.URLParam("name"),
|
||||
Age: ctx.URLParamIntDefault("age", 0),
|
||||
@@ -573,11 +573,11 @@ func newRequestDep1(ctx context.Context) *testCustomStruct {
|
||||
}
|
||||
|
||||
type testMyContext struct {
|
||||
Context context.Context
|
||||
Context *context.Context
|
||||
OtherField string
|
||||
}
|
||||
|
||||
func newRequestDep2(ctx context.Context) *testMyContext {
|
||||
func newRequestDep2(ctx *context.Context) *testMyContext {
|
||||
return &testMyContext{
|
||||
Context: ctx,
|
||||
OtherField: "test",
|
||||
|
||||
@@ -40,7 +40,7 @@ var _ Option = GRPC{}
|
||||
func (g GRPC) Apply(c *ControllerActivator) {
|
||||
defer c.Activated()
|
||||
|
||||
pre := func(ctx context.Context) {
|
||||
pre := func(ctx *context.Context) {
|
||||
if ctx.IsGRPC() { // gRPC, consumes and produces protobuf.
|
||||
g.Server.ServeHTTP(ctx.ResponseWriter(), ctx.Request())
|
||||
ctx.StopExecution()
|
||||
|
||||
@@ -288,9 +288,9 @@ func (app *Application) handle(controller interface{}, options ...Option) *Contr
|
||||
// HandleError registers a `hero.ErrorHandlerFunc` which will be fired when
|
||||
// application's controllers' functions returns an non-nil error.
|
||||
// Each controller can override it by implementing the `hero.ErrorHandler`.
|
||||
func (app *Application) HandleError(handler func(ctx context.Context, err error)) *Application {
|
||||
func (app *Application) HandleError(handler func(ctx *context.Context, err error)) *Application {
|
||||
errorHandler := hero.ErrorHandlerFunc(handler)
|
||||
app.container.GetErrorHandler = func(context.Context) hero.ErrorHandler {
|
||||
app.container.GetErrorHandler = func(*context.Context) hero.ErrorHandler {
|
||||
return errorHandler
|
||||
}
|
||||
return app
|
||||
|
||||
@@ -26,7 +26,7 @@ func Version(version string) OptionFunc {
|
||||
return func(c *ControllerActivator) {
|
||||
c.Router().SetRegisterRule(router.RouteOverlap) // required for this feature.
|
||||
|
||||
c.Use(func(ctx context.Context) {
|
||||
c.Use(func(ctx *context.Context) {
|
||||
if !versioning.Match(ctx, version) {
|
||||
ctx.StopExecution()
|
||||
return
|
||||
@@ -42,7 +42,7 @@ func Version(version string) OptionFunc {
|
||||
// a newer version of that specific resource is available instead.
|
||||
func Deprecated(options DeprecationOptions) OptionFunc {
|
||||
return func(c *ControllerActivator) {
|
||||
c.Use(func(ctx context.Context) {
|
||||
c.Use(func(ctx *context.Context) {
|
||||
versioning.WriteDeprecated(ctx, options)
|
||||
ctx.Next()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user