Exemple #1
0
// create new helper, this object will be used for globar service for martini middleware
func New() *MartiniHelper {
	this := &MartiniHelper{inject.New()}
	retHandler := martini.New().Get(reflect.TypeOf(martini.ReturnHandler(nil))).Interface()
	// retHandler := martini.defaultReturnHandler()
	this.Map(retHandler)
	return this
}
Exemple #2
0
// convert martini middleware into http.Handler
func (this *MartiniHelper) Conv(h martini.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		rwc := karambie.Context(rw)
		c := this.context(rwc, r)

		vals, err := c.Invoke(h)
		if err != nil {
			panic(err)
		}

		if rwc.Status() == 0 {
			// if the handler returned something, write it to the http response
			if len(vals) > 0 {
				ev := c.Get(reflect.TypeOf(martini.ReturnHandler(nil)))
				handleReturn := ev.Interface().(martini.ReturnHandler)
				handleReturn(c, vals)
			}
		}
	})
}