From b8faa34ecccccf42912d850a42ecb624c4e5ae6f Mon Sep 17 00:00:00 2001 From: sunface Date: Tue, 11 Sep 2018 17:23:14 +0800 Subject: [PATCH] update logger --- api/api_server.go | 23 +++++++++------- api/filter/filter.go | 4 +-- api/filter/traffic_rate_limiter.go | 4 +-- api/filter/traffic_route.go | 13 +++++++-- api/load_data.go | 8 +++--- api/manage/api.go | 42 +++++++++++++++--------------- api/manage/audit/audit_log.go | 6 ++--- api/manage/strategy/strategy.go | 14 +++++----- api/manage/test_api.go | 4 +-- api/req/req.go | 20 +++++++++++--- api/router.go | 37 +++++++++++++------------- cmd/api.go | 4 +-- cmd/traffic.go | 4 +-- juz.conf | 3 ++- misc/config.go | 1 + misc/data.go | 4 +-- traffic/load_data.go | 4 +-- traffic/traffic.go | 10 +++---- 18 files changed, 116 insertions(+), 89 deletions(-) diff --git a/api/api_server.go b/api/api_server.go index c2cfe8c9..1a203870 100644 --- a/api/api_server.go +++ b/api/api_server.go @@ -14,6 +14,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/labstack/echo" + "github.com/labstack/echo/middleware" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "go.uber.org/zap" @@ -25,7 +26,7 @@ type ApiServer struct { } func (p *ApiServer) Start() { - g.Info("start tfe..") + g.L.Info("start tfe..") // 获取所有内部服务节点信息 g.ETCD.QueryAll(misc.Conf.Etcd.Addrs) @@ -53,11 +54,15 @@ func (p *ApiServer) Start() { } func (o *ApiServer) Shutdown() { - g.Info("shutdown tfe..") + g.L.Info("shutdown tfe..") } func (p *ApiServer) listen() { e := echo.New() + e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ + AllowHeaders: append([]string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept}, misc.Conf.Api.Cors...), + AllowCredentials: true, + })) // 回调相关 //同步回调接口 @@ -70,7 +75,7 @@ func timing(f echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { ts := time.Now() rid := (ts.UnixNano()/10)*10 + misc.Conf.Api.ServerID - g.Info("New request accepted", zap.Int64("rid", rid), zap.String("ip", c.RealIP())) + g.L.Info("New request accepted", zap.Int64("rid", rid), zap.String("ip", c.RealIP())) c.Set("rid", rid) defer func() { // 统计请求指标 @@ -85,9 +90,9 @@ func timing(f echo.HandlerFunc) echo.HandlerFunc { err := c.Get("error_msg") if err == nil { - g.Info("Request success", zap.Int64("rid", rid)) + g.L.Info("Request success", zap.Int64("rid", rid)) } else { - g.Info("Request failed", zap.Int64("rid", rid), zap.Error(err.(error))) + g.L.Info("Request failed", zap.Int64("rid", rid), zap.Error(err.(error))) } }() @@ -98,7 +103,7 @@ func timing(f echo.HandlerFunc) echo.HandlerFunc { func (as *ApiServer) initTraffic() { r, err := rpc.Dial("tcp", misc.Conf.Traffic.Host+":"+misc.Conf.Traffic.Port) if err != nil { - g.Fatal("connect to raffic error", zap.Error(err)) + g.L.Fatal("connect to raffic error", zap.Error(err)) } as.router.Filter.Rpc = r @@ -108,15 +113,15 @@ func (as *ApiServer) initTraffic() { var res int err := as.router.Filter.Rpc.Call("RateLimiter.Ping", 1, &res) if err != nil || res != 1 { - g.Warn("rpc ping failed", zap.Error(err)) + g.L.Warn("rpc ping failed", zap.Error(err)) r, err := rpc.Dial("tcp", misc.Conf.Traffic.Host+":"+misc.Conf.Traffic.Port) if err != nil { - g.Warn("re-connect to traffic error", zap.Error(err)) + g.L.Warn("re-connect to traffic error", zap.Error(err)) time.Sleep(2 * time.Second) continue } as.router.Filter.Rpc = r - g.Info("re-connect to traffic ok") + g.L.Info("re-connect to traffic ok") } time.Sleep(3 * time.Second) diff --git a/api/filter/filter.go b/api/filter/filter.go index 4507ce96..2a8df04a 100644 --- a/api/filter/filter.go +++ b/api/filter/filter.go @@ -44,7 +44,7 @@ func (f *Filter) BeforeRoute(r *req.Request) Result { // 黑白名单 err := f.checkBW(r) if err != nil { - g.Info("BeforeRoute failed", zap.Error(err)) + g.L.Info("BeforeRoute failed", zap.Error(err)) // 统计被阻挡数 stats.Limits.With(prometheus.Labels{ "api_id": r.Api.APIID, @@ -57,7 +57,7 @@ func (f *Filter) BeforeRoute(r *req.Request) Result { // 参数校验 err = f.verifyParam(r) if err != nil { - g.Info("BeforeRoute failed", zap.Error(err)) + g.L.Info("BeforeRoute failed", zap.Error(err)) return Result{http.StatusBadRequest, g.ParamInvalidC, g.ParamInvalidE} } diff --git a/api/filter/traffic_rate_limiter.go b/api/filter/traffic_rate_limiter.go index a7983d61..9734c156 100644 --- a/api/filter/traffic_rate_limiter.go +++ b/api/filter/traffic_rate_limiter.go @@ -21,7 +21,7 @@ func (f *Filter) DecApiRate(r *req.Request) { err := f.Rpc.Call("RateLimiter.DecApiRate", &misc.TrafficConReq{r.Api.APIID, r.Api.TrafficStrategy, ""}, &res) if err != nil { if !strings.Contains(err.Error(), "shut down") { - g.Warn("rpc出错了", zap.Error(err)) + g.L.Warn("rpc出错了", zap.Error(err)) } } } @@ -48,7 +48,7 @@ func (f *Filter) IncApiRate(r *req.Request) (int, error) { err := f.Rpc.Call("RateLimiter.IncApiRate", &misc.TrafficConReq{r.Api.APIID, r.Api.TrafficStrategy, val}, &res) if err != nil { if !strings.Contains(err.Error(), "shut down") { - g.Warn("rpc出错了", zap.Error(err)) + g.L.Warn("rpc出错了", zap.Error(err)) } return 0, nil } diff --git a/api/filter/traffic_route.go b/api/filter/traffic_route.go index 65f399f6..fb0317ce 100644 --- a/api/filter/traffic_route.go +++ b/api/filter/traffic_route.go @@ -33,7 +33,11 @@ func (f *Filter) trafficRoute(r *req.Request) { api := apiI.(*misc.API) // 是否在路由ip列表中,如果在,直接路由 if strings.Contains(r.Api.TrafficIPs, r.ClientIP) { - g.Debug(r.DebugOn, "Canary by ip", zap.String("old_api", r.Api.APIID), zap.String("new_api", api.APIID), zap.String("client_ip", r.ClientIP)) + if r.DebugOn { + g.DL.Debug("Canary by ip", zap.String("old_api", r.Api.APIID), zap.String("new_api", api.APIID), zap.String("client_ip", r.ClientIP)) + } else { + g.L.Debug("Canary by ip", zap.String("old_api", r.Api.APIID), zap.String("new_api", api.APIID), zap.String("client_ip", r.ClientIP)) + } r.Api = api return } @@ -43,6 +47,11 @@ func (f *Filter) trafficRoute(r *req.Request) { if n > r.Api.TrafficRatio { return } - g.Debug(r.DebugOn, "Canary by random", zap.String("old_api", r.Api.APIID), zap.String("new_api", api.APIID)) + if r.DebugOn { + g.DL.Debug("Canary by random", zap.String("old_api", r.Api.APIID), zap.String("new_api", api.APIID)) + } else { + g.L.Debug("Canary by random", zap.String("old_api", r.Api.APIID), zap.String("new_api", api.APIID)) + } + r.Api = api } diff --git a/api/load_data.go b/api/load_data.go index 1b35d49a..c69dcaa8 100644 --- a/api/load_data.go +++ b/api/load_data.go @@ -38,7 +38,7 @@ func (p *ApiServer) loadAll() { apisS := make([]*misc.API, 0) err := g.DB.Select(&apisS, "select * from api_release") if err != nil { - g.Fatal("load apis error!", zap.Error(err)) + g.L.Fatal("load apis error!", zap.Error(err)) } an := make([]string, 0, len(apisS)) @@ -68,7 +68,7 @@ func (p *ApiServer) loadAll() { strategies := make([]*misc.Strategy, 0) err = g.DB.Select(&strategies, "select * from strategy") if err != nil { - g.Fatal("load strategies error!", zap.Error(err)) + g.L.Fatal("load strategies error!", zap.Error(err)) } for _, s := range strategies { @@ -110,7 +110,7 @@ func (p *ApiServer) loadUpdated() { err := g.DB.Select(&apisS, fmt.Sprintf("select * from api_release where modify_date >= '%s'", lastT)) if err != nil { - g.Error("load apis error!", zap.Error(err)) + g.L.Error("load apis error!", zap.Error(err)) return } @@ -149,7 +149,7 @@ func (p *ApiServer) loadUpdated() { query := fmt.Sprintf("select * from strategy where modify_date >= '%s'", lastT) err := g.DB.Select(&strategies, query) if err != nil { - g.Error("load strategies error!", zap.Error(err), zap.String("query", query)) + g.L.Error("load strategies error!", zap.Error(err), zap.String("query", query)) return } diff --git a/api/manage/api.go b/api/manage/api.go index e14b4d95..df4d7f87 100644 --- a/api/manage/api.go +++ b/api/manage/api.go @@ -59,7 +59,7 @@ func (m *Manage) QueryAPI(c echo.Context) error { err := g.DB.Select(&apis, query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -101,7 +101,7 @@ func (m *Manage) CountAPI(c echo.Context) error { rows, err := g.DB.Query(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -156,7 +156,7 @@ func (m *Manage) DefineAPI(c echo.Context) error { Message: g.AlreadyExistE, }) } - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -169,7 +169,7 @@ func (m *Manage) DefineAPI(c echo.Context) error { api.APIID, api.PathType, api.Service, api.BackendAddr, misc.API_OFFLINE, date) _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -182,7 +182,7 @@ func (m *Manage) DefineAPI(c echo.Context) error { *api.Desc, api.RouteType, api.BackendAddr, api.BackendType, api.BwStrategy, api.RetryStrategy, api.TrafficStrategy, *api.MockData, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, pr, api.CachedTime, api.App, api.AddrType, api.BackendURI, api.Method, api.APIID) res, err := g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -246,7 +246,7 @@ func (m *Manage) DeleteAPI(c echo.Context) error { query := fmt.Sprintf("select status,modify_date from api_release where api_id='%s'", apiID) rows, err := g.DB.Query(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -287,7 +287,7 @@ func (m *Manage) DeleteAPI(c echo.Context) error { query = fmt.Sprintf("delete from api_release where api_id='%s'", apiID) _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -300,7 +300,7 @@ func (m *Manage) DeleteAPI(c echo.Context) error { query = fmt.Sprintf("select * from api_define where api_id='%s'", apiID) err = g.DB.Get(&api, query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) } if api.ParamTable != nil { d, _ := g.B64.DecodeString(*api.ParamTable) @@ -314,7 +314,7 @@ func (m *Manage) DeleteAPI(c echo.Context) error { query = fmt.Sprintf("delete from api_define where api_id='%s'", apiID) _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -335,7 +335,7 @@ func (m *Manage) parseAPI(c echo.Context) (*misc.API, int, string) { api := &misc.API{} err := json.Unmarshal([]byte(apiR), &api) if err != nil { - g.Info("parse api", zap.Error(err), zap.String("api", string(apiR))) + g.L.Info("parse api", zap.Error(err), zap.String("api", string(apiR))) return nil, g.ParamInvalidC, g.ParamInvalidE } @@ -448,7 +448,7 @@ func (m *Manage) APIRelease(c echo.Context) error { query := fmt.Sprintf("select * from api_define where api_id='%s'", apiID) err := g.DB.Get(&api, query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -462,7 +462,7 @@ func (m *Manage) APIRelease(c echo.Context) error { _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -473,7 +473,7 @@ func (m *Manage) APIRelease(c echo.Context) error { query = fmt.Sprintf("update api_define set release_version='%s' where api_id='%s'", api.ReviseVersion, api.APIID) _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -519,7 +519,7 @@ func (m *Manage) APIOffline(c echo.Context) error { misc.API_OFFLINE, apiID) _, err := g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -531,7 +531,7 @@ func (m *Manage) APIOffline(c echo.Context) error { query = fmt.Sprintf("update api_define set release_version='%s' where api_id='%s'", "", apiID) _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -544,7 +544,7 @@ func (m *Manage) APIOffline(c echo.Context) error { query = fmt.Sprintf("select * from api_release where api_id='%s'", apiID) err = g.DB.Get(&api, query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) } if api.ParamTable != nil { d, _ := g.B64.DecodeString(*api.ParamTable) @@ -646,7 +646,7 @@ func (m *Manage) APIBatchStrategy(c echo.Context) error { query = fmt.Sprintf("%s where api_id = '%s'", query, apiID) res, err := g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -724,7 +724,7 @@ func (m *Manage) APIBatchDelStrategy(c echo.Context) error { res, err := g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -793,7 +793,7 @@ func (m *Manage) APIBatchRelease(c echo.Context) error { query := fmt.Sprintf("select * from api_define where api_id='%s'", apiID) err := g.DB.Get(&api, query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -807,7 +807,7 @@ func (m *Manage) APIBatchRelease(c echo.Context) error { _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -818,7 +818,7 @@ func (m *Manage) APIBatchRelease(c echo.Context) error { query = fmt.Sprintf("update api_define set release_version='%s' where api_id='%s'", api.ReviseVersion, api.APIID) _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, diff --git a/api/manage/audit/audit_log.go b/api/manage/audit/audit_log.go index c7c5d9c1..f27f60eb 100644 --- a/api/manage/audit/audit_log.go +++ b/api/manage/audit/audit_log.go @@ -33,7 +33,7 @@ func Log(userID string, service string, targetType int, targetID string, opType userID, service, targetType, targetID, opType, newc, desc) _, err := g.DB.Exec(query) if err != nil { - g.Info("record audit log error", zap.Error(err), zap.String("query", query)) + g.L.Info("record audit log error", zap.Error(err), zap.String("query", query)) } } @@ -56,7 +56,7 @@ func Count(c echo.Context) error { } rows, err := g.DB.Query(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -116,7 +116,7 @@ func Load(c echo.Context) error { } err := g.DB.Select(&rs, query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, diff --git a/api/manage/strategy/strategy.go b/api/manage/strategy/strategy.go index a982f327..f1362de0 100644 --- a/api/manage/strategy/strategy.go +++ b/api/manage/strategy/strategy.go @@ -65,7 +65,7 @@ func Create(c echo.Context) error { Message: "Strategy name already exist", }) } - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -103,7 +103,7 @@ func Update(c echo.Context) error { st.Name, st.SubType, st.Content, st.ID) _, err := g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -139,7 +139,7 @@ func Load(c echo.Context) error { err := g.DB.Select(&ss, query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -191,7 +191,7 @@ func Delete(c echo.Context) error { } _, err := g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -209,7 +209,7 @@ func Delete(c echo.Context) error { } _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -220,7 +220,7 @@ func Delete(c echo.Context) error { query = fmt.Sprintf("delete from strategy where id='%s'", id) _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -266,7 +266,7 @@ func Change(c echo.Context) error { query := fmt.Sprintf("update strategy set status='%d' where id='%s'", newS, id) _, err := g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, diff --git a/api/manage/test_api.go b/api/manage/test_api.go index d4c4bfbb..b0216cb5 100644 --- a/api/manage/test_api.go +++ b/api/manage/test_api.go @@ -31,7 +31,7 @@ func (m *Manage) APIQueryParam(c echo.Context) error { query := fmt.Sprintf("select params from test_api where api_id='%s'", apiID) rows, err := g.DB.Query(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, @@ -78,7 +78,7 @@ func (m *Manage) APISaveParam(c echo.Context) error { _, err = g.DB.Exec(query) if err != nil { - g.Info("access database error", zap.Error(err), zap.String("query", query)) + g.L.Info("access database error", zap.Error(err), zap.String("query", query)) return c.JSON(http.StatusInternalServerError, g.Result{ Status: http.StatusInternalServerError, ErrCode: g.DatabaseC, diff --git a/api/req/req.go b/api/req/req.go index 203e7f67..05ca2d48 100644 --- a/api/req/req.go +++ b/api/req/req.go @@ -20,6 +20,7 @@ type Request struct { Api *misc.API DebugOn bool ClientIP string + Url string BwStrategy *misc.BwStrategy RetryStrategy *misc.RetryStrategy @@ -30,7 +31,7 @@ func (r *Request) String() string { return fmt.Sprintf("method: %s,params: %v, api: %v, client_ip: %s", r.Method, r.Params, *r.Api, r.ClientIP) } -func Parse(c echo.Context) (*Request, error) { +func Parse(c echo.Context) (*Request, int, error) { r := &Request{ Rid: c.Get("rid").(int64), Params: make(map[string]string), @@ -67,7 +68,7 @@ func Parse(c echo.Context) (*Request, error) { //新网关使用以下参数'api_name' apiName = r.Params["api_name"] if apiName == "" { - return r, errors.New("api_name not founded") + return r, http.StatusBadRequest, errors.New("api_name not founded") } } } else { @@ -84,14 +85,25 @@ func Parse(c echo.Context) (*Request, error) { // 获取api信息 apiI, ok := misc.Apis.Load(apiID) if !ok { - return r, errors.New("api id not exist") + return r, http.StatusBadRequest, errors.New("api id not exist") } r.Api = apiI.(*misc.API) // 生成策略 strategy(r) - return r, nil + // 获取url + if r.Api.AddrType == misc.ADDR_URL { // direct url + r.Url = r.Api.BackendAddr + } else { // get url from etcd + s := g.GetServer(r.Api.BackendAddr) + if s == nil { + return r, http.StatusServiceUnavailable, errors.New(g.NoServerAvailableE) + } + r.Url = "http://" + s.IP + r.Api.BackendURI + } + + return r, 0, nil } func strategy(r *Request) { diff --git a/api/router.go b/api/router.go index 8a50cbb9..f1bfa18a 100644 --- a/api/router.go +++ b/api/router.go @@ -38,30 +38,35 @@ type router struct { /*----------------------请求路由---------------------*/ func (router *router) route(c echo.Context) error { // 解析请求 - r, err := req.Parse(c) + r, status, err := req.Parse(c) if err != nil { c.Set("api_id", "error_api_id") c.Set("service", "error_service") c.Set("app", "error_app") c.Set("error_msg", err) - return c.JSON(http.StatusBadRequest, g.Result{r.Rid, http.StatusBadRequest, g.ParamInvalidC, err.Error(), nil}) + return c.JSON(http.StatusOK, g.Result{r.Rid, status, g.ParamInvalidC, err.Error(), nil}) } + c.Set("api_id", r.Api.APIID) c.Set("service", r.Api.Service) c.Set("app", r.Api.App) - g.Debug(r.DebugOn, "request content", zap.Int64("rid", r.Rid), zap.String("req", r.String())) + if r.DebugOn { + g.DL.Debug("request content", zap.Int64("rid", r.Rid), zap.String("req", r.String())) + } else { + g.L.Debug("request content", zap.Int64("rid", r.Rid), zap.String("req", r.String())) + } // 判断api是否发布 if r.Api.Status != misc.API_RELEASED { - return c.JSON(http.StatusBadRequest, g.Result{r.Rid, http.StatusBadRequest, manage.APIOfflineC, manage.APIOfflineE, nil}) + return c.JSON(http.StatusOK, g.Result{r.Rid, http.StatusBadRequest, manage.APIOfflineC, manage.APIOfflineE, nil}) } // 在请求路由之前进行过滤 res := router.BeforeRoute(r) if res.Status != 0 { c.Set("error_msg", errors.New(res.Emsg)) - return c.JSON(res.Status, g.Result{r.Rid, res.Status, res.Ecode, res.Emsg, nil}) + return c.JSON(http.StatusOK, g.Result{r.Rid, res.Status, res.Ecode, res.Emsg, nil}) } // 开始请求 @@ -80,19 +85,23 @@ func (router *router) route(c echo.Context) error { // 请求失败,通知客户端 if err != nil { c.Set("error_msg", err) - return c.JSON(code, g.Result{r.Rid, code, g.ReqFailedC, err.Error(), nil}) + return c.JSON(http.StatusOK, g.Result{r.Rid, code, g.ReqFailedC, err.Error(), nil}) } case misc.BACKEND_TYPE_MOCK: code = http.StatusOK body = talent.String2Bytes(*r.Api.MockData) } - g.Debug(r.DebugOn, "response body", zap.Int64("rid", r.Rid), zap.Int("code", code), zap.String("body", talent.Bytes2String(body))) + if r.DebugOn { + g.DL.Debug("response body", zap.Int64("rid", r.Rid), zap.Int("code", code), zap.String("body", talent.Bytes2String(body))) + } else { + g.L.Debug("response body", zap.Int64("rid", r.Rid), zap.Int("code", code), zap.String("body", talent.Bytes2String(body))) + } // 成功时,把请求id放在header中返回,避免污染返回结果 c.Response().Header().Add("rid", strconv.FormatInt(r.Rid, 10)) - // 返回给客户端成功的结果 + // 返回给客户端请求的结果 return c.String(code, talent.Bytes2String(body)) } @@ -124,17 +133,7 @@ func (rt *router) sync(r *req.Request) (int, []byte, error) { // 写入客户端真实ip req.Header.Set("X-Forwarded-For", r.ClientIP) - var url string - // 获取url - if r.Api.AddrType == misc.ADDR_URL { // direct url - url = r.Api.BackendAddr - } else { // get url from etcd - s := g.GetServer(r.Api.BackendAddr) - if s == nil { - return http.StatusServiceUnavailable, nil, errors.New("no server available") - } - url = "http://" + s.IP + r.Api.BackendURI - } + var url = r.Url switch r.Api.Method { case "GET": // 拼接url diff --git a/cmd/api.go b/cmd/api.go index d4677be6..bef000c3 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -39,7 +39,7 @@ var apiCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { misc.InitConfig("juz.conf") g.InitLogger(misc.Conf.Common.LogLevel) - g.Info("Application version", zap.String("version", misc.Conf.Common.Version)) + g.L.Info("Application version", zap.String("version", misc.Conf.Common.Version)) p := &api.ApiServer{} p.Start() @@ -49,7 +49,7 @@ var apiCmd = &cobra.Command{ signal.Notify(chSig, syscall.SIGINT, syscall.SIGTERM) sig := <-chSig - g.Info("juz received signal", zap.Any("signal", sig)) + g.L.Info("juz received signal", zap.Any("signal", sig)) }, } diff --git a/cmd/traffic.go b/cmd/traffic.go index 49917533..7847fc32 100644 --- a/cmd/traffic.go +++ b/cmd/traffic.go @@ -36,7 +36,7 @@ var trafficCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { misc.InitConfig("juz.conf") g.InitLogger(misc.Conf.Common.LogLevel) - g.Info("Application version", zap.String("version", misc.Conf.Common.Version)) + g.L.Info("Application version", zap.String("version", misc.Conf.Common.Version)) p := &traffic.Traffic{} p.Start() @@ -46,7 +46,7 @@ var trafficCmd = &cobra.Command{ signal.Notify(chSig, syscall.SIGINT, syscall.SIGTERM) sig := <-chSig - g.Info("juz received signal", zap.Any("signal", sig)) + g.L.Info("juz received signal", zap.Any("signal", sig)) }, } diff --git a/juz.conf b/juz.conf index 0b6275de..2fd7484e 100644 --- a/juz.conf +++ b/juz.conf @@ -6,7 +6,8 @@ common: api: port: "8081" serverid: 1 - + cors: + - "MF-Token" manage: port: "8089" diff --git a/misc/config.go b/misc/config.go index 4ba74174..04cbf62b 100644 --- a/misc/config.go +++ b/misc/config.go @@ -30,6 +30,7 @@ type Config struct { Api struct { Port string ServerID int64 + Cors []string } Manage struct { Port string diff --git a/misc/data.go b/misc/data.go index be3b68ae..1540e598 100644 --- a/misc/data.go +++ b/misc/data.go @@ -132,12 +132,12 @@ func InitMysql() { Conf.Mysql.Addr, Conf.Mysql.Port, Conf.Mysql.Database) g.DB, err = sqlx.Open("mysql", sqlConn) if err != nil { - g.Fatal("init mysql error", zap.Error(err)) + g.L.Fatal("init mysql error", zap.Error(err)) } // 测试db是否正常 err = g.DB.Ping() if err != nil { - g.Fatal("init mysql, ping error", zap.Error(err)) + g.L.Fatal("init mysql, ping error", zap.Error(err)) } } diff --git a/traffic/load_data.go b/traffic/load_data.go index aa1e9d98..93dab2a9 100644 --- a/traffic/load_data.go +++ b/traffic/load_data.go @@ -29,7 +29,7 @@ func (t *Traffic) loadAll() { strategies := make([]*misc.Strategy, 0) err := g.DB.Select(&strategies, "select * from strategy") if err != nil { - g.Fatal("load strategies error!", zap.Error(err)) + g.L.Fatal("load strategies error!", zap.Error(err)) } for _, s := range strategies { @@ -53,7 +53,7 @@ func (t *Traffic) loadUpdated() { query := fmt.Sprintf("select * from strategy where modify_date >= '%s'", lastT) err := g.DB.Select(&strategies, query) if err != nil { - g.Error("load strategies error!", zap.Error(err), zap.String("query", query)) + g.L.Error("load strategies error!", zap.Error(err), zap.String("query", query)) return } diff --git a/traffic/traffic.go b/traffic/traffic.go index 3b06d03d..6a787e51 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -33,7 +33,7 @@ func (t *Traffic) Start() { } func (t *Traffic) Shutdown() { - g.Info("shutdown tfe..") + g.L.Info("shutdown tfe..") } type RateLimiter struct{} @@ -49,20 +49,20 @@ func (t *Traffic) startRpcServer() { server := rpc.NewServer() err := server.Register(rl) if err != nil { - g.Fatal("register error", zap.Error(err)) + g.L.Fatal("register error", zap.Error(err)) } - g.Info("Listen tcp on port", zap.String("port", misc.Conf.Traffic.Port)) + g.L.Info("Listen tcp on port", zap.String("port", misc.Conf.Traffic.Port)) l, err := net.Listen("tcp", ":"+misc.Conf.Traffic.Port) if err != nil { - g.Fatal("listen error", zap.Error(err)) + g.L.Fatal("listen error", zap.Error(err)) } go func() { for { conn, err := l.Accept() if err != nil { - g.Error("accept error", zap.Error(err)) + g.L.Error("accept error", zap.Error(err)) continue } server.ServeConn(conn)