示例#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
}