Skip to content

Commit

Permalink
Context is an interface now
Browse files Browse the repository at this point in the history
  • Loading branch information
akyoto committed May 31, 2019
1 parent dd21a1d commit 5e251c3
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 320 deletions.
27 changes: 19 additions & 8 deletions Application.go
Expand Up @@ -2,7 +2,7 @@ package aero

import (
"compress/gzip"
"context"
stdContext "context"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -37,12 +37,13 @@ type Application struct {
start time.Time
rewrite func(*RewriteContext)
middleware []Middleware
pushConditions []func(*Context) bool
pushConditions []func(Context) bool
onStart []func()
onShutdown []func()
onPush []func(*Context)
onPush []func(Context)
onError []func(error)
stop chan os.Signal
pushOptions http.PushOptions
contextPool sync.Pool
gzipWriterPool sync.Pool
serversMutex sync.Mutex
Expand Down Expand Up @@ -84,12 +85,22 @@ func New() *Application {
"form-action": "'self'",
})

// Context pool
app.contextPool.New = func() interface{} {
return &Context{
return &context{
app: app,
}
}

// Push options describes the headers that are sent
// to our server to retrieve the push response.
app.pushOptions = http.PushOptions{
Method: "GET",
Header: http.Header{
acceptEncodingHeader: []string{"gzip"},
},
}

// Configuration
app.Config.Reset()
app.Load()
Expand Down Expand Up @@ -201,7 +212,7 @@ func (app *Application) OnEnd(callback func()) {
}

// OnPush registers a callback to be executed when an HTTP/2 push happens.
func (app *Application) OnPush(callback func(*Context)) {
func (app *Application) OnPush(callback func(Context)) {
app.onPush = append(app.onPush, callback)
}

Expand All @@ -211,7 +222,7 @@ func (app *Application) OnError(callback func(error)) {
}

// AddPushCondition registers a callback to be executed when an HTTP/2 push happens.
func (app *Application) AddPushCondition(test func(*Context) bool) {
func (app *Application) AddPushCondition(test func(Context) bool) {
app.pushConditions = append(app.pushConditions, test)
}

Expand All @@ -228,7 +239,7 @@ func (app *Application) StartTime() time.Time {
// ServeHTTP responds to the given request.
func (app *Application) ServeHTTP(response http.ResponseWriter, request *http.Request) {
// Create context.
ctx := app.contextPool.Get().(*Context)
ctx := app.contextPool.Get().(*context)
ctx.status = http.StatusOK
ctx.request = request
ctx.response = response
Expand Down Expand Up @@ -369,7 +380,7 @@ func shutdown(server *http.Server) {
}

// Add a timeout to the server shutdown
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), 250*time.Millisecond)
defer cancel()

// Shut down server
Expand Down
16 changes: 8 additions & 8 deletions Application_test.go
Expand Up @@ -20,7 +20,7 @@ func TestApplicationGet(t *testing.T) {
app := aero.New()

// Register route
app.Get("/", func(ctx *aero.Context) error {
app.Get("/", func(ctx aero.Context) error {
return ctx.Text(helloWorld)
})

Expand All @@ -37,7 +37,7 @@ func TestApplicationPost(t *testing.T) {
app := aero.New()

// Register route
app.Post("/", func(ctx *aero.Context) error {
app.Post("/", func(ctx aero.Context) error {
return ctx.Text(helloWorld)
})

Expand All @@ -56,7 +56,7 @@ func TestApplicationDelete(t *testing.T) {
app := aero.New()

// Register route
app.Delete("/", func(ctx *aero.Context) error {
app.Delete("/", func(ctx aero.Context) error {
return ctx.Text(helloWorld)
})

Expand All @@ -75,7 +75,7 @@ func TestApplicationRewrite(t *testing.T) {
app := aero.New()

// Register route
app.Get("/hello", func(ctx *aero.Context) error {
app.Get("/hello", func(ctx aero.Context) error {
return ctx.Text(helloWorld)
})

Expand Down Expand Up @@ -115,7 +115,7 @@ func TestApplicationRun(t *testing.T) {
c := qt.New(t)

// When frontpage is requested, kill the server
app.Get("/", func(ctx *aero.Context) error {
app.Get("/", func(ctx aero.Context) error {
return ctx.HTML(helloWorld)
})

Expand Down Expand Up @@ -144,7 +144,7 @@ func TestApplicationRunHTTPS(t *testing.T) {
c := qt.New(t)

// Register route
app.Get("/", func(ctx *aero.Context) error {
app.Get("/", func(ctx aero.Context) error {
return ctx.HTML(helloWorld)
})

Expand All @@ -169,11 +169,11 @@ func TestApplicationRunHTTPS(t *testing.T) {
func TestApplicationRouteTests(t *testing.T) {
app := aero.New()

app.Get("/user/:id", func(ctx *aero.Context) error {
app.Get("/user/:id", func(ctx aero.Context) error {
return ctx.Text(ctx.Get("id"))
})

app.Get("/untested/:untested", func(ctx *aero.Context) error {
app.Get("/untested/:untested", func(ctx aero.Context) error {
return ctx.Text(ctx.Get("untested"))
})

Expand Down
8 changes: 4 additions & 4 deletions Body_test.go
Expand Up @@ -17,7 +17,7 @@ func TestBody(t *testing.T) {
c := qt.New(t)

// Register route
app.Get("/", func(ctx *aero.Context) error {
app.Get("/", func(ctx aero.Context) error {
body := ctx.Request().Body()
c.Assert(ctx.Request().Body().Reader(), qt.Not(qt.IsNil))
bodyText, _ := body.String()
Expand All @@ -39,7 +39,7 @@ func TestBodyJSON(t *testing.T) {
app := aero.New()

// Register route
app.Get("/", func(ctx *aero.Context) error {
app.Get("/", func(ctx aero.Context) error {
body := ctx.Request().Body()
obj, _ := body.JSONObject()
return ctx.Text(fmt.Sprint(obj["key"]))
Expand All @@ -61,7 +61,7 @@ func TestBodyErrors(t *testing.T) {
app := aero.New()
c := qt.New(t)

app.Get("/", func(ctx *aero.Context) error {
app.Get("/", func(ctx aero.Context) error {
body := ctx.Request().Body()
bodyJSON, err := body.JSON()

Expand All @@ -71,7 +71,7 @@ func TestBodyErrors(t *testing.T) {
return ctx.Text(helloWorld)
})

app.Get("/json-object", func(ctx *aero.Context) error {
app.Get("/json-object", func(ctx aero.Context) error {
body := ctx.Request().Body()
bodyJSONObject, err := body.JSONObject()

Expand Down

0 comments on commit 5e251c3

Please sign in to comment.