예제 #1
0
파일: logger.go 프로젝트: sguzwf/vertex
package middleware

import (
	"net/http"

	"github.com/EverythingMe/vertex"
	"github.com/dvirsky/go-pylog/logging"
)

// RequestLogger is a middleware that logs the paths and return values of all requests
var RequestLogger = vertex.MiddlewareFunc(func(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error) {

	logging.Info("Handling %s %s", r.Method, r.URL.String())

	ret, err := next(w, r)

	logging.Info("Return value was %v %v", ret, err)
	return ret, err
})

//func StaticText(msg string) vertex.MiddlewareFunc {

//	//h := http.FileServer(dir)
//	return HandlerFunc(func(w http.ResponseWriter, r *http.Request) (interface{}, error) {

//		fmt.Fprintln(w, msg)
//		return nil, ErrHijacked

//	})
//}
예제 #2
0
파일: recover.go 프로젝트: sguzwf/vertex
package middleware

import (
	"net/http"

	"github.com/dvirsky/go-pylog/logging"

	"github.com/EverythingMe/vertex"
)

// AutoRecover is a middleware that recovers automatically from panics inside request handlers
var AutoRecover = vertex.MiddlewareFunc(func(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (ret interface{}, err error) {

	defer func() {

		e := recover()
		if e != nil {
			logging.Critical("Caught panic: %v", e)

			err = vertex.NewErrorf("PANIC handling %s: %s", r.URL.Path, e)
			return
		}
	}()

	return next(w, r)

})