herald.go/main.go

84 lines
1.5 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"
2021-04-19 21:25:34 +02:00
"fmt"
2021-03-18 22:14:56 +01:00
"time"
2022-04-14 02:25:37 +02:00
_ "net/http/pprof"
2022-04-29 16:46:33 +02:00
"github.com/lbryio/hub/internal"
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"
log "github.com/sirupsen/logrus"
2021-03-18 22:14:56 +01:00
"google.golang.org/grpc"
)
2021-03-18 21:12:48 +01:00
func main() {
2021-07-06 02:20:38 +02:00
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
ctx := context.Background()
searchRequest := &pb.SearchRequest{}
args := server.ParseArgs(searchRequest)
2021-07-06 02:20:38 +02:00
if args.CmdType == server.ServeCmd {
// This will cancel goroutines with the server finishes.
ctxWCancel, cancel := context.WithCancel(ctx)
defer cancel()
2022-03-07 21:25:56 +01:00
initsignals()
interrupt := interruptListener()
2021-11-25 00:56:34 +01:00
2022-03-07 21:25:56 +01:00
s := server.MakeHubServer(ctxWCancel, args)
go s.Run()
2022-02-20 14:03:27 +01:00
2022-03-07 21:25:56 +01:00
defer func() {
log.Println("Shutting down server...")
2022-02-20 14:03:27 +01:00
2022-03-07 21:25:56 +01:00
s.EsClient.Stop()
s.GrpcServer.GracefulStop()
s.DB.Shutdown()
2022-02-20 14:03:27 +01:00
2022-03-07 21:25:56 +01:00
log.Println("Returning from main...")
}()
2022-02-20 14:03:27 +01:00
2022-03-07 21:25:56 +01:00
<-interrupt
return
2021-03-18 22:14:56 +01:00
}
conn, err := grpc.Dial("localhost:"+args.Port,
grpc.WithInsecure(),
grpc.WithBlock(),
)
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)
ctxWTimeout, cancelQuery := context.WithTimeout(ctx, time.Second)
defer cancelQuery()
2021-03-18 22:14:56 +01:00
2021-07-06 02:20:38 +02:00
log.Println(args)
switch args.CmdType {
case server.SearchCmd:
r, err := c.Search(ctxWTimeout, searchRequest)
2021-07-06 02:20:38 +02:00
if err != nil {
log.Fatal(err)
}
2021-05-16 05:13:14 +02:00
2021-07-06 02:20:38 +02:00
log.Printf("found %d results\n", r.GetTotal())
2021-04-19 21:25:34 +02:00
2021-07-06 02:20:38 +02:00
for _, t := range r.Txos {
2022-04-29 16:46:33 +02:00
fmt.Printf("%s:%d\n", internal.TxHashToTxId(t.TxHash), t.Nout)
2021-07-06 02:20:38 +02:00
}
default:
log.Fatalln("Unknown Command Type!")
2021-04-19 21:25:34 +02:00
}
2021-03-18 21:12:48 +01:00
}