示例#1
0
func testServer(t *testing.T) (cancel, step func(), resp <-chan *http.Response) {
	var (
		ctx, cancelfn = context.WithCancel(context.Background())
		stepch        = make(chan bool)
		endpoint      = func(context.Context, interface{}) (interface{}, error) { <-stepch; return struct{}{}, nil }
		response      = make(chan *http.Response)
		handler       = httptransport.NewServer(
			ctx,
			endpoint,
			func(*http.Request) (interface{}, error) { return struct{}{}, nil },
			func(http.ResponseWriter, interface{}) error { return nil },
			httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context { return ctx }),
			httptransport.ServerAfter(func(ctx context.Context, w http.ResponseWriter) { return }),
		)
	)
	go func() {
		server := httptest.NewServer(handler)
		defer server.Close()
		resp, err := http.Get(server.URL)
		if err != nil {
			t.Error(err)
			return
		}
		response <- resp
	}()
	return cancelfn, func() { stepch <- true }, response
}
示例#2
0
文件: service.go 项目: gourd/kit
// Handler returns go-kit http transport server
// of the given definition
func (s Service) Handler() http.Handler {
	ep := s.Middlewares.Chain()(s.Endpoint)
	options := append([]httptransport.ServerOption{
		httptransport.ServerBefore(s.Before...),
		httptransport.ServerAfter(s.After...),
		httptransport.ServerErrorEncoder(s.ErrorEncoder),
	}, s.Options...)
	return httptransport.NewServer(
		s.Context,
		ep,
		s.DecodeFunc,
		s.EncodeFunc,
		options...)
}
示例#3
0
	"net/http"

	"golang.org/x/net/context"

	"github.com/tgulacsi/agostle/converter"

	kithttp "github.com/go-kit/kit/transport/http"
)

var outlookToEmailServer = kithttp.NewServer(
	context.Background(),
	outlookToEmailEP,
	outlookToEmailDecode,
	outlookToEmailEncode,
	kithttp.ServerBefore(defaultBeforeFuncs...),
	kithttp.ServerAfter(kithttp.SetContentType("mail/rfc822")),
)

func outlookToEmailDecode(r *http.Request) (interface{}, error) {
	return getOneRequestFile(r)
}

func outlookToEmailEP(ctx context.Context, request interface{}) (response interface{}, err error) {
	f := request.(reqFile)
	defer f.Close()
	return converter.NewOLEStorageReader(f)
}

func outlookToEmailEncode(w http.ResponseWriter, response interface{}) error {
	res := response.(io.ReadCloser)
	defer res.Close()
示例#4
0
	"golang.org/x/net/context"

	"github.com/tgulacsi/agostle/converter"

	kithttp "github.com/go-kit/kit/transport/http"

	"gopkg.in/errgo.v1"
)

var pdfMergeServer = kithttp.NewServer(
	context.Background(),
	pdfMergeEP,
	pdfMergeDecode,
	pdfMergeEncode,
	kithttp.ServerBefore(defaultBeforeFuncs...),
	kithttp.ServerAfter(kithttp.SetContentType("application/pdf")),
)

func pdfMergeDecode(r *http.Request) (interface{}, error) {
	inputs, err := getRequestFiles(r)
	if err != nil {
		return nil, err
	}
	req := pdfMergeRequest{Inputs: inputs}
	switch r.URL.Query().Get("sort") {
	case "0":
		req.Sort = NoSort
	case "1":
		req.Sort = DoSort
	default:
		req.Sort = DefaultSort