Example #1
0
func TemplateHandler(fn func(c context.Context) (string, map[string]interface{})) mohttp.Handler {
	return mohttp.HandlerFunc(func(c context.Context) {
		var (
			name, data = fn(c)
			t          = templateContextValue.Get(c).(*templateOptions)
		)
		t.template.ExecuteTemplate(mohttp.GetResponseWriter(c), name, data)
	})
}
Example #2
0
func TextHandler(h mohttp.DataHandlerFunc) mohttp.Handler {
	return mohttp.HandlerFunc(func(c context.Context) {
		r, _ := mohttp.GetDataResponder(c)

		if _, ok := r.(*TextResponder); !ok {
			c = mohttp.WithDataResponder(c, &TextResponder{})
		}

		h.Handle(c)
	})
}
Example #3
0
func JSONHandler(fn mohttp.DataHandlerFunc) mohttp.Handler {
	return mohttp.HandlerFunc(func(c context.Context) {
		r, _ := mohttp.GetDataResponder(c)

		if _, ok := r.(*JSONResponder); !ok {
			c = mohttp.WithDataResponder(c, &JSONResponder{})
		}

		fn.Handle(c)
	})
}
Example #4
0
package middleware

import (
	"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") == "" {
Example #5
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)
	})
}
Example #6
0
var JSON = mohttp.DataResponderHandler(&middleware.JSONResponder{
	OnError: func(c context.Context, err error) interface{} {
		return map[string]interface{}{
			"error": err.Error(),
		}
	},

	Transform: func(data interface{}) interface{} {
		return map[string]interface{}{
			"data": data,
		}
	},
})

var AddLinkHeaders = mohttp.HandlerFunc(func(c context.Context) {
	res, ok := hateoas.GetResource(c)

	if ok {
		var (
			rw    = mohttp.GetResponseWriter(c)
			links = res.Links()
		)

		for i := range links {
			rw.Header().Add("Link", links[i].Header())
		}
	}

	mohttp.Next(c)
})
Example #7
0
)

var version = hateoas.NewResource(
	hateoas.Path("/version"),
	hateoas.GET(mohttp.StaticDataHandler(Version)),
)

var status = hateoas.NewResource(
	hateoas.Path("/status"),
	hateoas.PATCH(mohttp.HandlerFunc(func(c context.Context) {
		var body struct {
			Status string `json:"status"`
		}

		if err := middleware.JSONBodyDecode(c, &body); err != nil {
			mohttp.GetResponseWriter(c).WriteHeader(500)
			return
		}

		if body.Status == "inactive" {
			getServer(c).Close()
		}
	})),
)

var plugins = hateoas.NewResource(
	hateoas.Path("/plugins"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		var (
			plugins = getServer(c).plugins
			resp    = make([]pl, len(plugins))
		)