fixes some error handling, updates storj import links

This commit is contained in:
Natalie Ventura Villasana 2018-04-18 14:21:25 -04:00 committed by JT Olds
parent 8d8fd76748
commit d93dacb53e
4 changed files with 32 additions and 41 deletions

View File

@ -6,44 +6,40 @@ package main
import (
"log"
"net/http"
"os"
"github.com/julienschmidt/httprouter"
"github.com/zeebo/errs"
"go.uber.org/zap"
"github.com/storj/netstate/routes"
"github.com/storj/storage/boltdb"
)
var (
// Error is the default main errs class
Error = errs.Class("main err")
errLoggerFail = Error.New("zap logger failed to start")
"storj.io/storj/netstate/routes"
"storj.io/storj/storage/boltdb"
)
func main() {
err := Main()
if err != nil {
log.Fatalf("fatal error: %v", err)
os.Exit(1)
}
}
func Main() error {
logger, err := zap.NewDevelopment()
if err != nil {
log.Println(errLoggerFail)
return
return err
}
defer logger.Sync()
logger.Info("serving on localhost:3000")
bdb, err := boltdb.New("netstate.db")
if err != nil {
logger.Fatal("db error:",
zap.Error(boltdb.ErrInitDb),
zap.Error(err),
)
return
return err
}
defer bdb.Close()
file := routes.NewNetStateRoutes(bdb)
routes := routes.NewNetStateRoutes(bdb)
http.ListenAndServe(":3000", start(file))
return http.ListenAndServe(":3000", start(routes))
}
func start(f *routes.NetStateRoutes) *httprouter.Router {

View File

@ -8,18 +8,10 @@ import (
"fmt"
"log"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/zeebo/errs"
"github.com/storj/storage/boltdb"
)
var (
// Error is the default route errs class
Error = errs.Class("routes err")
errReadReq = Error.New("error reading request body")
"storj.io/storj/storage/boltdb"
)
type NetStateRoutes struct {
@ -54,6 +46,7 @@ func (n *NetStateRoutes) Put(w http.ResponseWriter, r *http.Request, ps httprout
if err := n.DB.Put(file); err != nil {
http.Error(w, "err saving file", http.StatusInternalServerError)
log.Println(err)
return
}
fmt.Fprintf(w, "PUT to %s\n", givenPath)
@ -64,7 +57,9 @@ func (n *NetStateRoutes) Get(w http.ResponseWriter, r *http.Request, ps httprout
fileInfo, err := n.DB.Get([]byte(fileKey))
if err != nil {
http.Error(w, "err getting file", http.StatusInternalServerError)
log.Println(err)
return
}
bytes, err := json.Marshal(fileInfo)
@ -85,8 +80,13 @@ func (n *NetStateRoutes) List(w http.ResponseWriter, r *http.Request, ps httprou
log.Println(err)
return
}
keyString := strings.Join(fileKeys, "")
_, err = w.Write([]byte(keyString))
bytes, err := json.Marshal(fileKeys)
if err != nil {
http.Error(w, "internal error: unable to marshal path list", http.StatusInternalServerError)
log.Println(err)
return
}
_, err = w.Write(bytes)
if err != nil {
log.Printf("failed writing response: %v", err)
}
@ -97,6 +97,7 @@ func (n *NetStateRoutes) Delete(w http.ResponseWriter, r *http.Request, ps httpr
if err := n.DB.Delete([]byte(fileKey)); err != nil {
http.Error(w, "internal error: unable to delete file", http.StatusInternalServerError)
log.Printf("err deleting file %v", err)
return
}
fmt.Fprintf(w, "Deleted file key: %s", fileKey)

View File

@ -11,9 +11,6 @@ import (
var (
defaultTimeout = 1 * time.Second
ErrDbOpen = Error.New("error boltdb failed to open")
ErrInitDb = Error.New("error instantiating boltdb")
)
// Client is the storage interface for the Bolt database
@ -26,7 +23,7 @@ type Client struct {
func New(path string) (*Client, error) {
db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: defaultTimeout})
if err != nil {
return nil, ErrDbOpen
return nil, err
}
return &Client{

View File

@ -21,17 +21,14 @@ const (
)
var (
errCreatingFileBucket = Error.New("error creating file bucket")
errFileNotFound = Error.New("error file not found")
errIterKeys = Error.New("error unable to iterate through bucket keys")
errDeletingFile = Error.New("error unable to delete file key")
errFileNotFound = Error.New("error file not found")
)
func (client *Client) Put(file File) error {
return client.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(fileBucketName))
if err != nil {
return errCreatingFileBucket
return err
}
fileKey := []byte(file.Path)
@ -71,7 +68,7 @@ func (client *Client) List() ([]string, error) {
return nil
})
if err != nil {
return errIterKeys
return err
}
return nil
})
@ -83,7 +80,7 @@ func (client *Client) Delete(fileKey []byte) error {
if err := client.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte(fileBucketName)).Delete(fileKey)
}); err != nil {
return errDeletingFile
return err
}
return nil
}