コード例 #1
0
ファイル: main.go プロジェクト: espebra/filebin
func reqHandler(fn func(http.ResponseWriter, *http.Request, config.Configuration, model.Context)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		startTime := time.Now().UTC()
		reqId := "r-" + generateReqId(5)

		// Populate the context for this request here
		var ctx = model.Context{}
		ctx.TemplateBox = templateBox
		ctx.StaticBox = staticBox
		ctx.Baseurl = cfg.Baseurl
		ctx.WorkQueue = WorkQueue
		ctx.Backend = &backend
		ctx.Metrics = &m
		ctx.Events = &e

		if cfg.ClientAddrHeader == "" {
			// Extract the IP address only
			ip, _, err := net.SplitHostPort(r.RemoteAddr)
			if err == nil {
				ctx.RemoteAddr = ip
			} else {
				ctx.RemoteAddr = r.RemoteAddr
			}
		} else {
			ctx.RemoteAddr = r.Header.Get(cfg.ClientAddrHeader)
		}

		// Initialize logger for this request
		ctx.Log = log.New(os.Stdout, reqId+" ", log.LstdFlags)

		ctx.Log.Println(r.Method + " " + r.RequestURI)
		if r.Host != "" {
			ctx.Log.Println("Host: " + r.Host)
		}
		ctx.Log.Println("Remote address: " + ctx.RemoteAddr)

		// Print X-Forwarded-For since we might be behind some TLS
		// terminator and web cache
		xff := r.Header.Get("X-Forwarded-For")
		if xff != "" && xff != ctx.RemoteAddr {
			ctx.Log.Println("X-Forwarded-For: " + xff)
		}

		referer := r.Header.Get("Referer")
		if referer != "" {
			ctx.Log.Println("Referer: " + referer)
		}

		ua := r.Header.Get("User-Agent")
		if ua != "" {
			ctx.Log.Println("User-Agent: " + ua)
		}

		fn(w, r, cfg, ctx)

		finishTime := time.Now().UTC()
		elapsedTime := finishTime.Sub(startTime)
		ctx.Log.Println("Response time: " + elapsedTime.String())
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: jmcarbo/filebin
func reqHandler(fn func(http.ResponseWriter, *http.Request, config.Configuration, model.Context)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		startTime := time.Now().UTC()
		reqId := "r-" + generateReqId(5)

		// Populate the context for this request here
		var ctx = model.Context{}
		ctx.TemplateBox = templateBox
		ctx.StaticBox = staticBox
		ctx.Baseurl = cfg.Baseurl
		ctx.WorkQueue = WorkQueue

		// Initialize logger for this request
		ctx.Log = log.New(os.Stdout, reqId+" ", log.LstdFlags)

		ctx.Log.Println(r.Method + " " + r.RequestURI)
		ctx.Log.Println("Remote address: " + r.RemoteAddr)

		// Print X-Forwarded-For since we might be behind some TLS
		// terminator and web cache
		xff := r.Header.Get("X-Forwarded-For")
		if xff != "" {
			ctx.Log.Println("X-Forwarded-For: " + xff)
		}
		ua := r.Header.Get("User-Agent")
		if ua != "" {
			ctx.Log.Println("User-Agent: " + ua)
		}

		fn(w, r, cfg, ctx)

		finishTime := time.Now().UTC()
		elapsedTime := finishTime.Sub(startTime)
		ctx.Log.Println("Response time: " + elapsedTime.String())
	}
}