Skip to content

Commit

Permalink
return path from create-close
Browse files Browse the repository at this point in the history
cenkalti committed Feb 8, 2018
1 parent ced7d9a commit 5f0ab6b
Showing 3 changed files with 38 additions and 10 deletions.
14 changes: 14 additions & 0 deletions tracker.go
Original file line number Diff line number Diff line change
@@ -321,6 +321,16 @@ func (t *Tracker) createClose(w http.ResponseWriter, r *http.Request) {
t.internalServerError("cannot insert file_on record", err, r, w)
return
}
row = tx.QueryRow("select h.hostip, d.read_port "+
"from device d join host h on h.hostid=d.hostid "+
"where d.devid=?", devid)
var hostip string
var httpPort int64
err = row.Scan(&hostip, &httpPort)
if err != nil {
t.internalServerError("cannot select host ip", err, r, w)
return
}
err = tx.Commit()
if err != nil {
t.internalServerError("cannot commit transaction", err, r, w)
@@ -329,6 +339,10 @@ func (t *Tracker) createClose(w http.ResponseWriter, r *http.Request) {
if olddevids != nil {
go t.publishDeleteTask(olddevids, oldfid)
}
var response CreateClose
response.Path = fmt.Sprintf("http://%s:%d/dev%d/%s", hostip, httpPort, devid, vivify(fid))
encoder := json.NewEncoder(w)
encoder.Encode(response) // nolint: errcheck
}

func (t *Tracker) deleteFile(w http.ResponseWriter, r *http.Request) {
30 changes: 20 additions & 10 deletions tracker_test.go
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ func TestCreateClose(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = tr.db.Exec("insert into device(devid, status, hostid) values(2, 'alive', 1)")
_, err = tr.db.Exec("insert into device(devid, status, hostid, read_port) values(2, 'alive', 1, 5678)")
if err != nil {
t.Fatal(err)
}
@@ -140,10 +140,15 @@ func TestCreateClose(t *testing.T) {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expected := ""
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
var resp CreateClose
err = json.Unmarshal(rr.Body.Bytes(), &resp)
if err != nil {
t.Fatal(err)
}
expected := "http://1.2.3.4:5678/dev2/0/000/000/0000000009.fid"
if resp.Path != expected {
t.Errorf("handler returned unexpected path: got %v want %v",
resp.Path, expected)
}
}

@@ -170,7 +175,7 @@ func TestCreateCloseOverwrite(t *testing.T) {
t.Fatal(err)
}

_, err = tr.db.Exec("insert into device(devid, status, hostid) values(3, 'alive', 1)")
_, err = tr.db.Exec("insert into device(devid, status, hostid, read_port) values(3, 'alive', 1, 5678)")
if err != nil {
t.Fatal(err)
}
@@ -189,10 +194,15 @@ func TestCreateCloseOverwrite(t *testing.T) {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expected := ""
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
var resp CreateClose
err = json.Unmarshal(rr.Body.Bytes(), &resp)
if err != nil {
t.Fatal(err)
}
expected := "http://1.2.3.4:5678/dev3/0/000/000/0000000009.fid"
if resp.Path != expected {
t.Errorf("handler returned unexpected path: got %v want %v",
resp.Path, expected)
}
}

4 changes: 4 additions & 0 deletions trackerapi.go
Original file line number Diff line number Diff line change
@@ -10,6 +10,10 @@ type CreateOpen struct {
Fid int64 `json:"fid"`
}

type CreateClose struct {
Path string `json:"path"`
}

type GetDevices struct {
Devices []Device `json:"devices"`
}

0 comments on commit 5f0ab6b

Please sign in to comment.