cleanups.go 649 字节
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package middleware

import (
	"goku-ce-1.0/dao/cache"
	"github.com/codegangsta/inject"
	"github.com/farseer810/yawf"
	"log"
	"net/http"
)

func CleanupHandler(context yawf.Context, log *log.Logger) {
	defer func() {
		if err := recover(); err != nil {
			if log != nil {
				log.Printf("PANIC: %s\n", err)
			}

			val := context.Get(inject.InterfaceOf((*http.ResponseWriter)(nil)))
			res := val.Interface().(http.ResponseWriter)
			res.WriteHeader(http.StatusInternalServerError)

			res.Write([]byte("500 Internal Server Error"))
		}
		conn := cache.GetConnectionFromContext(context)
		if conn != nil {
			conn.Close()
		}
	}()
	context.Next()
}