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 }
// 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...) }
"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()
"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