Пример #1
0
func baseHandler(handler func(w rest.ResponseWriter, r *rest.Request)) rest.HandlerFunc {
	return rest.HandlerFunc(func(w rest.ResponseWriter, r *rest.Request) {
		// common
		log.Println(r.URL, r.Method)
		r.Env["context"] = ctx
		handler(w, r)
	})
}
Пример #2
0
import (
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/ant0ine/go-json-rest/rest"
	"github.com/stretchr/testify/assert"
)

const internalHandlerBody = "internal"

var encapsulated = rest.HandlerFunc(func(w rest.ResponseWriter, r *rest.Request) {
	// w.WriteJson would just be a hassle here, so we need to explicitly set the content type and cast the jrest
	// type to net/http types
	w.Header().Set("Content-Type", "text/plain")
	_, _ = w.(http.ResponseWriter).Write([]byte(internalHandlerBody))
})

func jrestServer(mw rest.Middleware, endpoint string) http.Handler {
	restAPI := rest.NewApi()
	restAPI.Use(mw)

	router, err := rest.MakeRouter(rest.Get(endpoint, encapsulated))

	if err != nil {
		return nil
	}

	restAPI.SetApp(router)
	return restAPI.MakeHandler()
Пример #3
0
func baseHandlerFunc(handler func(w rest.ResponseWriter, r *rest.Request)) rest.HandlerFunc {
	return baseHandler(rest.HandlerFunc(handler))
}