2021-03-18 21:12:48 +01:00
|
|
|
package main
|
|
|
|
|
2021-03-18 22:14:56 +01:00
|
|
|
import (
|
|
|
|
"context"
|
2021-04-19 21:25:34 +02:00
|
|
|
"fmt"
|
2021-03-18 22:14:56 +01:00
|
|
|
"log"
|
|
|
|
"net"
|
2021-05-31 03:34:57 +02:00
|
|
|
"os"
|
2021-06-04 18:38:17 +02:00
|
|
|
"strings"
|
2021-03-18 22:14:56 +01:00
|
|
|
"time"
|
|
|
|
|
2021-05-31 03:34:57 +02:00
|
|
|
"github.com/akamensky/argparse"
|
2021-03-18 22:14:56 +01:00
|
|
|
pb "github.com/lbryio/hub/protobuf/go"
|
2021-04-19 21:25:34 +02:00
|
|
|
"github.com/lbryio/hub/server"
|
2021-06-28 19:54:49 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/util"
|
2021-03-18 22:14:56 +01:00
|
|
|
"google.golang.org/grpc"
|
2021-05-31 03:34:57 +02:00
|
|
|
"google.golang.org/grpc/reflection"
|
2021-03-18 22:14:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-06-10 02:04:06 +02:00
|
|
|
defaultHost = "0.0.0.0"
|
2021-05-25 02:09:28 +02:00
|
|
|
defaultPort = "50051"
|
2021-06-04 18:38:17 +02:00
|
|
|
defaultEsHost = "http://localhost"
|
|
|
|
defaultEsPort = "9200"
|
2021-03-18 22:14:56 +01:00
|
|
|
)
|
2021-03-18 21:12:48 +01:00
|
|
|
|
2021-05-25 02:09:28 +02:00
|
|
|
|
2021-06-10 02:04:06 +02:00
|
|
|
func GetEnvironment(data []string, getkeyval func(item string) (key, val string)) map[string]string {
|
|
|
|
items := make(map[string]string)
|
|
|
|
for _, item := range data {
|
|
|
|
key, val := getkeyval(item)
|
|
|
|
items[key] = val
|
|
|
|
}
|
|
|
|
return items
|
2021-05-25 02:09:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 02:04:06 +02:00
|
|
|
func GetEnvironmentStandard() map[string]string {
|
|
|
|
return GetEnvironment(os.Environ(), func(item string) (key, val string) {
|
2021-06-04 18:38:17 +02:00
|
|
|
splits := strings.Split(item, "=")
|
|
|
|
key = splits[0]
|
|
|
|
val = splits[1]
|
|
|
|
return
|
|
|
|
})
|
2021-06-10 02:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseArgs(searchRequest *pb.SearchRequest) *server.Args {
|
2021-06-04 18:38:17 +02:00
|
|
|
|
2021-06-10 02:04:06 +02:00
|
|
|
environment := GetEnvironmentStandard()
|
2021-05-31 03:34:57 +02:00
|
|
|
parser := argparse.NewParser("hub", "hub server and client")
|
|
|
|
|
|
|
|
serveCmd := parser.NewCommand("serve", "start the hub server")
|
|
|
|
|
2021-06-10 02:04:06 +02:00
|
|
|
host := parser.String("", "rpchost", &argparse.Options{Required: false, Help: "host", Default: defaultHost})
|
2021-05-31 03:34:57 +02:00
|
|
|
port := parser.String("", "rpcport", &argparse.Options{Required: false, Help: "port", Default: defaultPort})
|
2021-06-10 02:04:06 +02:00
|
|
|
esHost := parser.String("", "eshost", &argparse.Options{Required: false, Help: "host", Default: defaultEsHost})
|
|
|
|
esPort := parser.String("", "esport", &argparse.Options{Required: false, Help: "port", Default: defaultEsPort})
|
|
|
|
dev := parser.Flag("", "dev", &argparse.Options{Required: false, Help: "port", Default: false})
|
2021-05-31 03:34:57 +02:00
|
|
|
|
2021-05-31 20:53:08 +02:00
|
|
|
text := parser.String("", "text", &argparse.Options{Required: false, Help: "text query"})
|
2021-05-31 03:34:57 +02:00
|
|
|
name := parser.String("", "name", &argparse.Options{Required: false, Help: "name"})
|
|
|
|
claimType := parser.String("", "claim_type", &argparse.Options{Required: false, Help: "claim_type"})
|
|
|
|
id := parser.String("", "id", &argparse.Options{Required: false, Help: "id"})
|
|
|
|
author := parser.String("", "author", &argparse.Options{Required: false, Help: "author"})
|
|
|
|
title := parser.String("", "title", &argparse.Options{Required: false, Help: "title"})
|
|
|
|
description := parser.String("", "description", &argparse.Options{Required: false, Help: "description"})
|
|
|
|
channelId := parser.String("", "channel_id", &argparse.Options{Required: false, Help: "channel id"})
|
|
|
|
channelIds := parser.StringList("", "channel_ids", &argparse.Options{Required: false, Help: "channel ids"})
|
|
|
|
|
|
|
|
// Now parse the arguments
|
|
|
|
err := parser.Parse(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(parser.Usage(err))
|
|
|
|
}
|
2021-05-16 05:13:14 +02:00
|
|
|
|
2021-06-04 18:38:17 +02:00
|
|
|
|
|
|
|
args := &server.Args{
|
|
|
|
Serve: false,
|
2021-06-10 02:04:06 +02:00
|
|
|
Host: *host,
|
2021-06-04 18:38:17 +02:00
|
|
|
Port: ":" + *port,
|
2021-06-10 02:04:06 +02:00
|
|
|
EsHost: *esHost,
|
|
|
|
EsPort: *esPort,
|
|
|
|
Dev: *dev,
|
2021-06-04 18:38:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if esHost, ok := environment["ELASTIC_HOST"]; ok {
|
|
|
|
args.EsHost = esHost
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:38:27 +02:00
|
|
|
if !strings.HasPrefix(args.EsHost, "http") {
|
|
|
|
args.EsHost = "http://" + args.EsHost
|
|
|
|
}
|
|
|
|
|
2021-06-04 18:38:17 +02:00
|
|
|
if esPort, ok := environment["ELASTIC_PORT"]; ok {
|
|
|
|
args.EsPort = esPort
|
|
|
|
}
|
2021-05-25 05:28:43 +02:00
|
|
|
|
2021-05-31 03:34:57 +02:00
|
|
|
/*
|
|
|
|
Verify no invalid argument combinations
|
|
|
|
*/
|
|
|
|
if len(*channelIds) > 0 && *channelId != "" {
|
|
|
|
log.Fatal("Cannot specify both channel_id and channel_ids")
|
|
|
|
}
|
|
|
|
|
|
|
|
if serveCmd.Happened() {
|
2021-05-25 05:28:43 +02:00
|
|
|
args.Serve = true
|
|
|
|
}
|
2021-05-31 03:34:57 +02:00
|
|
|
|
2021-05-31 20:53:08 +02:00
|
|
|
if *text != "" {
|
|
|
|
searchRequest.Text = *text
|
2021-05-16 05:13:14 +02:00
|
|
|
}
|
2021-05-25 05:28:43 +02:00
|
|
|
if *name!= "" {
|
|
|
|
searchRequest.Name = []string{*name}
|
|
|
|
}
|
2021-05-16 05:13:14 +02:00
|
|
|
if *claimType != "" {
|
|
|
|
searchRequest.ClaimType = []string{*claimType}
|
|
|
|
}
|
|
|
|
if *id != "" {
|
|
|
|
searchRequest.XId = [][]byte{[]byte(*id)}
|
|
|
|
}
|
|
|
|
if *author != "" {
|
|
|
|
searchRequest.Author = []string{*author}
|
|
|
|
}
|
|
|
|
if *title != "" {
|
|
|
|
searchRequest.Title = []string{*title}
|
|
|
|
}
|
2021-05-18 12:02:55 +02:00
|
|
|
if *description != "" {
|
|
|
|
searchRequest.Description = []string{*description}
|
|
|
|
}
|
2021-05-31 03:34:57 +02:00
|
|
|
if *channelId != "" {
|
|
|
|
searchRequest.ChannelId = &pb.InvertibleField{Invert: false, Value: []string{*channelId}}
|
|
|
|
}
|
|
|
|
if len(*channelIds) > 0 {
|
|
|
|
searchRequest.ChannelId = &pb.InvertibleField{Invert: false, Value: *channelIds}
|
|
|
|
}
|
2021-05-25 02:09:28 +02:00
|
|
|
|
2021-05-25 05:28:43 +02:00
|
|
|
return args
|
2021-05-25 02:09:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-18 21:12:48 +01:00
|
|
|
func main() {
|
2021-05-25 05:28:43 +02:00
|
|
|
searchRequest := &pb.SearchRequest{}
|
|
|
|
|
|
|
|
args := parseArgs(searchRequest)
|
|
|
|
|
|
|
|
if args.Serve {
|
2021-05-25 02:09:28 +02:00
|
|
|
|
|
|
|
l, err := net.Listen("tcp", args.Port)
|
2021-03-18 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to listen: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-05-25 02:09:28 +02:00
|
|
|
s := server.MakeHubServer(args)
|
2021-06-01 04:19:10 +02:00
|
|
|
pb.RegisterHubServer(s.GrpcServer, s)
|
|
|
|
reflection.Register(s.GrpcServer)
|
2021-03-18 22:14:56 +01:00
|
|
|
|
|
|
|
log.Printf("listening on %s\n", l.Addr().String())
|
2021-06-17 01:42:11 +02:00
|
|
|
log.Println(s.Args)
|
2021-06-01 04:19:10 +02:00
|
|
|
if err := s.GrpcServer.Serve(l); err != nil {
|
2021-03-18 22:14:56 +01:00
|
|
|
log.Fatalf("failed to serve: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-25 02:09:28 +02:00
|
|
|
conn, err := grpc.Dial("localhost"+args.Port,
|
|
|
|
grpc.WithInsecure(),
|
2021-05-31 03:34:57 +02:00
|
|
|
grpc.WithBlock(),
|
2021-05-25 02:09:28 +02:00
|
|
|
)
|
2021-03-18 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("did not connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
c := pb.NewHubClient(conn)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-05-16 05:13:14 +02:00
|
|
|
|
2021-05-13 22:06:19 +02:00
|
|
|
r, err := c.Search(ctx, searchRequest)
|
2021-03-18 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("found %d results\n", r.GetTotal())
|
2021-04-19 21:25:34 +02:00
|
|
|
|
|
|
|
for _, t := range r.Txos {
|
2021-06-28 19:54:49 +02:00
|
|
|
fmt.Printf("%s:%d\n", util.TxHashToTxId(t.TxHash), t.Nout)
|
2021-04-19 21:25:34 +02:00
|
|
|
}
|
2021-03-18 21:12:48 +01:00
|
|
|
}
|