herald.go/main.go

59 lines
1.1 KiB
Go
Raw Normal View History

2021-03-18 21:12:48 +01:00
package main
2021-03-18 22:14:56 +01:00
import (
"context"
"log"
"net"
"os"
"time"
pb "github.com/lbryio/hub/protobuf/go"
"google.golang.org/grpc"
)
const (
port = ":50051"
)
2021-03-18 21:12:48 +01:00
func main() {
2021-03-18 22:14:56 +01:00
if len(os.Args) > 1 && os.Args[1] == "serve" {
l, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterHubServer(s, &server{})
log.Printf("listening on %s\n", l.Addr().String())
if err := s.Serve(l); err != nil {
log.Fatalf("failed to serve: %v", err)
}
return
}
conn, err := grpc.Dial("localhost"+port, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewHubClient(conn)
// Contact the server and print out its response.
query := "what is lbry"
if len(os.Args) > 1 {
query = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.Search(ctx, &pb.SearchRequest{Query: query})
if err != nil {
log.Fatal(err)
}
log.Printf("found %d results\n", r.GetTotal())
2021-03-18 21:12:48 +01:00
}