Skip to content

Commit

Permalink
Added Route that accesses database
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KevinMackenzie committed Oct 6, 2017
1 parent eab444b commit 56f3294
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
104 changes: 104 additions & 0 deletions traffic_ops/traffic_ops_golang/my_tests.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions traffic_ops/traffic_ops_golang/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 56f3294

Please sign in to comment.