Пример #1
0
func NewLogMiddle(s *stats.Stats) func(http.Handler) http.Handler {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			start := time.Now()
			res := &wrapper{w, 0, 200}
			ip := realip.RealIP(r)

			logger.Infof("[%s] >>> %s %s", ip, r.Method, r.RequestURI)
			h.ServeHTTP(w, r)
			size := humanize.Bytes(uint64(res.written))

			switch {
			case res.status >= 500:
				s.Incr("[error]")
				logger.Errorf(" [%s] << %s %s %d (%s) in %s", ip, r.Method, r.RequestURI,
					res.status, size, time.Since(start))
			case res.status >= 400:
				s.Incr("[error]")
				logger.Warningf(" [%s] << %s %s %d (%s) in %s", ip, r.Method, r.RequestURI,
					res.status, size, time.Since(start))
			default:
				s.Incr("[ok]")
				logger.Infof(" [%s] << %s %s %d (%s) in %s", ip, r.Method, r.RequestURI,
					res.status, size, time.Since(start))
			}
		})
	}
}
Пример #2
0
// HTTPServerDNS is handler of httpdns query
func HTTPServerDNS(w http.ResponseWriter, req *http.Request) {
	domain := req.URL.Query().Get("d")
	if len(domain) == 0 {
		domain = req.Form.Get("d")
		if len(domain) == 0 {
			http.NotFound(w, req)
			return
		}
	}
	w.Write([]byte(QueryDNS(domain, realip.RealIP(req))))
}
Пример #3
0
// HTTPServerMYIP is handler of show-my-ip query
func HTTPServerMYIP(w http.ResponseWriter, req *http.Request) {
	w.Write([]byte(realip.RealIP(req)))
}