예제 #1
0
파일: interpose.go 프로젝트: pilwon/gogo
func Middleware(h func(http.Handler) http.Handler) middleware.Handler {
	return middleware.HandlerFunc(func(c context.Context, w http.ResponseWriter, r *http.Request) context.Context {
		h(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			gogo.Next(c, w, r)
		})).ServeHTTP(w, r)
		return nil
	})
}
예제 #2
0
파일: logger.go 프로젝트: pilwon/gogo
func (l *Logger) ServeHTTP(c context.Context, w http.ResponseWriter, r *http.Request) context.Context {
	start := time.Now()
	l.Printf("Started %s %s", r.Method, r.URL.Path)

	gogo.Next(c, w, r)

	res := w.(gogo.ResponseWriter)
	l.Printf("Completed %v %s in %v", res.Status(), http.StatusText(res.Status()), time.Since(start))

	return nil
}
예제 #3
0
파일: recovery.go 프로젝트: pilwon/gogo
func (rec *Recovery) ServeHTTP(c context.Context, w http.ResponseWriter, r *http.Request) context.Context {
	defer func() {
		if err := recover(); err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			stack := make([]byte, rec.StackSize)
			stack = stack[:runtime.Stack(stack, rec.StackAll)]

			f := "PANIC: %s\n%s"
			rec.Logger.Printf(f, err, stack)

			if rec.PrintStack {
				fmt.Fprintf(w, f, err, stack)
			} else {
				fmt.Fprintf(w, "Internal Server Error")
			}
		}
	}()
	gogo.Next(c, w, r)
	return nil
}
예제 #4
0
func (l *GorillaLogger) ServeHTTP(c context.Context, w http.ResponseWriter, r *http.Request) context.Context {
	gorillaHandlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gogo.Next(c, w, r)
	})).ServeHTTP(w, r)
	return nil
}