Commit 9b4531c9 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

move tags endpoints to debug api (#291)

parent 1f29b19b
...@@ -6,5 +6,4 @@ package api ...@@ -6,5 +6,4 @@ package api
type ( type (
BytesPostResponse = bytesPostResponse BytesPostResponse = bytesPostResponse
TagResponse = tagResponse
) )
...@@ -56,14 +56,6 @@ func (s *server) setupRouting() { ...@@ -56,14 +56,6 @@ func (s *server) setupRouting() {
"POST": http.HandlerFunc(s.chunkUploadHandler), "POST": http.HandlerFunc(s.chunkUploadHandler),
}) })
router.Handle("/bzz-tag/name/{name}", jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.CreateTag),
})
router.Handle("/bzz-tag/uuid/{uuid}", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getTagInfoUsingUUid),
})
s.Handler = web.ChainHandlers( s.Handler = web.ChainHandlers(
logging.NewHTTPAccessLogHandler(s.Logger, logrus.InfoLevel, "api access"), logging.NewHTTPAccessLogHandler(s.Logger, logrus.InfoLevel, "api access"),
handlers.CompressHandler, handlers.CompressHandler,
......
...@@ -12,4 +12,5 @@ type ( ...@@ -12,4 +12,5 @@ type (
AddressesResponse = addressesResponse AddressesResponse = addressesResponse
PinnedChunk = pinnedChunk PinnedChunk = pinnedChunk
ListPinnedChunksResponse = listPinnedChunksResponse ListPinnedChunksResponse = listPinnedChunksResponse
TagResponse = tagResponse
) )
...@@ -77,6 +77,12 @@ func (s *server) setupRouting() { ...@@ -77,6 +77,12 @@ func (s *server) setupRouting() {
router.Handle("/chunks-pin", jsonhttp.MethodHandler{ router.Handle("/chunks-pin", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.listPinnedChunks), "GET": http.HandlerFunc(s.listPinnedChunks),
}) })
router.Handle("/tags", jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.createTag),
})
router.Handle("/tags/{uid}", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getTag),
})
router.Handle("/topology", jsonhttp.MethodHandler{ router.Handle("/topology", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.topologyHandler), "GET": http.HandlerFunc(s.topologyHandler),
}) })
......
...@@ -2,9 +2,12 @@ ...@@ -2,9 +2,12 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package api package debugapi
import ( import (
crand "crypto/rand"
"errors"
"fmt"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
...@@ -45,13 +48,25 @@ func newTagResponse(tag *tags.Tag) tagResponse { ...@@ -45,13 +48,25 @@ func newTagResponse(tag *tags.Tag) tagResponse {
StartedAt: tag.StartedAt, StartedAt: tag.StartedAt,
} }
} }
func (s *server) CreateTag(w http.ResponseWriter, r *http.Request) {
tagName := mux.Vars(r)["name"]
tag, err := s.Tags.Create(tagName, 0, false) func (s *server) createTag(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
if name == "" {
b := make([]byte, 4)
_, err := crand.Read(b)
if err != nil {
s.Logger.Debugf("create tag: read random bytes %v", err)
s.Logger.Errorf("create tag: read random bytes error")
jsonhttp.InternalServerError(w, nil)
return
}
name = fmt.Sprintf("tag-%v-%x", time.Now().UnixNano(), b)
}
tag, err := s.Tags.Create(name, 0, false)
if err != nil { if err != nil {
s.Logger.Debugf("bzz-chunk: tag create error: %v", err) s.Logger.Debugf("create tag: %s %v", name, err)
s.Logger.Error("bzz-chunk: tag create error") s.Logger.Errorf("create tag: %s error", name)
jsonhttp.InternalServerError(w, "cannot create tag") jsonhttp.InternalServerError(w, "cannot create tag")
return return
} }
...@@ -60,22 +75,28 @@ func (s *server) CreateTag(w http.ResponseWriter, r *http.Request) { ...@@ -60,22 +75,28 @@ func (s *server) CreateTag(w http.ResponseWriter, r *http.Request) {
} }
func (s *server) getTagInfoUsingUUid(w http.ResponseWriter, r *http.Request) { func (s *server) getTag(w http.ResponseWriter, r *http.Request) {
uidStr := mux.Vars(r)["uuid"] uidStr := mux.Vars(r)["uid"]
uuid, err := strconv.ParseUint(uidStr, 10, 32) uid, err := strconv.ParseUint(uidStr, 10, 32)
if err != nil { if err != nil {
s.Logger.Debugf("bzz-tag: parse uid %s: %v", uidStr, err) s.Logger.Debugf("get tag: parse uid %s: %v", uidStr, err)
s.Logger.Error("bzz-tag: parse uid") s.Logger.Error("get tag: parse uid")
jsonhttp.BadRequest(w, "invalid uid") jsonhttp.BadRequest(w, "invalid uid")
return return
} }
tag, err := s.Tags.Get(uint32(uuid)) tag, err := s.Tags.Get(uint32(uid))
if err != nil { if err != nil {
s.Logger.Debugf("bzz-tag: tag not present : %v, uuid %s", err, uidStr) if errors.Is(err, tags.ErrNotFound) {
s.Logger.Error("bzz-tag: tag not present") s.Logger.Debugf("get tag: tag %v not present: %v", uid, err)
jsonhttp.InternalServerError(w, "tag not present") s.Logger.Warningf("get tag: tag %v not present", uid)
jsonhttp.NotFound(w, "tag not present")
return
}
s.Logger.Debugf("get tag: tag %v: %v", uid, err)
s.Logger.Errorf("get tag: %v", uid)
jsonhttp.InternalServerError(w, nil)
return return
} }
......
...@@ -30,10 +30,9 @@ import ( ...@@ -30,10 +30,9 @@ import (
) )
var ( var (
errExists = errors.New("already exists") errExists = errors.New("already exists")
errNA = errors.New("not available yet") errNA = errors.New("not available yet")
errNoETA = errors.New("unable to calculate ETA") errNoETA = errors.New("unable to calculate ETA")
errTagNotFound = errors.New("tag not found")
) )
// State is the enum type for chunk states // State is the enum type for chunk states
......
...@@ -31,8 +31,8 @@ import ( ...@@ -31,8 +31,8 @@ import (
) )
var ( var (
TagUidFunc = rand.Uint32 TagUidFunc = rand.Uint32
TagNotFoundErr = errors.New("tag not found") ErrNotFound = errors.New("tag not found")
) )
// Tags hold tag information indexed by a unique random uint32 // Tags hold tag information indexed by a unique random uint32
...@@ -75,7 +75,7 @@ func (ts *Tags) All() (t []*Tag) { ...@@ -75,7 +75,7 @@ func (ts *Tags) All() (t []*Tag) {
func (ts *Tags) Get(uid uint32) (*Tag, error) { func (ts *Tags) Get(uid uint32) (*Tag, error) {
t, ok := ts.tags.Load(uid) t, ok := ts.tags.Load(uid)
if !ok { if !ok {
return nil, TagNotFoundErr return nil, ErrNotFound
} }
return t.(*Tag), nil return t.(*Tag), nil
} }
...@@ -94,7 +94,7 @@ func (ts *Tags) GetByAddress(address swarm.Address) (*Tag, error) { ...@@ -94,7 +94,7 @@ func (ts *Tags) GetByAddress(address swarm.Address) (*Tag, error) {
}) })
if t == nil { if t == nil {
return nil, errTagNotFound return nil, ErrNotFound
} }
return t, nil return t, nil
} }
...@@ -104,7 +104,7 @@ func (ts *Tags) GetFromContext(ctx context.Context) (*Tag, error) { ...@@ -104,7 +104,7 @@ func (ts *Tags) GetFromContext(ctx context.Context) (*Tag, error) {
uid := sctx.GetTag(ctx) uid := sctx.GetTag(ctx)
t, ok := ts.tags.Load(uid) t, ok := ts.tags.Load(uid)
if !ok { if !ok {
return nil, errTagNotFound return nil, ErrNotFound
} }
return t.(*Tag), nil return t.(*Tag), nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment