Skip to content

Commit

Permalink
feat(api.FSI): initial FSI api methods
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 authored and dustmop committed Jul 15, 2019
1 parent 57b665d commit 63e57a3
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ func NewServerRoutes(s Server) *http.ServeMux {
m.Handle("/update/logs/file", s.middleware(uh.LogFileHandler))
m.Handle("/update/service", s.middleware(uh.ServiceHandler))

fsih := NewFSIHandlers(s.Instance, cfg.API.ReadOnly)
m.Handle("/dsstatus", s.middleware(fsih.StatusHandler))

renderh := NewRenderHandlers(node.Repo)
m.Handle("/render/", s.middleware(renderh.RenderHandler))

Expand Down
59 changes: 59 additions & 0 deletions api/fsi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package api

import (
"fmt"
"net/http"

util "github.com/qri-io/apiutil"
"github.com/qri-io/qri/lib"
)


// FSIHandlers connects HTTP requests to the FSI subsystem
type FSIHandlers struct {
lib.FSIMethods
ReadOnly bool
}

// NewFSIHandlers creates handlers that talk to qri's filesystem integration
func NewFSIHandlers(inst *lib.Instance, readOnly bool) FSIHandlers {
return FSIHandlers{
FSIMethods: *lib.NewFSIMethods(inst),
ReadOnly: readOnly,
}
}


// StatusHandler is the endpoint for getting the status of a linked dataset
func (h *FSIHandlers) StatusHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
util.EmptyOkHandler(w, r)
case "GET":
if h.ReadOnly {
readOnlyResponse(w, "/status")
return
}
h.statusHandler(w, r)
case "POST":
h.statusHandler(w, r)
default:
util.NotFoundHandler(w, r)
}
}

func (h *FSIHandlers) statusHandler(w http.ResponseWriter, r *http.Request) {
ref, err := DatasetRefFromPath(r.URL.Path[len("/dsstatus"):])
if err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, fmt.Errorf("bad reference: %s", err.Error()))
return
}

alias := ref.AliasString()
res := []lib.StatusItem{}
if err = h.AliasStatus(&alias, &res); err != nil {
util.WriteErrResponse(w, http.StatusInternalServerError, fmt.Errorf("error getting status: %s", err.Error()))
return
}
util.WriteResponse(w, res)
}
3 changes: 3 additions & 0 deletions fsi/fsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ func (fsi *FSI) load() (links, error) {

data, err := ioutil.ReadFile(fsi.linksPath)
if err != nil {
if os.IsNotExist(err) {
return links{}, nil
}
return nil, err
}

Expand Down
16 changes: 16 additions & 0 deletions fsi/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ type StatusItem struct {
Message string
}

// AliasStatus returns the status for a given dataset alias
func (fsi *FSI) AliasStatus(alias string) (changes []StatusItem, err error) {
links, err := fsi.load()
if err != nil {
return nil, err
}

for _, l := range links {
if l.Alias == alias {
return fsi.Status(l.Path)
}
}

return nil, fmt.Errorf("alias not found: %s", alias)
}

// Status reads the diff status from the current working directory
func (fsi *FSI) Status(dir string) (changes []StatusItem, err error) {
refStr, ok := GetLinkedFilesysRef(dir)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ github.com/libp2p/go-libp2p-net v0.0.0-20190226201932-e71fff5ba6e9/go.mod h1:8W6
github.com/libp2p/go-libp2p-net v0.0.1/go.mod h1:Yt3zgmlsHOgUWSXmt5V/Jpz9upuJBE8EgNU9DrCcR8c=
github.com/libp2p/go-libp2p-net v0.0.2 h1:qP06u4TYXfl7uW/hzqPhlVVTSA2nw1B/bHBJaUnbh6M=
github.com/libp2p/go-libp2p-net v0.0.2/go.mod h1:Yt3zgmlsHOgUWSXmt5V/Jpz9upuJBE8EgNU9DrCcR8c=
github.com/libp2p/go-libp2p-netutil v0.0.1 h1:LgD6+skofkOx8z6odD9+MZHKjupv3ng1u6KRhaADTnA=
github.com/libp2p/go-libp2p-netutil v0.0.1/go.mod h1:GdusFvujWZI9Vt0X5BKqwWWmZFxecf9Gt03cKxm2f/Q=
github.com/libp2p/go-libp2p-peer v0.0.1/go.mod h1:nXQvOBbwVqoP+T5Y5nCjeH4sP9IX/J0AMzcDUVruVoo=
github.com/libp2p/go-libp2p-peer v0.1.1 h1:qGCWD1a+PyZcna6htMPo26jAtqirVnJ5NvBQIKV7rRY=
Expand Down Expand Up @@ -468,6 +469,7 @@ github.com/libp2p/go-tcp-transport v0.0.1/go.mod h1:mnjg0o0O5TmXUaUIanYPUqkW4+u6
github.com/libp2p/go-tcp-transport v0.0.3/go.mod h1:f11C2zvCaGDkE8aFPUKmuYZwd3pP6HI24LeLMWhJnkQ=
github.com/libp2p/go-tcp-transport v0.0.4 h1:2iRu994wCT/iEz62F+c60FUoSkijNEQ0q2Itc+79XlQ=
github.com/libp2p/go-tcp-transport v0.0.4/go.mod h1:+E8HvC8ezEVOxIo3V5vCK9l1y/19K427vCzQ+xHKH/o=
github.com/libp2p/go-testutil v0.0.1 h1:Xg+O0G2HIMfHqBOBDcMS1iSZJ3GEcId4qOxCQvsGZHk=
github.com/libp2p/go-testutil v0.0.1/go.mod h1:iAcJc/DKJQanJ5ws2V+u5ywdL2n12X1WbbEG+Jjy69I=
github.com/libp2p/go-ws-transport v0.0.1/go.mod h1:p3bKjDWHEgtuKKj+2OdPYs5dAPIjtpQGHF2tJfGz7Ww=
github.com/libp2p/go-ws-transport v0.0.3/go.mod h1:iU0kzfMcO4tBVIk3z+7srp1YG/RFLWTSuO4enpivw8g=
Expand Down
13 changes: 13 additions & 0 deletions lib/fsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,16 @@ func (m *FSIMethods) Status(dir *string, res *[]StatusItem) (err error) {
*res, err = fsint.Status(*dir)
return err
}


// AlisStatus checks for any modifications or errors in a dataset alias
func (m *FSIMethods) AliasStatus(alias *string, res *[]StatusItem) (err error) {
if m.inst.rpc != nil {
return m.inst.rpc.Call("FSIMethods.AliasStatus", alias, res)
}

// TODO (b5) - inst should have an fsi instance
fsint := fsi.NewFSI(m.inst.repo, fsi.RepoPath(m.inst.repoPath))
*res, err = fsint.AliasStatus(*alias)
return err
}

0 comments on commit 63e57a3

Please sign in to comment.