2021-03-18 16:12:48 -04:00
package main
2021-03-18 17:14:56 -04:00
import (
"context"
2021-04-19 15:25:34 -04:00
"fmt"
2021-03-18 17:14:56 -04:00
"log"
"net"
2021-05-30 21:34:57 -04:00
"os"
2021-06-04 12:38:17 -04:00
"strings"
2021-03-18 17:14:56 -04:00
"time"
2021-05-30 21:34:57 -04:00
"github.com/akamensky/argparse"
2021-03-18 17:14:56 -04:00
pb "github.com/lbryio/hub/protobuf/go"
2021-04-19 15:25:34 -04:00
"github.com/lbryio/hub/server"
2021-06-28 13:54:49 -04:00
"github.com/lbryio/lbry.go/v2/extras/util"
2021-03-18 17:14:56 -04:00
"google.golang.org/grpc"
2021-05-30 21:34:57 -04:00
"google.golang.org/grpc/reflection"
2021-03-18 17:14:56 -04:00
)
const (
2021-10-04 17:58:27 -04:00
defaultHost = "0.0.0.0"
defaultPort = "50051"
defaultEsHost = "http://localhost"
defaultEsIndex = "claims"
defaultEsPort = "9200"
defaultRefreshDelta = 5
defaultCacheTTL = 5
2021-03-18 17:14:56 -04:00
)
2021-03-18 16:12:48 -04:00
2021-06-09 20:04:06 -04: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-24 20:09:28 -04:00
}
2021-06-09 20:04:06 -04:00
func GetEnvironmentStandard ( ) map [ string ] string {
return GetEnvironment ( os . Environ ( ) , func ( item string ) ( key , val string ) {
2021-06-04 12:38:17 -04:00
splits := strings . Split ( item , "=" )
key = splits [ 0 ]
val = splits [ 1 ]
return
} )
2021-06-09 20:04:06 -04:00
}
2021-07-05 20:20:38 -04:00
/ *
func makeServeCmd ( parser * argparse . Parser ) * argparse . Command {
serveCmd := parser . NewCommand ( "serve" , "start the hub server" )
host := serveCmd . String ( "" , "rpchost" , & argparse . Options { Required : false , Help : "host" , Default : defaultHost } )
port := serveCmd . String ( "" , "rpcport" , & argparse . Options { Required : false , Help : "port" , Default : defaultPort } )
esHost := serveCmd . String ( "" , "eshost" , & argparse . Options { Required : false , Help : "host" , Default : defaultEsHost } )
esPort := serveCmd . String ( "" , "esport" , & argparse . Options { Required : false , Help : "port" , Default : defaultEsPort } )
dev := serveCmd . Flag ( "" , "dev" , & argparse . Options { Required : false , Help : "port" , Default : false } )
return serveCmd
}
2021-09-21 14:02:09 -04:00
* /
2021-07-05 20:20:38 -04:00
2021-09-18 13:21:32 -04:00
func parseArgs ( searchRequest * pb . SearchRequest ) * server . Args {
2021-06-04 12:38:17 -04:00
2021-06-09 20:04:06 -04:00
environment := GetEnvironmentStandard ( )
2021-05-30 21:34:57 -04:00
parser := argparse . NewParser ( "hub" , "hub server and client" )
serveCmd := parser . NewCommand ( "serve" , "start the hub server" )
2021-07-05 20:20:38 -04:00
searchCmd := parser . NewCommand ( "search" , "claim search" )
2021-08-13 18:36:43 -03:00
debug := parser . Flag ( "" , "debug" , & argparse . Options { Required : false , Help : "enable debug logging" , Default : false } )
2021-05-30 21:34:57 -04:00
2021-08-11 01:46:21 -03:00
host := parser . String ( "" , "rpchost" , & argparse . Options { Required : false , Help : "RPC host" , Default : defaultHost } )
port := parser . String ( "" , "rpcport" , & argparse . Options { Required : false , Help : "RPC port" , Default : defaultPort } )
esHost := parser . String ( "" , "eshost" , & argparse . Options { Required : false , Help : "elasticsearch host" , Default : defaultEsHost } )
esPort := parser . String ( "" , "esport" , & argparse . Options { Required : false , Help : "elasticsearch port" , Default : defaultEsPort } )
esIndex := parser . String ( "" , "esindex" , & argparse . Options { Required : false , Help : "elasticsearch index name" , Default : defaultEsIndex } )
2021-10-04 17:58:27 -04:00
refreshDelta := parser . Int ( "" , "refresh-delta" , & argparse . Options { Required : false , Help : "elasticsearch index refresh delta in seconds" , Default : defaultRefreshDelta } )
cacheTTL := parser . Int ( "" , "cachettl" , & argparse . Options { Required : false , Help : "Cache TTL in minutes" , Default : defaultCacheTTL } )
2021-05-30 21:34:57 -04:00
2021-05-31 14:53:08 -04:00
text := parser . String ( "" , "text" , & argparse . Options { Required : false , Help : "text query" } )
2021-05-30 21:34:57 -04: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-15 23:13:14 -04:00
2021-06-04 12:38:17 -04:00
args := & server . Args {
2021-10-04 17:58:27 -04:00
CmdType : server . SearchCmd ,
Host : * host ,
Port : ":" + * port ,
EsHost : * esHost ,
EsPort : * esPort ,
EsIndex : * esIndex ,
Debug : * debug ,
RefreshDelta : * refreshDelta ,
CacheTTL : * cacheTTL ,
2021-06-04 12:38:17 -04:00
}
if esHost , ok := environment [ "ELASTIC_HOST" ] ; ok {
args . EsHost = esHost
}
2021-06-17 11:38:27 -04:00
if ! strings . HasPrefix ( args . EsHost , "http" ) {
args . EsHost = "http://" + args . EsHost
}
2021-06-04 12:38:17 -04:00
if esPort , ok := environment [ "ELASTIC_PORT" ] ; ok {
args . EsPort = esPort
}
2021-05-24 23:28:43 -04:00
2021-05-30 21:34:57 -04:00
/ *
2021-08-11 00:39:37 -03:00
Verify no invalid argument combinations
* /
2021-05-30 21:34:57 -04:00
if len ( * channelIds ) > 0 && * channelId != "" {
log . Fatal ( "Cannot specify both channel_id and channel_ids" )
}
if serveCmd . Happened ( ) {
2021-07-05 20:20:38 -04:00
args . CmdType = server . ServeCmd
} else if searchCmd . Happened ( ) {
args . CmdType = server . SearchCmd
2021-05-24 23:28:43 -04:00
}
2021-05-30 21:34:57 -04:00
2021-05-31 14:53:08 -04:00
if * text != "" {
searchRequest . Text = * text
2021-05-15 23:13:14 -04:00
}
2021-08-11 00:39:37 -03:00
if * name != "" {
2021-08-12 04:19:25 -03:00
searchRequest . ClaimName = * name
2021-05-24 23:28:43 -04:00
}
2021-05-15 23:13:14 -04:00
if * claimType != "" {
searchRequest . ClaimType = [ ] string { * claimType }
}
if * id != "" {
2021-08-12 04:19:25 -03:00
searchRequest . ClaimId = & pb . InvertibleField { Invert : false , Value : [ ] string { * id } }
2021-05-15 23:13:14 -04:00
}
if * author != "" {
2021-08-12 04:19:25 -03:00
searchRequest . Author = * author
2021-05-15 23:13:14 -04:00
}
if * title != "" {
2021-08-12 04:19:25 -03:00
searchRequest . Title = * title
2021-05-15 23:13:14 -04:00
}
2021-05-18 06:02:55 -04:00
if * description != "" {
2021-08-12 04:19:25 -03:00
searchRequest . Description = * description
2021-05-18 06:02:55 -04:00
}
2021-05-30 21:34:57 -04: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-24 20:09:28 -04:00
2021-05-24 23:28:43 -04:00
return args
2021-05-24 20:09:28 -04:00
}
2021-03-18 16:12:48 -04:00
func main ( ) {
2021-07-05 20:20:38 -04:00
2021-05-24 23:28:43 -04:00
searchRequest := & pb . SearchRequest { }
2021-09-18 13:21:32 -04:00
args := parseArgs ( searchRequest )
2021-05-24 23:28:43 -04:00
2021-07-05 20:20:38 -04:00
if args . CmdType == server . ServeCmd {
2021-05-24 20:09:28 -04:00
l , err := net . Listen ( "tcp" , args . Port )
2021-03-18 17:14:56 -04:00
if err != nil {
log . Fatalf ( "failed to listen: %v" , err )
}
2021-05-24 20:09:28 -04:00
s := server . MakeHubServer ( args )
2021-05-31 22:19:10 -04:00
pb . RegisterHubServer ( s . GrpcServer , s )
reflection . Register ( s . GrpcServer )
2021-03-18 17:14:56 -04:00
log . Printf ( "listening on %s\n" , l . Addr ( ) . String ( ) )
2021-06-16 19:42:11 -04:00
log . Println ( s . Args )
2021-07-05 20:20:38 -04:00
go s . PromethusEndpoint ( "2112" , "metrics" )
2021-05-31 22:19:10 -04:00
if err := s . GrpcServer . Serve ( l ) ; err != nil {
2021-03-18 17:14:56 -04:00
log . Fatalf ( "failed to serve: %v" , err )
}
return
}
2021-05-24 20:09:28 -04:00
conn , err := grpc . Dial ( "localhost" + args . Port ,
grpc . WithInsecure ( ) ,
2021-05-30 21:34:57 -04:00
grpc . WithBlock ( ) ,
2021-05-24 20:09:28 -04:00
)
2021-03-18 17:14:56 -04: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-07-05 20:20:38 -04:00
log . Println ( args )
switch args . CmdType {
case server . SearchCmd :
r , err := c . Search ( ctx , searchRequest )
if err != nil {
log . Fatal ( err )
}
2021-05-15 23:13:14 -04:00
2021-07-05 20:20:38 -04:00
log . Printf ( "found %d results\n" , r . GetTotal ( ) )
2021-04-19 15:25:34 -04:00
2021-07-05 20:20:38 -04:00
for _ , t := range r . Txos {
fmt . Printf ( "%s:%d\n" , util . TxHashToTxId ( t . TxHash ) , t . Nout )
}
default :
log . Fatalln ( "Unknown Command Type!" )
2021-04-19 15:25:34 -04:00
}
2021-03-18 16:12:48 -04:00
}