Skip to content

Commit

Permalink
Moved Context to core.Context
Browse files Browse the repository at this point in the history
  • Loading branch information
anuptalwalkar committed Nov 21, 2016
1 parent bdd9383 commit 69006fd
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 35 deletions.
16 changes: 10 additions & 6 deletions service/context.go → core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,34 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package service
package core

import gcontext "context"
import (
gcontext "context"

"go.uber.org/fx/service"
)

// Context embedds Host and go context for use
type Context interface {
gcontext.Context
Host
service.Host

Resource(key string) interface{}
SetResource(key string, value interface{})
}

type context struct {
gcontext.Context
Host
service.Host

resources map[string]interface{}
}

var _ Context = &context{}

// NewContext always returns service.Context for use in the service
func NewContext(ctx gcontext.Context, host Host) Context {
// NewContext always returns core.Context for use in the service
func NewContext(ctx gcontext.Context, host service.Host) Context {
return &context{
Context: ctx,
Host: host,
Expand Down
10 changes: 6 additions & 4 deletions service/context_test.go → core/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package service
package core

import (
"testing"

"go.uber.org/fx/service"

"github.com/stretchr/testify/assert"

gcontext "context"
Expand All @@ -33,7 +35,7 @@ type _testStruct struct {
}

func TestContext_Simple(t *testing.T) {
ctx := NewContext(gcontext.Background(), NullHost())
ctx := NewContext(gcontext.Background(), service.NullHost())
testStruct := _testStruct{
data: "hello",
}
Expand All @@ -44,13 +46,13 @@ func TestContext_Simple(t *testing.T) {
}

func TestContext_NilResource(t *testing.T) {
ctx := NewContext(gcontext.Background(), NullHost())
ctx := NewContext(gcontext.Background(), service.NullHost())

assert.Nil(t, ctx.Resource("resource"))
}

func TestContext_HostAccess(t *testing.T) {
ctx := NewContext(gcontext.Background(), NullHost())
ctx := NewContext(gcontext.Background(), service.NullHost())
assert.NotNil(t, ctx.Config())
assert.Equal(t, "dummy", ctx.Name())
}
3 changes: 2 additions & 1 deletion examples/simple/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import (
"io"
"net/http"

"go.uber.org/fx/core"
"go.uber.org/fx/modules/uhttp"
"go.uber.org/fx/service"
)

type exampleHandler struct{}

func (exampleHandler) ServeHTTP(ctx service.Context, w http.ResponseWriter, r *http.Request) {
func (exampleHandler) ServeHTTP(ctx core.Context, w http.ResponseWriter, r *http.Request) {
io.WriteString(w, fmt.Sprintf("Headers: %+v", r.Header))
}

Expand Down
16 changes: 8 additions & 8 deletions modules/uhttp/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"net/http"

"go.uber.org/fx/service"
"go.uber.org/fx/core"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
Expand All @@ -33,20 +33,20 @@ import (
// Filter applies filters on requests, request contexts or responses such as
// adding tracing to the context
type Filter interface {
Apply(ctx service.Context, w http.ResponseWriter, r *http.Request, next Handler)
Apply(ctx core.Context, w http.ResponseWriter, r *http.Request, next Handler)
}

// FilterFunc is an adaptor to call normal functions to apply filters
type FilterFunc func(ctx service.Context, w http.ResponseWriter, r *http.Request, next Handler)
type FilterFunc func(ctx core.Context, w http.ResponseWriter, r *http.Request, next Handler)

// Apply implements Apply from the Filter interface and simply delegates to the function
func (f FilterFunc) Apply(ctx service.Context, w http.ResponseWriter, r *http.Request, next Handler) {
func (f FilterFunc) Apply(ctx core.Context, w http.ResponseWriter, r *http.Request, next Handler) {
f(ctx, w, r, next)
}

type tracerFilter struct{}

func (t tracerFilter) Apply(ctx service.Context, w http.ResponseWriter, r *http.Request, next Handler) {
func (t tracerFilter) Apply(ctx core.Context, w http.ResponseWriter, r *http.Request, next Handler) {
operationName := r.Method
carrier := opentracing.HTTPHeadersCarrier(r.Header)
spanCtx, err := ctx.Tracer().Extract(opentracing.HTTPHeaders, carrier)
Expand All @@ -57,12 +57,12 @@ func (t tracerFilter) Apply(ctx service.Context, w http.ResponseWriter, r *http.
ext.HTTPUrl.Set(span, r.URL.String())
defer span.Finish()

gctx := service.NewContext(opentracing.ContextWithSpan(ctx, span), ctx)
gctx := core.NewContext(opentracing.ContextWithSpan(ctx, span), ctx)
next.ServeHTTP(gctx, w, r)
}

// panicFilter handles any panics and return an error
func panicFilter(ctx service.Context, w http.ResponseWriter, r *http.Request, next Handler) {
func panicFilter(ctx core.Context, w http.ResponseWriter, r *http.Request, next Handler) {
defer func() {
if err := recover(); err != nil {
// TODO(ai) log and add stats to this
Expand All @@ -87,7 +87,7 @@ type executionChain struct {
finalHandler Handler
}

func (ec executionChain) ServeHTTP(ctx service.Context, w http.ResponseWriter, req *http.Request) {
func (ec executionChain) ServeHTTP(ctx core.Context, w http.ResponseWriter, req *http.Request) {
if ec.currentFilter < len(ec.filters) {
filter := ec.filters[ec.currentFilter]
ec.currentFilter++
Expand Down
5 changes: 3 additions & 2 deletions modules/uhttp/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"testing"

"go.uber.org/fx/core"
"go.uber.org/fx/service"

"github.com/stretchr/testify/assert"
Expand All @@ -51,13 +52,13 @@ func TestExecutionChainFilters(t *testing.T) {
func testServeHTTP(chain executionChain) *httptest.ResponseRecorder {
request := httptest.NewRequest("", "http://filters", nil)
response := httptest.NewRecorder()
ctx := service.NewContext(context.Background(), service.NullHost())
ctx := core.NewContext(context.Background(), service.NullHost())
chain.ServeHTTP(ctx, response, request)
return response
}

func getNoopHandler() Handler {
return HandlerFunc(func(ctx service.Context, w http.ResponseWriter, r *http.Request) {
return HandlerFunc(func(ctx core.Context, w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "filters ok")
})
}
9 changes: 5 additions & 4 deletions modules/uhttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ import (
"context"
"net/http"

"go.uber.org/fx/core"
"go.uber.org/fx/service"
)

// Handler is a context-aware extension of http.Handler.
type Handler interface {
ServeHTTP(ctx service.Context, w http.ResponseWriter, r *http.Request)
ServeHTTP(ctx core.Context, w http.ResponseWriter, r *http.Request)
}

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers.
type HandlerFunc func(ctx service.Context, w http.ResponseWriter, r *http.Request)
type HandlerFunc func(ctx core.Context, w http.ResponseWriter, r *http.Request)

// ServeHTTP calls the caller HandlerFunc.
func (f HandlerFunc) ServeHTTP(ctx service.Context, w http.ResponseWriter, r *http.Request) {
func (f HandlerFunc) ServeHTTP(ctx core.Context, w http.ResponseWriter, r *http.Request) {
f(ctx, w, r)
}

Expand All @@ -56,6 +57,6 @@ type handlerWrapper struct {

// ServeHTTP calls Handler.ServeHTTP(ctx, w, r) and injects a new service context for use.
func (h *handlerWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := service.NewContext(context.Background(), h.host)
ctx := core.NewContext(context.Background(), h.host)
h.handler.ServeHTTP(ctx, w, r)
}
4 changes: 2 additions & 2 deletions modules/uhttp/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"fmt"
"net/http"

"go.uber.org/fx/service"
"go.uber.org/fx/core"
)

type healthHandler struct{}

func (h healthHandler) ServeHTTP(ctx service.Context, w http.ResponseWriter, r *http.Request) {
func (h healthHandler) ServeHTTP(ctx core.Context, w http.ResponseWriter, r *http.Request) {
// TODO(ai) import more sophisticated health mechanism from internal libraries
fmt.Fprintf(w, "OK\n")
}
9 changes: 5 additions & 4 deletions modules/uhttp/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"testing"
"time"

"go.uber.org/fx/core"
. "go.uber.org/fx/core/testutils"
"go.uber.org/fx/modules"
"go.uber.org/fx/service"
Expand Down Expand Up @@ -215,7 +216,7 @@ func registerNothing(_ service.Host) []RouteHandler {
return nil
}

func makeSingleHandler(path string, fn func(service.Context, http.ResponseWriter, *http.Request)) []RouteHandler {
func makeSingleHandler(path string, fn func(core.Context, http.ResponseWriter, *http.Request)) []RouteHandler {
return []RouteHandler{
{
Path: path,
Expand All @@ -225,7 +226,7 @@ func makeSingleHandler(path string, fn func(service.Context, http.ResponseWriter
}

func registerTracerCheckHandler(host service.Host) []RouteHandler {
return makeSingleHandler("/", func(ctx service.Context, _ http.ResponseWriter, r *http.Request) {
return makeSingleHandler("/", func(ctx core.Context, _ http.ResponseWriter, r *http.Request) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
panic(fmt.Sprintf("Intentional panic, invalid span: %v", span))
Expand All @@ -239,13 +240,13 @@ func registerTracerCheckHandler(host service.Host) []RouteHandler {
}

func registerCustomHealth(_ service.Host) []RouteHandler {
return makeSingleHandler("/health", func(ctx service.Context, w http.ResponseWriter, r *http.Request) {
return makeSingleHandler("/health", func(ctx core.Context, w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "not ok")
})
}

func registerPanic(_ service.Host) []RouteHandler {
return makeSingleHandler("/", func(ctx service.Context, _ http.ResponseWriter, r *http.Request) {
return makeSingleHandler("/", func(ctx core.Context, _ http.ResponseWriter, r *http.Request) {
panic("Intentional panic for:" + r.URL.Path)
})
}
5 changes: 3 additions & 2 deletions modules/uhttp/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/fx/core"
"go.uber.org/fx/service"
)

Expand All @@ -45,11 +46,11 @@ func withRouter(t *testing.T, f func(r *Router, l net.Listener)) {
l := serve(t, r)
defer l.Close()
r.Handle("/foo/baz/quokka",
HandlerFunc(func(ctx service.Context, w http.ResponseWriter, r *http.Request) {
HandlerFunc(func(ctx core.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
}))
r.Handle("/foo/bar/quokka",
HandlerFunc(func(ctx service.Context, w http.ResponseWriter, r *http.Request) {
HandlerFunc(func(ctx core.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("world"))
}))
f(r, l)
Expand Down
4 changes: 2 additions & 2 deletions modules/uhttp/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"net/http"
"testing"

"go.uber.org/fx/service"
"go.uber.org/fx/core"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
Expand All @@ -39,7 +39,7 @@ func TestFromGorilla_OK(t *testing.T) {
}

func TestNewRouteHandler(t *testing.T) {
rh := NewRouteHandler("/", HandlerFunc(func(ctx service.Context, w http.ResponseWriter, r *http.Request) {
rh := NewRouteHandler("/", HandlerFunc(func(ctx core.Context, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi\n")
}))

Expand Down

0 comments on commit 69006fd

Please sign in to comment.