Exemple #1
0
func queryID(c context.Context) string {
	q := mohttp.GetRequest(c).URL.Query()
	vals := make([]string, len(q))
	i := 0

	for k, v := range q {
		sort.Strings(v)
		vals[i] = k + "=" + strings.Join(v, "&")
		i++
	}

	sort.Strings(vals)

	return strings.Join(vals, ",")
}
Exemple #2
0
func MatchMediaTypes(c context.Context, mediaTypes ...string) bool {
	acc := strings.Split(mohttp.GetRequest(c).Header.Get("Accept"), ",")

	if len(acc) == 0 {
		return true
	}

	for _, mt := range mediaTypes {
		if mt == "*/*" {
			return true
		}

		for _, h := range acc {
			if mt == strings.TrimSpace(h) {
				return true
			}
		}
	}

	return false
}
Exemple #3
0
func EtagHandlerFunc(fn func(context.Context) (interface{}, string, error)) mohttp.Handler {
	return mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		d, etag, err := fn(c)

		if err != nil {
			return nil, err
		}

		var (
			found = mohttp.GetRequest(c).Header.Get("If-None-Match")
			rw    = mohttp.GetResponseWriter(c)
		)

		rw.Header().Set("ETag", etag)

		if etag == found {
			rw.WriteHeader(304)
			return mohttp.DataNoBody, nil
		}

		return d, nil
	})
}
Exemple #4
0
	"github.com/NYTimes/gziphandler"
	"github.com/jonasi/mohttp"
	"golang.org/x/net/context"
	"net/http"
	"sync"
)

var GzipHandler mohttp.Handler = mohttp.HandlerFunc(func(c context.Context) {
	h := gziphandler.GzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w2 := &addContentTypeWriter{ResponseWriter: w}
		c2 := mohttp.WithResponseWriter(c, w2)

		mohttp.Next(c2)
	}))

	h.ServeHTTP(mohttp.GetResponseWriter(c), mohttp.GetRequest(c))
})

type addContentTypeWriter struct {
	o sync.Once
	http.ResponseWriter
}

func (w *addContentTypeWriter) Write(b []byte) (int, error) {
	w.o.Do(func() {
		h := w.ResponseWriter.Header()

		if h.Get("Content-Type") == "" {
			h.Set("Content-Type", http.DetectContentType(b))
		}
Exemple #5
0
	hateoas.Path("/commands"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		commander := getCommander(c)
		return commander.History(), nil
	})),
	hateoas.POST(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		var (
			logger    = plugin.GetLogger(c)
			commander = getCommander(c)
		)

		var args struct {
			Args []string `json:"args"`
		}

		if err := json.NewDecoder(mohttp.GetRequest(c).Body).Decode(&args); err != nil {
			logger.Error("JSON decoding error", "error", err)
			return nil, err
		}

		if len(args.Args) < 1 {
			return nil, fmt.Errorf("Invalid args")
		}

		return commander.Run(args.Args...)
	})),
)

var command = hateoas.NewResource(
	hateoas.Path("/commands/:id"),
	hateoas.AddLink("stdout", stdout),
Exemple #6
0
func FileHandler(fn func(context.Context) string) mohttp.Handler {
	return mohttp.HandlerFunc(func(c context.Context) {
		path := fn(c)
		http.ServeFile(mohttp.GetResponseWriter(c), mohttp.GetRequest(c), path)
	})
}
Exemple #7
0
func JSONBodyDecode(c context.Context, dest interface{}) error {
	r := mohttp.GetRequest(c)
	defer r.Body.Close()

	return json.NewDecoder(r.Body).Decode(dest)
}