storj/examples/netstate-client/main.go
Natalie Villasana 1a06c2b0c7 adding grpc interface to network state (#33)
* adds proto files for netstate crud

* moves netstate grpc client lib into pkg/netstate where grpc netstate service is defined

* starts adding grpc client and server tests

* moves creation of grpc server into cmd/netstate/main.go, removes pkg/netstate/service.go, adds more client testing

* changed all 'Path' and 'Value' fields from strings to bytes, updated tests

* changes Get and Delete in proto file to receive 'requests' instead of 'file paths', adds tests for Get, List, and Delete

* changes netstate-routes to get 'fileValue' bytes not 'fileInfo'

* adds example rpc client in 'examples' and adds more specific debug logs

* adds readmes for netstate rpc services and updates netstate-routes
2018-05-14 18:31:26 -06:00

113 lines
2.5 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"flag"
"strings"
"go.uber.org/zap"
"google.golang.org/grpc"
proto "storj.io/storj/protos/netstate"
)
var (
port string
)
const (
success string = "success"
)
func initializeFlags() {
flag.StringVar(&port, "port", ":8080", "port")
flag.Parse()
}
func main() {
initializeFlags()
logger, _ := zap.NewDevelopment()
defer logger.Sync()
conn, err := grpc.Dial(port, grpc.WithInsecure())
if err != nil {
logger.Error("Failed to dial: ", zap.Error(err))
}
client := proto.NewNetStateClient(conn)
ctx := context.Background()
// Examples file paths to be saved
fp := proto.FilePath{
Path: []byte("welcome/to/my/file/journey"),
SmallValue: []byte("granola"),
}
fp2 := proto.FilePath{
Path: []byte("so/many/file/paths"),
SmallValue: []byte("m&ms"),
}
fp3 := proto.FilePath{
Path: []byte("another/file/path/for/the/pile"),
SmallValue: []byte("popcorn"),
}
// Example Puts
putRes, err := client.Put(ctx, &fp)
if err != nil || putRes.Confirmation != success {
logger.Error("failed to put", zap.Error(err))
}
putRes2, err := client.Put(ctx, &fp2)
if err != nil || putRes2.Confirmation != success {
logger.Error("failed to put", zap.Error(err))
}
putRes3, err := client.Put(ctx, &fp3)
if err != nil || putRes3.Confirmation != success {
logger.Error("failed to put", zap.Error(err))
}
// Example Get
getReq := proto.GetRequest{
Path: []byte("so/many/file/paths"),
}
getRes, err := client.Get(ctx, &getReq)
if err != nil {
logger.Error("failed to get", zap.Error(err))
}
value := string(getRes.SmallValue)
logger.Debug("get response: " + value)
// Example List
listReq := proto.ListRequest{
// This Bucket value isn't actually used by List() now,
// but in the future could be used to select specific
// buckets to list from.
Bucket: []byte("files"),
}
listRes, err := client.List(ctx, &listReq)
if err != nil {
logger.Error("failed to list file paths")
}
var stringList []string
for _, pathByte := range listRes.Filepaths {
stringList = append(stringList, string(pathByte))
}
logger.Debug("listed paths: " + strings.Join(stringList, ", "))
// Example Delete
delReq := proto.DeleteRequest{
Path: []byte("welcome/to/my/file/journey"),
}
delRes, err := client.Delete(ctx, &delReq)
if err != nil {
logger.Error("failed to delete: 'welcome/to/my/file/journey'")
}
if delRes.Confirmation == "success" {
logger.Debug("deleted: welcome/to/my/file/journey")
}
}