From 56f329492296e2f1156e578160c0241b0bd7984d Mon Sep 17 00:00:00 2001 From: Kevin Mackenzie Date: Fri, 6 Oct 2017 16:38:51 -0400 Subject: [PATCH] Added Route that accesses database There is no setup code to initialize this database, rather it is a simple example for how to do it. This is just an indicator of the learning process. --- traffic_ops/traffic_ops_golang/my_tests.go | 104 +++++++++++++++++++++ traffic_ops/traffic_ops_golang/routes.go | 1 + 2 files changed, 105 insertions(+) create mode 100644 traffic_ops/traffic_ops_golang/my_tests.go diff --git a/traffic_ops/traffic_ops_golang/my_tests.go b/traffic_ops/traffic_ops_golang/my_tests.go new file mode 100644 index 0000000000..fcce673645 --- /dev/null +++ b/traffic_ops/traffic_ops_golang/my_tests.go @@ -0,0 +1,104 @@ +package main; + +import ( + "fmt" + "net/http" + "database/sql" + "encoding/json" + "strconv" + + "github.com/apache/incubator-trafficcontrol/lib/go-log" + + "github.com/jmoiron/sqlx" +) + +const TestExtPrivLevel = PrivLevelReadOnly; + +type MyData struct { + ID string `json:"id"` + Name string `json:"name"` + Name1 string `json:"name1"` +} + +type MyDataResponse struct { + Response MyData `json:"response"` +} + +func testExtHandler(db *sqlx.DB) RegexHandlerFunc { + return func(w http.ResponseWriter, r *http.Request, p PathParams) { + handleErr := func(err error, status int) { + log.Errorf("%v %v\n", r.RemoteAddr, err) + w.WriteHeader(status) + fmt.Fprintf(w, http.StatusText(status)) + } + + serverID, err := strconv.ParseInt(p["id"], 10, 64) + if err != nil { + handleErr(err, http.StatusBadRequest) + //w.Header().Set("Content-Type", "application/json") + //fmt.Fprintf(w, "{\"result\": \"Bad Integer\"}") + return + } + + resp, err := getTestDataJson(serverID, db) + if err != nil { + handleErr(err, http.StatusInternalServerError) + //w.Header().Set("Content-Type", "application/json") + //fmt.Fprintf(w, "{\"result\": \"Error Querying Database\"}") + return + } + + respBts, err := json.Marshal(resp) + if err != nil { + handleErr(err, http.StatusInternalServerError) + //w.Header().Set("Content-Type", "application/json") + //fmt.Fprintf(w, "{\"result\": \"Error Marshalling Json\"}") + return + } + + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, "%s", respBts); + } +} + +func getTestData(db *sqlx.DB, serverID int64) (*MyData, error) { + query := `SELECT +me.ID as id, +me.NAME as name, +me.NAME1 as name1 +FROM my_data me +WHERE me.ID = $1` + + rows, err := db.Query(query, serverID) + if err != nil { + return nil, err + } + defer rows.Close() + + // TODO: what if there are zero rows? + rows.Next() + var id sql.NullString + var name sql.NullString + var name1 sql.NullString + + if err := rows.Scan(&id, &name, &name1); err != nil { + return nil, err + } + + ret := MyData{ + ID: id.String, + Name: name.String, + Name1: name1.String, + } + return &ret, nil +} + +func getTestDataJson(serverID int64, db * sqlx.DB) (*MyDataResponse, error) { + myData, err := getTestData(db, serverID) + if err != nil { + return nil, fmt.Errorf("error getting my data: %v", err) + } + + resp := MyDataResponse{ Response: *myData } + return &resp, nil +} diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go index 4bc1ef17b9..b4dc793b22 100644 --- a/traffic_ops/traffic_ops_golang/routes.go +++ b/traffic_ops/traffic_ops_golang/routes.go @@ -60,6 +60,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { {1.2, http.MethodGet, "parameters-wip.json$", wrapHeaders(wrapAuthWithData(parametersHandler(d.DB), d.Insecure, d.Secrets[0], rd.PrivLevelStmt, ParametersPrivLevel))}, {1.2, http.MethodGet, "system/info-wip$", wrapHeaders(wrapAuthWithData(systemInfoHandler(d.DB), d.Insecure, d.Secrets[0], rd.PrivLevelStmt, SystemInfoPrivLevel))}, {1.2, http.MethodGet, "system/info-wip.json$", wrapHeaders(wrapAuthWithData(systemInfoHandler(d.DB), d.Insecure, d.Secrets[0], rd.PrivLevelStmt, SystemInfoPrivLevel))}, + {1.2, http.MethodGet, "exp/{id}/data.json$", wrapHeaders(wrapAuth(testExtHandler(d.DB), d.Insecure, d.TOSecret, rd.PrivLevelStmt, TestExtPrivLevel))}, }, rootHandler(d), nil }