[GitHub] rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts
rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts URL: https://github.com/apache/trafficcontrol/pull/2653#discussion_r209757007 ## File path: traffic_ops/traffic_ops_golang/crconfig/handler.go ## @@ -20,223 +20,146 @@ package crconfig */ import ( - "encoding/json" "errors" "net/http" "net/url" - "strconv" "time" "github.com/apache/trafficcontrol/lib/go-log" "github.com/apache/trafficcontrol/lib/go-tc" "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" - "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth" - "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config" - - "github.com/jmoiron/sqlx" ) // Handler creates and serves the CRConfig from the raw SQL data. // This MUST only be used for debugging or previewing, the raw un-snapshotted data MUST NOT be used by any component of the CDN. -func Handler(db *sqlx.DB, cfg config.Config) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - start := time.Now() - handleErrs := tc.GetHandleErrorsFunc(w, r) - - params, err := api.GetCombinedParams(r) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - - cdn, ok := params["cdn"] - if !ok { - handleErrs(http.StatusInternalServerError, errors.New("params missing CDN")) - return - } - - ctx := r.Context() - user, err := auth.GetCurrentUser(ctx) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - - crConfig, err := Make(db.DB, cdn, user.UserName, r.Host, r.URL.Path, cfg.Version) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } +func Handler(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn"}, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, errCode, userErr, sysErr) + return + } + defer inf.Close() - resp := struct { - Response *tc.CRConfig `json:"response"` - }{crConfig} - respBts, err := json.Marshal(resp) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - log.Infof("CRConfig time to generate: %+v\n", time.Since(start)) - w.Header().Set("Content-Type", "application/json") - w.Write(respBts) + start := time.Now() + crConfig, err := Make(inf.Tx.Tx, inf.Params["cdn"], inf.User.UserName, r.Host, r.URL.Path, inf.Config.Version) + if err != nil { + api.HandleErr(w, r, http.StatusInternalServerError, nil, err) + return } + log.Infof("CRConfig time to generate: %+v\n", time.Since(start)) + api.WriteResp(w, r, crConfig) } // SnapshotGetHandler gets and serves the CRConfig from the snapshot table. -func SnapshotGetHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - handleErrs := tc.GetHandleErrorsFunc(w, r) - params, err := api.GetCombinedParams(r) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - cdn, ok := params["cdn"] - if !ok { - handleErrs(http.StatusInternalServerError, errors.New("params missing CDN")) - return - } +func SnapshotGetHandler(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn"}, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, errCode, userErr, sysErr) + return + } + defer inf.Close() - snapshot, cdnExists, err := GetSnapshot(db.DB, cdn) - if err != nil { - handleErrs(http.StatusInternalServerError, errors.New("getting snapshot: "+err.Error())) - return - } - if !cdnExists { - handleErrs(http.StatusNotFound, errors.New("CDN not found")) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"response":` + snapshot + `}`)) + snapshot, cdnExists,
[GitHub] rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts
rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts URL: https://github.com/apache/trafficcontrol/pull/2653#discussion_r209757007 ## File path: traffic_ops/traffic_ops_golang/crconfig/handler.go ## @@ -20,223 +20,146 @@ package crconfig */ import ( - "encoding/json" "errors" "net/http" "net/url" - "strconv" "time" "github.com/apache/trafficcontrol/lib/go-log" "github.com/apache/trafficcontrol/lib/go-tc" "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" - "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth" - "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config" - - "github.com/jmoiron/sqlx" ) // Handler creates and serves the CRConfig from the raw SQL data. // This MUST only be used for debugging or previewing, the raw un-snapshotted data MUST NOT be used by any component of the CDN. -func Handler(db *sqlx.DB, cfg config.Config) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - start := time.Now() - handleErrs := tc.GetHandleErrorsFunc(w, r) - - params, err := api.GetCombinedParams(r) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - - cdn, ok := params["cdn"] - if !ok { - handleErrs(http.StatusInternalServerError, errors.New("params missing CDN")) - return - } - - ctx := r.Context() - user, err := auth.GetCurrentUser(ctx) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - - crConfig, err := Make(db.DB, cdn, user.UserName, r.Host, r.URL.Path, cfg.Version) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } +func Handler(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn"}, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, errCode, userErr, sysErr) + return + } + defer inf.Close() - resp := struct { - Response *tc.CRConfig `json:"response"` - }{crConfig} - respBts, err := json.Marshal(resp) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - log.Infof("CRConfig time to generate: %+v\n", time.Since(start)) - w.Header().Set("Content-Type", "application/json") - w.Write(respBts) + start := time.Now() + crConfig, err := Make(inf.Tx.Tx, inf.Params["cdn"], inf.User.UserName, r.Host, r.URL.Path, inf.Config.Version) + if err != nil { + api.HandleErr(w, r, http.StatusInternalServerError, nil, err) + return } + log.Infof("CRConfig time to generate: %+v\n", time.Since(start)) + api.WriteResp(w, r, crConfig) } // SnapshotGetHandler gets and serves the CRConfig from the snapshot table. -func SnapshotGetHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - handleErrs := tc.GetHandleErrorsFunc(w, r) - params, err := api.GetCombinedParams(r) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - cdn, ok := params["cdn"] - if !ok { - handleErrs(http.StatusInternalServerError, errors.New("params missing CDN")) - return - } +func SnapshotGetHandler(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn"}, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, errCode, userErr, sysErr) + return + } + defer inf.Close() - snapshot, cdnExists, err := GetSnapshot(db.DB, cdn) - if err != nil { - handleErrs(http.StatusInternalServerError, errors.New("getting snapshot: "+err.Error())) - return - } - if !cdnExists { - handleErrs(http.StatusNotFound, errors.New("CDN not found")) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"response":` + snapshot + `}`)) + snapshot, cdnExists,
[GitHub] rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts
rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts URL: https://github.com/apache/trafficcontrol/pull/2653#discussion_r209756497 ## File path: traffic_ops/traffic_ops_golang/monitoring/monitoring.go ## @@ -110,56 +86,70 @@ type Monitoring struct { Config map[string]interface{} `json:"config"` } -// MonitoringResponse ... type MonitoringResponse struct { Response Monitoring `json:"response"` } -// Router ... type Router struct { Typestring Profile string } -// DeliveryService ... type DeliveryService struct { XMLID string `json:"xmlId"` TotalTPSThreshold float64 `json:"totalTpsThreshold"` Status string `json:"status"` TotalKBPSThreshold float64 `json:"totalKbpsThreshold"` } -// TODO change to use the PathParams, instead of parsing the URL -func monitoringHandler(db *sqlx.DB) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - handleErrs := tc.GetHandleErrorsFunc(w, r) +func Get(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn"}, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, errCode, userErr, sysErr) + return + } + defer inf.Close() + *inf.CommitTx = true + api.RespWriter(w, r)(getMonitoringJSON(inf.Tx.Tx, inf.Params["cdn"])) Review comment: As below, `RespWriter` -> `WriteResp` -> `WriteRespRaw` -> `w.Header().Set("Content-Type"` `https://github.com/apache/trafficcontrol/blob/master/traffic_ops/traffic_ops_golang/api/api.go#L67` This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services
[GitHub] rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts
rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts URL: https://github.com/apache/trafficcontrol/pull/2653#discussion_r209756497 ## File path: traffic_ops/traffic_ops_golang/monitoring/monitoring.go ## @@ -110,56 +86,70 @@ type Monitoring struct { Config map[string]interface{} `json:"config"` } -// MonitoringResponse ... type MonitoringResponse struct { Response Monitoring `json:"response"` } -// Router ... type Router struct { Typestring Profile string } -// DeliveryService ... type DeliveryService struct { XMLID string `json:"xmlId"` TotalTPSThreshold float64 `json:"totalTpsThreshold"` Status string `json:"status"` TotalKBPSThreshold float64 `json:"totalKbpsThreshold"` } -// TODO change to use the PathParams, instead of parsing the URL -func monitoringHandler(db *sqlx.DB) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - handleErrs := tc.GetHandleErrorsFunc(w, r) +func Get(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn"}, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, errCode, userErr, sysErr) + return + } + defer inf.Close() + *inf.CommitTx = true + api.RespWriter(w, r)(getMonitoringJSON(inf.Tx.Tx, inf.Params["cdn"])) Review comment: As below, `RespWriter` -> `WriteResp` -> `WriteRespRaw` -> `w.Header().Set("Content-Type"` https://github.com/apache/trafficcontrol/blob/master/traffic_ops/traffic_ops_golang/api/api.go#L67 This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services
[GitHub] rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts
rob05c commented on a change in pull request #2653: Fix TO Go routes to all use transactions and timeouts URL: https://github.com/apache/trafficcontrol/pull/2653#discussion_r209756203 ## File path: traffic_ops/traffic_ops_golang/profileparameter/unassignedparams.go ## @@ -32,24 +32,24 @@ import ( // my $rs_data = $self->db->resultset("Parameter")->search( 'me.id' => { 'not in' => \@assigned_params } ); -func GetUnassigned(db *sql.DB) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - _, intParams, userErr, sysErr, errCode := api.AllParams(r, []string{"id"}, []string{"id"}) - if userErr != nil || sysErr != nil { - api.HandleErr(w, r, errCode, userErr, sysErr) - return - } - api.RespWriter(w, r)(getUnassignedParametersByProfileID(intParams["id"], db)) +func GetUnassigned(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"id"}, []string{"id"}) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, errCode, userErr, sysErr) + return } + defer inf.Close() + *inf.CommitTx = true + api.RespWriter(w, r)(getUnassignedParametersByProfileID(inf.Tx.Tx, inf.IntParams["id"])) Review comment: They do https://github.com/apache/trafficcontrol/blob/master/traffic_ops/traffic_ops_golang/api/api.go#L67 This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services