http.go 2.5 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5
package gateway

import (
	"fmt"
	"net/http"
Y
Your Name 已提交
6
	"strconv"
Y
Your Name 已提交
7 8
	"time"

Y
Your Name 已提交
9 10 11 12
	"github.com/eolinker/goku-api-gateway/diting"
	goku_labels "github.com/eolinker/goku-api-gateway/goku-labels"
	"github.com/eolinker/goku-api-gateway/node/monitor"

Y
Your Name 已提交
13 14 15 16 17 18 19
	log "github.com/eolinker/goku-api-gateway/goku-log"
	access_log "github.com/eolinker/goku-api-gateway/goku-node/access-log"
	"github.com/eolinker/goku-api-gateway/goku-node/common"
	"github.com/eolinker/goku-api-gateway/node/utils"
	fields "github.com/eolinker/goku-api-gateway/server/access-field"
)

Y
Your Name 已提交
20
var systemRequestPath = map[string]bool{"/oauth2/token": true, "/oauth2/authorize": true, "/oauth2/verify": true}
Y
Your Name 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

//HTTPHandler httpHandler
type HTTPHandler struct {
	router *Before
}

func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	timeStart := time.Now()
	// 记录访问次数
	requestID := utils.GetRandomString(16)

	ctx := common.NewContext(req, requestID, w)

	log.Debug(requestID, " url: ", ctx.Request().URL().String())
	log.Debug(requestID, " header: ", ctx.RequestOrg.Header.String())

	if log.GetLogger().Level == log.DebugLevel {
		rawBody, err := ctx.RequestOrg.RawBody()
		if err == nil {
			log.Debug(requestID, " body: ", string(rawBody))
		}
	}
	remoteAddr := utils.Intercept(req.RemoteAddr, ":")
	ctx.LogFields[fields.RemoteAddr] = remoteAddr

	if realIP := ctx.GetHeader("X-Real-Ip"); realIP == "" {
		ctx.ProxyRequest.SetHeader("X-Real-Ip", remoteAddr)
		ctx.LogFields[fields.HTTPXForwardedFor] = remoteAddr
	} else {
		ctx.LogFields[fields.HTTPXForwardedFor] = realIP
	}

	h.router.Router(w, req, ctx)

	n, status := ctx.Finish()

Y
Your Name 已提交
57
	delay := time.Since(timeStart)
Y
Your Name 已提交
58 59 60 61 62

	ctx.LogFields[fields.RequestID] = requestID
	ctx.LogFields[fields.StatusCode] = status
	ctx.LogFields[fields.HTTPUserAgent] = fmt.Sprint("\"", req.UserAgent(), "\"")
	ctx.LogFields[fields.HTTPReferer] = req.Referer()
Y
Your Name 已提交
63
	ctx.LogFields[fields.RequestTime] = delay
Y
Your Name 已提交
64
	ctx.LogFields[fields.Request] = fmt.Sprint("\"", req.Method, " ", req.URL.String(), " ", req.Proto, "\"")
Y
Your Name 已提交
65 66 67 68 69
	ctx.LogFields[fields.BodyBytesSent] = n
	ctx.LogFields[fields.Host] = req.Host
	access_log.Log(ctx.LogFields)
	log.WithFields(ctx.LogFields).Info()

Y
Your Name 已提交
70 71 72 73
	// /oauth2的path不参与统计
	if systemRequestPath[req.URL.Path] {
		return
	}
Y
Your Name 已提交
74
	// 监控计数
Y
Your Name 已提交
75
	labels := make(diting.Labels)
Y
Your Name 已提交
76 77 78 79

	labels[goku_labels.API] = strconv.Itoa(ctx.ApiID())
	labels[goku_labels.Strategy] = ctx.StrategyId()
	labels[goku_labels.Status] = strconv.Itoa(status)
Y
Your Name 已提交
80
	monitor.APIMonitor.Observe(float64(delay/time.Millisecond), labels)
Y
Your Name 已提交
81 82

}